dos.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 by Nils Sjoholm
  5. members of the Free Pascal development team
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. History:
  14. 10.02.1998 First version for Amiga.
  15. Just GetDate and GetTime.
  16. 11.02.1998 Added AmigaToDt and DtToAmiga
  17. Changed GetDate and GetTime to
  18. use AmigaToDt and DtToAmiga.
  19. Added DiskSize and DiskFree.
  20. They are using a string as arg
  21. have to try to fix that.
  22. 12.02.1998 Added Fsplit and FExpand.
  23. Cleaned up the unit and removed
  24. stuff that was not used yet.
  25. 13.02.1998 Added CToPas and PasToC and removed
  26. the uses of strings.
  27. 14.02.1998 Removed AmigaToDt and DtToAmiga
  28. from public area.
  29. Added deviceids and devicenames
  30. arrays so now diskfree and disksize
  31. is compatible with dos.
  32. }
  33. Unit Dos;
  34. Interface
  35. Type
  36. ComStr = String[255]; { size increased to be more compatible with Unix}
  37. PathStr = String[255]; { size increased to be more compatible with Unix}
  38. DirStr = String[255]; { size increased to be more compatible with Unix}
  39. NameStr = String[255]; { size increased to be more compatible with Unix}
  40. ExtStr = String[255]; { size increased to be more compatible with Unix}
  41. { If you need more devicenames just expand this two arrays }
  42. deviceids = (DF0ID, DF1ID, DF2ID, DF3ID, DH0ID, DH1ID,
  43. CD0ID, MDOS1ID, MDOS2ID);
  44. registers = record
  45. case i : integer of
  46. 0 : (ax,f1,bx,f2,cx,f3,dx,f4,bp,f5,si,f51,di,f6,ds,f7,es,f8,flags,fs,gs : word);
  47. 1 : (al,ah,f9,f10,bl,bh,f11,f12,cl,ch,f13,f14,dl,dh : byte);
  48. 2 : (eax, ebx, ecx, edx, ebp, esi, edi : longint);
  49. end;
  50. Const
  51. devicenames : array [DF0ID..MDOS2ID] of PChar = (
  52. 'df0:','df1:','df2:','df3:','dh0:',
  53. 'dh1:','cd0','A:','B:');
  54. Type
  55. SearchRec = Record
  56. {Fill : array[1..21] of byte; Fill replaced with below}
  57. SearchNum: LongInt; {to track which search this is}
  58. SearchPos: LongInt; {directory position}
  59. DirPtr: LongInt; {directory pointer for reading directory}
  60. SearchType: Byte; {0=normal, 1=open will close}
  61. SearchAttr: Byte; {attribute we are searching for}
  62. Fill: Array[1..07] of Byte; {future use}
  63. {End of replacement for fill}
  64. Attr : Byte; {attribute of found file}
  65. Time : LongInt; {last modify date of found file}
  66. Size : LongInt; {file size of found file}
  67. Reserved : Word; {future use}
  68. Name : String[255]; {name of found file}
  69. SearchSpec: String[255]; {search pattern}
  70. NamePos: Word; {end of path, start of name position}
  71. End;
  72. FileRec = Record
  73. Handle : word;
  74. Mode : word;
  75. RecSize : word;
  76. _private : array[1..26] of byte;
  77. UserData: array[1..16] of byte;
  78. Name: array[0..255] of char;
  79. End;
  80. TextBuf = array[0..127] of char;
  81. TextRec = record
  82. handle : word;
  83. mode : word;
  84. bufSize : word;
  85. _private : word;
  86. bufpos : word;
  87. bufend : word;
  88. bufptr : ^textbuf;
  89. openfunc : pointer;
  90. inoutfunc : pointer;
  91. flushfunc : pointer;
  92. closefunc : pointer;
  93. userdata : array[1..16] of byte;
  94. name : array[0..255] of char;
  95. buffer : textbuf;
  96. End;
  97. DateTime = record
  98. Year: Word;
  99. Month: Word;
  100. Day: Word;
  101. Hour: Word;
  102. Min: Word;
  103. Sec: word;
  104. End;
  105. pClockData = ^tClockData;
  106. tClockData = Record
  107. sec : Word;
  108. min : Word;
  109. hour : Word;
  110. mday : Word;
  111. month : Word;
  112. year : Word;
  113. wday : Word;
  114. END;
  115. Procedure GetDate(var year, month, mday, wday: word);
  116. Procedure GetTime(var hour, minute, second, sec100: word);
  117. Function DosVersion: Word;
  118. procedure SetDate(year,month,day: word);
  119. Procedure SetTime(hour,minute,second,sec100: word);
  120. Procedure GetCBreak(var breakvalue: boolean);
  121. Procedure SetCBreak(breakvalue: boolean);
  122. Procedure GetVerify(var verify: boolean);
  123. Procedure SetVerify(verify: boolean);
  124. Function DiskFree(drive: byte) : longint;
  125. Function DiskSize(drive: byte) : longint;
  126. Procedure FindFirst(const path: pathstr; attr: word; var f: searchRec);
  127. Procedure FindNext(var f: searchRec);
  128. Procedure FindClose(Var f: SearchRec);
  129. Procedure SwapVectors;
  130. Procedure MSDos(var regs: registers);
  131. Procedure GetIntVec(intno: byte; var vector: pointer);
  132. Procedure SetIntVec(intno: byte; vector: pointer);
  133. Procedure Keep(exitcode: word);
  134. Procedure Intr(intno: byte; var regs: registers);
  135. Procedure GetFAttr(var f; var attr: word);
  136. Procedure SetFAttr(var f; attr: word);
  137. Procedure GetFTime(var f; var time: longint);
  138. Procedure SetFTime(var f; time: longint);
  139. Procedure UnpackTime(p: longint; var t: datetime);
  140. Procedure PackTime(var t: datetime; var p: longint);
  141. Function FSearch(path: pathstr; dirlist: string): pathstr;
  142. Function FExpand(const path: pathstr): pathstr;
  143. Procedure FSplit(path: pathstr; var dir: dirstr; var name: namestr;
  144. var ext: extstr);
  145. Procedure Exec(const path: pathstr; const comline: comstr);
  146. Function DosExitCode: word;
  147. Function EnvCount: longint;
  148. Function EnvStr(index: integer): string;
  149. Function GetEnv (envvar: string): string;
  150. Implementation
  151. Type
  152. BPTR = Longint;
  153. {$PACKRECORDS 4}
  154. { Returned by Examine() and ExInfo(), must be on a 4 byte boundary }
  155. pFileInfoBlock = ^tFileInfoBlock;
  156. tFileInfoBlock = record
  157. fib_DiskKey : Longint;
  158. fib_DirEntryType : Longint;
  159. { Type of Directory. If < 0, then a plain file.
  160. If > 0 a directory }
  161. fib_FileName : Array [0..107] of Char;
  162. { Null terminated. Max 30 chars used for now }
  163. fib_Protection : Longint;
  164. { bit mask of protection, rwxd are 3-0. }
  165. fib_EntryType : Longint;
  166. fib_Size : Longint; { Number of bytes in file }
  167. fib_NumBlocks : Longint; { Number of blocks in file }
  168. fib_Date : tDateStamp; { Date file last changed }
  169. fib_Comment : Array [0..79] of Char;
  170. { Null terminated comment associated with file }
  171. fib_OwnerUID : Word;
  172. fib_OwnerGID : Word;
  173. fib_Reserved : Array [0..31] of Char;
  174. end;
  175. { returned by Info(), must be on a 4 byte boundary }
  176. pInfoData = ^tInfoData;
  177. tInfoData = record
  178. id_NumSoftErrors : Longint; { number of soft errors on disk
  179. }
  180. id_UnitNumber : Longint; { Which unit disk is (was)
  181. mounted on }
  182. id_DiskState : Longint; { See defines below }
  183. id_NumBlocks : Longint; { Number of blocks on disk }
  184. id_NumBlocksUsed : Longint; { Number of block in use }
  185. id_BytesPerBlock : Longint;
  186. id_DiskType : Longint; { Disk Type code }
  187. id_VolumeNode : BPTR; { BCPL pointer to volume node }
  188. id_InUse : Longint; { Flag, zero if not in use }
  189. end;
  190. {$PACKRECORDS NORMAL}
  191. procedure CurrentTime(var Seconds, Micros : Longint); Assembler;
  192. asm
  193. MOVE.L A6,-(A7)
  194. MOVE.L _IntuitionBase,A6
  195. MOVE.L Seconds,a0
  196. MOVE.L Micros,a1
  197. JSR -084(A6)
  198. MOVE.L (A7)+,A6
  199. end;
  200. function Date2Amiga(date : pClockData) : Longint; Assembler;
  201. asm
  202. MOVE.L A6,-(A7)
  203. MOVE.L _UtilityBase,A6
  204. MOVE.L date,a0
  205. JSR -126(A6)
  206. MOVE.L (A7)+,A6
  207. end;
  208. procedure Amiga2Date(amigatime : Longint;
  209. resultat : pClockData); Assembler;
  210. asm
  211. MOVE.L A6,-(A7)
  212. MOVE.L _UtilityBase,A6
  213. MOVE.L amigatime,d0
  214. MOVE.L resultat,a0
  215. JSR -120(A6)
  216. MOVE.L (A7)+,A6
  217. end;
  218. function Examine(lock : BPTR;
  219. info : pFileInfoBlock) : Boolean; Assembler;
  220. asm
  221. MOVEM.L d2/a6,-(A7)
  222. MOVE.L _DOSBase,A6
  223. MOVE.L lock,d1
  224. MOVE.L info,d2
  225. JSR -102(A6)
  226. MOVEM.L (A7)+,d2/a6
  227. TST.L d0
  228. SNE d0
  229. NEG.B d0
  230. end;
  231. function Lock(name : Pchar;
  232. accessmode : Longint) : BPTR; Assembler;
  233. asm
  234. MOVEM.L d2/a6,-(A7)
  235. MOVE.L _DOSBase,A6
  236. MOVE.L name,d1
  237. MOVE.L accessmode,d2
  238. JSR -084(A6)
  239. MOVEM.L (A7)+,d2/a6
  240. end;
  241. procedure UnLock(lock : BPTR); Assembler;
  242. asm
  243. MOVE.L A6,-(A7)
  244. MOVE.L _DOSBase,A6
  245. MOVE.L lock,d1
  246. JSR -090(A6)
  247. MOVE.L (A7)+,A6
  248. end;
  249. function Info(lock : BPTR;
  250. params : pInfoData) : Boolean; Assembler;
  251. asm
  252. MOVEM.L d2/a6,-(A7)
  253. MOVE.L _DOSBase,A6
  254. MOVE.L lock,d1
  255. MOVE.L params,d2
  256. JSR -114(A6)
  257. MOVEM.L (A7)+,d2/a6
  258. TST.L d0
  259. SNE d0
  260. NEG.B d0
  261. end;
  262. function NameFromLock(Datei : BPTR;
  263. Buffer : Pchar;
  264. BufferSize : Longint) : Boolean; Assembler;
  265. asm
  266. MOVEM.L d2/d3/a6,-(A7)
  267. MOVE.L _DOSBase,A6
  268. MOVE.L Datei,d1
  269. MOVE.L Buffer,d2
  270. MOVE.L BufferSize,d3
  271. JSR -402(A6)
  272. MOVEM.L (A7)+,d2/d3/a6
  273. TST.L d0
  274. SNE d0
  275. NEG.B d0
  276. end;
  277. function PasToC(var s: string): Pchar;
  278. var i: integer;
  279. begin
  280. i := Length(s) + 1;
  281. if i > 255 then
  282. begin
  283. Delete(s, 255, 1); { ensure there is a spare byte }
  284. Dec(i)
  285. end;
  286. s[i] := #0;
  287. PasToC := @s[1]
  288. end;
  289. procedure CToPas(var s: string);
  290. begin
  291. s[0] := #255;
  292. s[0] := Chr(Pos(#0, s) - 1) { gives -1 (255) if not found }
  293. end;
  294. Function do_exec ( Commandline : pchar; tmp : integer) : integer;
  295. begin
  296. end;
  297. Procedure Intr (intno: byte; var regs: registers);
  298. Begin
  299. { Does not apply to Linux - not implemented }
  300. End;
  301. Var
  302. LastDosExitCode: word;
  303. Procedure Exec (Const Path: PathStr; Const ComLine: ComStr);
  304. Begin
  305. End;
  306. Function DosExitCode: Word;
  307. Begin
  308. End;
  309. Function DosVersion: Word;
  310. Begin
  311. End;
  312. Procedure GetDate(Var Year, Month, MDay, WDay: Word);
  313. Var
  314. cd : pClockData;
  315. mysec,
  316. tick : Longint;
  317. begin
  318. New(cd);
  319. CurrentTime(mysec,tick);
  320. Amiga2Date(mysec,cd);
  321. Year := cd^.year;
  322. Month := cd^.month;
  323. MDay := cd^.mday;
  324. WDay := cd^.wday;
  325. Dispose(cd);
  326. end;
  327. Procedure SetDate(Year, Month, Day: Word);
  328. Begin
  329. { !! }
  330. End;
  331. Procedure GetTime(Var Hour, Minute, Second, Sec100: Word);
  332. Var
  333. mysec,
  334. tick : Longint;
  335. cd : pClockData;
  336. begin
  337. New(cd);
  338. CurrentTime(mysec,tick);
  339. Amiga2Date(mysec,cd);
  340. Hour := cd^.hour;
  341. Minute := cd^.min;
  342. Second := cd^.sec;
  343. Sec100 := 0;
  344. Dispose(cd);
  345. END;
  346. Procedure SetTime(Hour, Minute, Second, Sec100: Word);
  347. Begin
  348. { !! }
  349. End;
  350. Procedure GetCBreak(Var BreakValue: Boolean);
  351. Begin
  352. { Not implemented for Linux, but set to true as a precaution. }
  353. breakvalue:=true
  354. End;
  355. Procedure SetCBreak(BreakValue: Boolean);
  356. Begin
  357. { ! No Linux equivalent ! }
  358. End;
  359. Procedure GetVerify(Var Verify: Boolean);
  360. Begin
  361. { Not implemented for Linux, but set to true as a precaution. }
  362. verify:=true;
  363. End;
  364. Procedure SetVerify(Verify: Boolean);
  365. Begin
  366. { ! No Linux equivalent ! }
  367. End;
  368. Function DiskFree(Drive: Byte): Longint;
  369. Var
  370. MyLock : BPTR;
  371. Inf : pInfoData;
  372. Free : Longint;
  373. Begin
  374. Free := -1;
  375. New(Inf);
  376. MyLock := Lock(devicenames[Drive],-2);
  377. If MyLock <> NIL then begin
  378. if Info(MyLock,Inf) then begin
  379. Free := (Inf^.id_NumBlocks * Inf^.id_BytesPerBlock) -
  380. (Inf^.id_NumBlocksUsed * Inf^.id_BytesPerBlock);
  381. end;
  382. Unlock(MyLock);
  383. end;
  384. Dispose(Inf);
  385. diskfree := Free;
  386. end;
  387. Function DiskSize(Drive: Byte): Longint;
  388. Var
  389. MyLock : BPTR;
  390. Inf : pInfoData;
  391. Size : Longint;
  392. Begin
  393. Size := -1;
  394. New(Inf);
  395. MyLock := Lock(devicenames[Drive],-2);
  396. If MyLock <> NIL then begin
  397. if Info(MyLock,Inf) then begin
  398. Size := (Inf^.id_NumBlocks * Inf^.id_BytesPerBlock);
  399. end;
  400. Unlock(MyLock);
  401. end;
  402. Dispose(Inf);
  403. disksize := Size;
  404. end;
  405. Procedure FindClose(Var f: SearchRec);
  406. Begin
  407. End;
  408. Function FNMatch(Var Pattern: PathStr; Var Name: PathStr): Boolean;
  409. Begin {start FNMatch}
  410. End;
  411. Procedure FindWorkProc(Var f: SearchRec);
  412. Begin
  413. End;
  414. Function FindLastUsed: Word;
  415. Begin
  416. End;
  417. Procedure FindFirst(Const Path: PathStr; Attr: Word; Var f: SearchRec);
  418. Begin
  419. End;
  420. Procedure FindNext(Var f: SearchRec);
  421. Begin
  422. End;
  423. Procedure SwapVectors;
  424. Begin
  425. { Does not apply to Linux - Do Nothing }
  426. End;
  427. Function EnvCount: Longint;
  428. Begin
  429. End;
  430. Function EnvStr(Index: Integer): String;
  431. Begin
  432. End;
  433. Function GetEnv(EnvVar: String): String;
  434. Begin
  435. End;
  436. Procedure FSplit(Path: PathStr; Var Dir: DirStr; Var Name: NameStr;
  437. var
  438. I: Word;
  439. begin
  440. I := Length(Path);
  441. while (I > 0) and not ((Path[I] = '/') or (Path[I] = ':')) do Dec(I);
  442. if Path[I] = '/' then
  443. dir := Copy(Path, 0, I-1)
  444. else dir := Copy(Path,0,I);
  445. if Length(Path) > Length(dir) then
  446. name := Copy(Path, I + 1, Length(Path)-I)
  447. else name := '';
  448. I := Pos('.',Path);
  449. if I > 0 then
  450. ext := Copy(Path,I,Length(Path)-(I-1))
  451. else ext := '';
  452. end;
  453. Function FExpand(Const Path: PathStr): PathStr;
  454. var
  455. FLock : BPTR;
  456. buffer : PathStr;
  457. begin
  458. FLock := Lock(PasToC(Path),-2);
  459. if FLock <> NIL then begin
  460. if NameFromLock(FLock,PasToC(buffer),255) then begin
  461. CToPas(buffer);
  462. Unlock(FLock);
  463. FExpend := buffer;
  464. end else begin
  465. Unlock(FLock);
  466. FExpand := '';
  467. end;
  468. end else FExpand := '';
  469. end;
  470. Procedure msdos(var regs : registers);
  471. Begin
  472. { ! Not implemented in Linux ! }
  473. End;
  474. Procedure getintvec(intno : byte;var vector : pointer);
  475. Begin
  476. { ! Not implemented in Linux ! }
  477. End;
  478. Procedure setintvec(intno : byte;vector : pointer);
  479. Begin
  480. { ! Not implemented in Linux ! }
  481. End;
  482. Procedure keep(exitcode : word);
  483. Begin
  484. { ! Not implemented in Linux ! }
  485. End;
  486. Procedure getfattr(var f; var attr : word);
  487. Begin
  488. End;
  489. Procedure setfattr (var f;attr : word);
  490. Begin
  491. { ! Not implemented in Linux ! }
  492. End;
  493. Procedure getftime (var f; var time : longint);
  494. {
  495. This function returns a file's date and time as the number of
  496. seconds after January 1, 1978 that the file was created.
  497. }
  498. var
  499. FInfo : pFileInfoBlock;
  500. FTime : Longint;
  501. FLock : Longint;
  502. begin
  503. FTime := 0;
  504. FLock := Lock(PasToC(filerec(f).name), -2);
  505. IF FLock <> NIL then begin
  506. New(FInfo);
  507. if Examine(FLock, FInfo) then begin
  508. with FInfo^.fib_Date do
  509. FTime := ds_Days * (24 * 60 * 60) +
  510. ds_Minute * 60 +
  511. ds_Tick div 50;
  512. end else begin
  513. FTime := 0;
  514. end;
  515. Unlock(FLock);
  516. Dispose(FInfo);
  517. end;
  518. time := FTime;
  519. end;
  520. Procedure setftime(var f; time : longint);
  521. Begin
  522. { ! Not implemented in Linux ! }
  523. End;
  524. Procedure unpacktime(p : longint;var t : datetime);
  525. Begin
  526. AmigaToDt(p,t);
  527. End;
  528. Procedure packtime(var t : datetime;var p : longint);
  529. Begin
  530. p := DtToAmiga(t);
  531. end;
  532. Function fsearch(path : pathstr;dirlist : string) : pathstr;
  533. Begin
  534. End;
  535. Procedure AmigaToDt(SecsPast: LongInt; Var Dt: DateTime);
  536. var
  537. cd : pClockData;
  538. Begin
  539. New(cd);
  540. Amiga2Date(SecsPast,cd);
  541. Dt.sec := cd^.sec;
  542. Dt.min := cd^.min;
  543. Dt.hour := cd^.hour;
  544. Dt.day := cd^.mday;
  545. Dt.month := cd^.month;
  546. Dt.year := cd^.year;
  547. Dispose(cd);
  548. End;
  549. Function DtToAmiga(DT: DateTime): LongInt;
  550. var
  551. cd : pClockData;
  552. temp : Longint;
  553. Begin
  554. New(cd);
  555. cd^.sec := Dt.sec;
  556. cd^.min := Dt.min;
  557. cd^.hour := Dt.hour;
  558. cd^.mday := Dt.day;
  559. cd^.month := Dt.month;
  560. cd^.year := Dt.year;
  561. temp := Date2Amiga(cd);
  562. Dispose(cd);
  563. DtToAmiga := temp;
  564. end;
  565. End.