dos.pp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,98 by Michael Van Canneyt and Peter Vreman,
  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. Unit Dos;
  13. Interface
  14. {
  15. If you want to link to the C library, define crtlib.
  16. You can set it here, but it should be set through the makefile
  17. }
  18. {.$DEFINE CRTLIB}
  19. Const
  20. {Max FileName Length for files}
  21. FileNameLen=255;
  22. {Bitmasks for CPU Flags}
  23. fcarry = $0001;
  24. fparity = $0004;
  25. fauxiliary = $0010;
  26. fzero = $0040;
  27. fsign = $0080;
  28. foverflow = $0800;
  29. {Bitmasks for file attribute}
  30. readonly = $01;
  31. hidden = $02;
  32. sysfile = $04;
  33. volumeid = $08;
  34. directory = $10;
  35. archive = $20;
  36. anyfile = $3F;
  37. {File Status}
  38. fmclosed = $D7B0;
  39. fminput = $D7B1;
  40. fmoutput = $D7B2;
  41. fminout = $D7B3;
  42. Type
  43. ComStr = String[FileNameLen];
  44. PathStr = String[FileNameLen];
  45. DirStr = String[FileNameLen];
  46. NameStr = String[FileNameLen];
  47. ExtStr = String[FileNameLen];
  48. {$PACKRECORDS 1}
  49. SearchRec = Record
  50. {Fill : array[1..21] of byte; Fill replaced with below}
  51. SearchNum : LongInt; {to track which search this is}
  52. SearchPos : LongInt; {directory position}
  53. DirPtr : LongInt; {directory pointer for reading directory}
  54. SearchType : Byte; {0=normal, 1=open will close, 2=only 1 file}
  55. SearchAttr : Byte; {attribute we are searching for}
  56. Fill : Array[1..07] of Byte; {future use}
  57. {End of fill}
  58. Attr : Byte; {attribute of found file}
  59. Time : LongInt; {last modify date of found file}
  60. Size : LongInt; {file size of found file}
  61. Reserved : Word; {future use}
  62. Name : String[FileNameLen]; {name of found file}
  63. SearchSpec : String[FileNameLen]; {search pattern}
  64. NamePos : Word; {end of path, start of name position}
  65. End;
  66. {
  67. filerec.inc contains the definition of the filerec.
  68. textrec.inc contains the definition of the textrec.
  69. It is in a separate file to make it available in other units without
  70. having to use the DOS unit for it.
  71. }
  72. {$i filerec.inc}
  73. {$i textrec.inc}
  74. Registers = record
  75. case i : integer of
  76. 0 : (ax,f1,bx,f2,cx,f3,dx,f4,bp,f5,si,f51,di,f6,ds,f7,es,f8,flags,fs,gs : word);
  77. 1 : (al,ah,f9,f10,bl,bh,f11,f12,cl,ch,f13,f14,dl,dh : byte);
  78. 2 : (eax, ebx, ecx, edx, ebp, esi, edi : longint);
  79. End;
  80. DateTime = record
  81. Year,
  82. Month,
  83. Day,
  84. Hour,
  85. Min,
  86. Sec : word;
  87. End;
  88. Var
  89. DosError : integer;
  90. {Utils}
  91. function weekday(y,m,d : longint) : longint;
  92. Procedure UnixDateToDt(SecsPast: LongInt; Var Dt: DateTime);
  93. Function DTToUnixDate(DT: DateTime): LongInt;
  94. {Info/Date/Time}
  95. Function DosVersion: Word;
  96. Procedure GetDate(var year, month, mday, wday: word);
  97. Procedure GetTime(var hour, minute, second, sec100: word);
  98. procedure SetDate(year,month,day: word);
  99. Procedure SetTime(hour,minute,second,sec100: word);
  100. Procedure UnpackTime(p: longint; var t: datetime);
  101. Procedure PackTime(var t: datetime; var p: longint);
  102. {Exec}
  103. Procedure Exec(const path: pathstr; const comline: comstr);
  104. Function DosExitCode: word;
  105. {Disk}
  106. Procedure AddDisk(const path:string);
  107. Function DiskFree(drive: byte) : longint;
  108. Function DiskSize(drive: byte) : longint;
  109. Procedure FindFirst(const path: pathstr; attr: word; var f: searchRec);
  110. Procedure FindNext(var f: searchRec);
  111. Procedure FindClose(Var f: SearchRec);
  112. {File}
  113. Procedure GetFAttr(var f; var attr: word);
  114. Procedure GetFTime(var f; var time: longint);
  115. Function FSearch(path: pathstr; dirlist: string): pathstr;
  116. Function FExpand(const path: pathstr): pathstr;
  117. Procedure FSplit(path: pathstr; var dir: dirstr; var name: namestr; var ext: extstr);
  118. {Environment}
  119. Function EnvCount: longint;
  120. Function EnvStr(index: integer): string;
  121. Function GetEnv (envvar: string): string;
  122. {Do Nothing Functions, no Linux version}
  123. Procedure Intr(intno: byte; var regs: registers);
  124. Procedure MSDos(var regs: registers);
  125. Procedure SwapVectors;
  126. Procedure GetIntVec(intno: byte; var vector: pointer);
  127. Procedure SetIntVec(intno: byte; vector: pointer);
  128. Procedure Keep(exitcode: word);
  129. Procedure SetFAttr(var f; attr: word);
  130. Procedure SetFTime(var f; time: longint);
  131. Procedure GetCBreak(var breakvalue: boolean);
  132. Procedure SetCBreak(breakvalue: boolean);
  133. Procedure GetVerify(var verify: boolean);
  134. Procedure SetVerify(verify: boolean);
  135. Implementation
  136. Uses
  137. Strings
  138. {$ifndef crtlib}
  139. ,linux
  140. {$endif}
  141. ;
  142. {******************************************************************************
  143. --- Link C Lib if set ---
  144. ******************************************************************************}
  145. type
  146. RtlInfoType = Record
  147. FMode,
  148. FInode,
  149. FUid,
  150. FGid,
  151. FSize,
  152. FMTime : LongInt;
  153. End;
  154. {$IFDEF CRTLIB}
  155. {Links to C library}
  156. Procedure _rtl_getenv(target: pchar; st: pchar); [ C ];
  157. Procedure _rtl_envstr(i: longint; st: pchar); [ C ];
  158. Function _rtl_envcnt: longint; [ C ];
  159. Procedure _rtl_gettime(gt: longint); [ C ];
  160. Procedure _rtl_getversion(rel: pchar); [ C ];
  161. Function _rtl_exec(cmdline: pchar; var exitst: integer): integer; [ C ];
  162. Procedure _rtl_closedir(dirptr: longint); [ C ];
  163. Procedure _rtl_seekdir(dirptr: longint; seekpos: longint); [ C ];
  164. Function _rtl_telldir(dirptr: longint): longint; [ C ];
  165. Function _rtl_opendir(path: pchar): longint; [ C ];
  166. Procedure _rtl_readdir(dirptr: longint; dname: pchar); [ C ];
  167. Procedure _rtl_stat(path: pchar; infoptr: longint); [ C ];
  168. Procedure _rtl_fstat(fd: longint; infoptr: longint); [ C ];
  169. {$ENDIF CRTLIB}
  170. {******************************************************************************
  171. --- Info / Date / Time ---
  172. ******************************************************************************}
  173. Const
  174. {Date Calculation}
  175. C1970 = 2440588;
  176. D0 = 1461;
  177. D1 = 146097;
  178. D2 = 1721119;
  179. type
  180. {$PACKRECORDS 1}
  181. GTRec = Record
  182. Year,
  183. Month,
  184. MDay,
  185. WDay,
  186. Hour,
  187. Minute,
  188. Second : Word;
  189. End;
  190. Function DosVersion:Word;
  191. Var
  192. Buffer : Array[0..255] of Char;
  193. Tmp2,
  194. TmpStr : String[40];
  195. TmpPos,
  196. SubRel,
  197. Rel : LongInt;
  198. info : utsname;
  199. Begin
  200. {$IFDEF CRTLIB}
  201. _rtl_getversion(buffer);
  202. {$ELSE}
  203. UName(info);
  204. Move(info.release,buffer[0],40);
  205. {$ENDIF}
  206. TmpStr:=StrPas(Buffer);
  207. SubRel:=0;
  208. TmpPos:=Pos('.',TmpStr);
  209. if TmpPos>0 then
  210. begin
  211. Tmp2:=Copy(TmpStr,TmpPos+1,40);
  212. Delete(TmpStr,TmpPos,40);
  213. end;
  214. TmpPos:=Pos('.',Tmp2);
  215. if TmpPos>0 then
  216. Delete(Tmp2,TmpPos,40);
  217. Val(TmpStr,Rel);
  218. Val(Tmp2,SubRel);
  219. DosVersion:=Rel+(SubRel shl 8);
  220. End;
  221. function WeekDay (y,m,d:longint):longint;
  222. {
  223. Calculates th day of the week. returns -1 on error
  224. }
  225. var
  226. u,v : longint;
  227. begin
  228. if (m<1) or (m>12) or (y<1600) or (y>4000) or
  229. (d<1) or (d>30+((m+ord(m>7)) and 1)-ord(m=2)) or
  230. ((m*d=58) and (((y mod 4>0) or (y mod 100=0)) and (y mod 400>0))) then
  231. WeekDay:=-1
  232. else
  233. begin
  234. u:=m;
  235. v:=y;
  236. if m<3 then
  237. begin
  238. inc(u,12);
  239. dec(v);
  240. end;
  241. WeekDay:=(d+2*u+((3*(u+1)) div 5)+v+(v div 4)-(v div 100)+(v div 400)+1) mod 7;
  242. end;
  243. end;
  244. Procedure GetDate(Var Year, Month, MDay, WDay: Word);
  245. {$IFDEF CRTLIB}
  246. Var
  247. gt : GTRec;
  248. {$ENDIF}
  249. Begin
  250. {$IFDEF CRTLIB}
  251. _rtl_gettime(longint(@gt));
  252. Year:=gt.year+1900;
  253. Month:=gt.month+1;
  254. MDay:=gt.mday;
  255. WDay:=gt.wday;
  256. {$ELSE}
  257. Linux.GetDate(Year,Month,MDay);
  258. Wday:=weekday(Year,Month,MDay);
  259. {$ENDIF}
  260. end;
  261. Procedure SetDate(Year, Month, Day: Word);
  262. Begin
  263. {!!}
  264. End;
  265. Procedure GetTime(Var Hour, Minute, Second, Sec100: Word);
  266. {$IFDEF CRTLIB}
  267. Var
  268. gt : GTRec;
  269. {$ENDIF}
  270. Begin
  271. {$IFDEF CRTLIB}
  272. _rtl_gettime(longint(@gt));
  273. Hour := GT.Hour;
  274. Minute := GT.Minute;
  275. Second := GT.Second;
  276. {$ELSE}
  277. Linux.GetTime(Hour,Minute,Second);
  278. {$ENDIF}
  279. Sec100 := 0;
  280. end;
  281. Procedure SetTime(Hour, Minute, Second, Sec100: Word);
  282. Begin
  283. {!!}
  284. End;
  285. Procedure packtime(var t : datetime;var p : longint);
  286. Begin
  287. p:=(t.sec shr 1)+(t.min shl 5)+(t.hour shl 11)+(t.day shl 16)+(t.month shl 21)+((t.year-1980) shl 25);
  288. End;
  289. Procedure unpacktime(p : longint;var t : datetime);
  290. Begin
  291. t.sec:=(p and 31) shl 1;
  292. t.min:=(p shr 5) and 63;
  293. t.hour:=(p shr 11) and 31;
  294. t.day:=(p shr 16) and 31;
  295. t.month:=(p shr 21) and 15;
  296. t.year:=(p shr 25)+1980;
  297. End;
  298. Procedure UnixDateToDt(SecsPast: LongInt; Var Dt: DateTime);
  299. Begin
  300. EpochToLocal(SecsPast,dt.Year,dt.Month,dt.Day,dt.Hour,dt.Min,dt.Sec);
  301. End;
  302. Function DTToUnixDate(DT: DateTime): LongInt;
  303. Begin
  304. DTToUnixDate:=LocalToEpoch(dt.Year,dt.Month,dt.Day,dt.Hour,dt.Min,dt.Sec);
  305. End;
  306. {******************************************************************************
  307. --- Exec ---
  308. ******************************************************************************}
  309. var
  310. LastDosExitCode: word;
  311. Procedure Exec (Const Path: PathStr; Const ComLine: ComStr);
  312. var
  313. {$IFDEF CRTLIB}
  314. Buf : Array[0..512] of Char;
  315. i : Integer;
  316. {$ELSE}
  317. pid : longint;
  318. status : integer;
  319. {$ENDIF}
  320. Begin
  321. {$IFDEF CRTLIB}
  322. i:=Length(Path);
  323. Move(Path[1],Buf[0],i);
  324. Buf[i]:=' ';
  325. Move(ComLine[1],Buf[i+1],Length(ComLine));
  326. Buf[i+Length(ComLine)+1]:=#0;
  327. i:=0;
  328. LastDosExitCode := _rtl_exec(pchar(@buf), i);
  329. Doserror:=i;
  330. {$ELSE}
  331. pid:=Fork;
  332. if pid=0 then
  333. begin
  334. {The child does the actual exec, and then exits}
  335. Execl (Path+' '+ComLine);
  336. {If the execve fails, we return an exitvalue of 127, to let it be known}
  337. halt (127)
  338. end
  339. else
  340. if pid=-1 then {Fork failed}
  341. begin
  342. DosError:=8;
  343. exit
  344. end;
  345. {We're in the parent, let's wait.}
  346. Waitpid (pid,@status,0);
  347. if status=127 then {The child couldn't execve !!}
  348. DosError:=8 {We set this error, erroneously, since we cannot get to the real error}
  349. else
  350. begin
  351. LastDosExitCode:=status shr 8;
  352. DosError:=0
  353. end;
  354. {$ENDIF}
  355. End;
  356. Function DosExitCode: Word;
  357. Begin
  358. DosExitCode:=LastDosExitCode;
  359. End;
  360. {******************************************************************************
  361. --- Disk ---
  362. ******************************************************************************}
  363. {
  364. The Diskfree and Disksize functions need a file on the specified drive, since this
  365. is required for the statfs system call.
  366. These filenames are set in drivestr[0..26], and have been preset to :
  367. 0 - '.' (default drive - hence current dir is ok.)
  368. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  369. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  370. 3 - '/' (C: equivalent of dos is the root partition)
  371. 4..26 (can be set by you're own applications)
  372. ! Use AddDisk() to Add new drives !
  373. They both return -1 when a failure occurs.
  374. }
  375. Const
  376. FixDriveStr : array[0..3] of pchar=(
  377. '.',
  378. '/fd0/.',
  379. '/fd1/.',
  380. '/.'
  381. );
  382. var
  383. Drives : byte;
  384. DriveStr : array[4..26] of pchar;
  385. Procedure AddDisk(const path:string);
  386. begin
  387. if not (DriveStr[Drives]=nil) then
  388. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  389. GetMem(DriveStr[Drives],length(Path)+1);
  390. StrPCopy(DriveStr[Drives],path);
  391. inc(Drives);
  392. if Drives>26 then
  393. Drives:=4;
  394. end;
  395. Function DiskFree(Drive: Byte): Longint;
  396. {$IFNDEF CRTLIB}
  397. var
  398. fs : statfs;
  399. {$ENDIF}
  400. Begin
  401. {$IFNDEF CRTLIB}
  402. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  403. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  404. Diskfree:=fs.bavail*fs.bsize
  405. else
  406. Diskfree:=-1;
  407. {$ENDIF}
  408. End;
  409. Function DiskSize(Drive: Byte): Longint;
  410. {$IFNDEF CRTLIB}
  411. var
  412. fs : statfs;
  413. {$ENDIF}
  414. Begin
  415. {$IFNDEF CRTLIB}
  416. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  417. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  418. DiskSize:=fs.blocks*fs.bsize
  419. else
  420. DiskSize:=-1;
  421. {$ENDIF}
  422. End;
  423. {******************************************************************************
  424. --- Findfirst FindNext ---
  425. ******************************************************************************}
  426. Const
  427. RtlFindSize = 15;
  428. Type
  429. RtlFindRecType = Record
  430. SearchNum,
  431. DirPtr,
  432. LastUsed : LongInt;
  433. End;
  434. Var
  435. RtlFindRecs : Array[1..RtlFindSize] of RtlFindRecType;
  436. CurrSearchNum : LongInt;
  437. Procedure FindClose(Var f: SearchRec);
  438. {
  439. Closes dirptr if it is open
  440. }
  441. Var
  442. i : longint;
  443. Begin
  444. if f.SearchType=0 then
  445. begin
  446. i:=1;
  447. repeat
  448. if (RtlFindRecs[i].SearchNum=f.SearchNum) then
  449. break;
  450. inc(i);
  451. until (i>RtlFindSize);
  452. If i<=RtlFindSize Then
  453. Begin
  454. RtlFindRecs[i].SearchNum:=0;
  455. if f.dirptr>0 then
  456. begin
  457. {$IFDEF CRTLIB}
  458. _rtl_closeDir(f.dirptr);
  459. {$ELSE}
  460. closedir(pdir(f.dirptr));
  461. {$ENDIF}
  462. Dispose(pdir(f.dirptr)^.buf);
  463. Dispose(pdir(f.dirptr));
  464. end;
  465. End;
  466. end;
  467. f.dirptr:=0;
  468. End;
  469. Function FindGetFileInfo(const s:string;var f:SearchRec):boolean;
  470. var
  471. DT : DateTime;
  472. Info : RtlInfoType;
  473. {$IFDEF CRTLIB}
  474. buf : array[0..255] of char;
  475. {$ELSE}
  476. st : stat;
  477. {$ENDIF}
  478. begin
  479. FindGetFileInfo:=false;
  480. {$IFDEF CRTLIB}
  481. move(s[1],buf,length(s));
  482. buf[length(s)]:=#0;
  483. _rtl_stat(@buf, LongInt(@Info));
  484. {$ELSE}
  485. if not Fstat(s,st) then
  486. exit;
  487. info.FSize:=st.Size;
  488. info.FMTime:=st.mtime;
  489. if (st.mode and STAT_IFMT)=STAT_IFDIR then
  490. info.fmode:=$10
  491. else
  492. info.fmode:=$20;
  493. if (st.mode and STAT_IWUSR)=0 then
  494. info.fmode:=info.fmode or 1;
  495. {$ENDIF}
  496. If ((Info.FMode and Not(f.searchattr))=0) Then
  497. Begin
  498. f.Name:=Copy(s,f.NamePos+1,255);
  499. f.Attr:=Info.FMode;
  500. f.Size:=Info.FSize;
  501. UnixDateToDT(Info.FMTime, DT);
  502. PackTime(DT,f.Time);
  503. FindGetFileInfo:=true;
  504. End;
  505. end;
  506. Function FindLastUsed: Longint;
  507. {
  508. Find unused or least recently used dirpointer slot in findrecs array
  509. }
  510. Var
  511. BestMatch,i : Longint;
  512. Found : Boolean;
  513. Begin
  514. BestMatch:=1;
  515. i:=1;
  516. Found:=False;
  517. While (i <= RtlFindSize) And (Not Found) Do
  518. Begin
  519. If (RtlFindRecs[i].SearchNum = 0) Then
  520. Begin
  521. BestMatch := i;
  522. Found := True;
  523. End
  524. Else
  525. Begin
  526. If RtlFindRecs[i].LastUsed > RtlFindRecs[BestMatch].LastUsed Then
  527. BestMatch := i;
  528. End;
  529. Inc(i);
  530. End;
  531. FindLastUsed := BestMatch;
  532. End;
  533. Procedure FindNext(Var f: SearchRec);
  534. {
  535. re-opens dir if not already in array and calls FindWorkProc
  536. }
  537. Var
  538. DirName : Array[0..256] of Char;
  539. i,
  540. ArrayPos : Longint;
  541. FName,
  542. SName : string;
  543. Found,
  544. Finished : boolean;
  545. {$IFNDEF CRTLIB}
  546. p : PDirEnt;
  547. {$ENDIF}
  548. Begin
  549. If f.SearchType=0 Then
  550. Begin
  551. ArrayPos:=0;
  552. For i:=1 to RtlFindSize Do
  553. Begin
  554. If RtlFindRecs[i].SearchNum = f.SearchNum Then
  555. ArrayPos:=i;
  556. Inc(RtlFindRecs[i].LastUsed);
  557. End;
  558. If ArrayPos=0 Then
  559. Begin
  560. If f.NamePos = 0 Then
  561. Begin
  562. DirName[0] := '.';
  563. DirName[1] := '/';
  564. DirName[2] := #0;
  565. End
  566. Else
  567. Begin
  568. Move(f.SearchSpec[1], DirName[0], f.NamePos);
  569. DirName[f.NamePos] := #0;
  570. End;
  571. {$IFDEF CRTLIB}
  572. f.DirPtr := _rtl_opendir(DirName);
  573. {$ELSE}
  574. f.DirPtr := longint(opendir(@(DirName)));
  575. {$ENDIF}
  576. If f.DirPtr > 0 Then
  577. begin
  578. ArrayPos:=FindLastUsed;
  579. If RtlFindRecs[ArrayPos].SearchNum > 0 Then
  580. Begin
  581. {$IFDEF CRTLIB}
  582. _rtl_closeDir(rtlfindrecs[arraypos].dirptr);
  583. {$ELSE}
  584. CloseDir(pdir(rtlfindrecs[arraypos].dirptr));
  585. {$ENDIF}
  586. End;
  587. RtlFindRecs[ArrayPos].SearchNum := f.SearchNum;
  588. RtlFindRecs[ArrayPos].DirPtr := f.DirPtr;
  589. if f.searchpos>0 then
  590. begin
  591. {$IFDEF CRTLIB}
  592. _rtl_seekdir(f.dirptr, f.searchpos);
  593. {$ELSE}
  594. seekdir(pdir(f.dirptr), f.searchpos);
  595. {$ENDIF}
  596. end;
  597. end;
  598. End;
  599. if ArrayPos>0 then
  600. RtlFindRecs[ArrayPos].LastUsed:=0;
  601. end;
  602. {Main loop}
  603. SName:=Copy(f.SearchSpec,f.NamePos+1,255);
  604. Found:=False;
  605. Finished:=(f.dirptr=0);
  606. While Not Finished Do
  607. Begin
  608. {$IFDEF CRTLIB}
  609. _rtl_readdir(f.dirptr, @FBuf);
  610. FName:=StrPas(FBuf[0]);
  611. {$ELSE}
  612. p:=readdir(pdir(f.dirptr));
  613. if p=nil then
  614. FName:=''
  615. else
  616. FName:=Strpas(@p^.name);
  617. {$ENDIF}
  618. If FName='' Then
  619. Finished:=True
  620. Else
  621. Begin
  622. If FNMatch(SName,FName) Then
  623. Begin
  624. Found:=FindGetFileInfo(Copy(f.SearchSpec,1,f.NamePos)+FName,f);
  625. if Found then
  626. Finished:=true;
  627. End;
  628. End;
  629. End;
  630. {Shutdown}
  631. If Found Then
  632. Begin
  633. {$IFDEF CRTLIB}
  634. f.searchpos:=_rtl_telldir(f.dirptr);
  635. {$ELSE}
  636. f.searchpos:=telldir(pdir(f.dirptr));
  637. {$ENDIF}
  638. DosError:=0;
  639. End
  640. Else
  641. Begin
  642. FindClose(f);
  643. DosError:=18;
  644. End;
  645. End;
  646. Procedure FindFirst(Const Path: PathStr; Attr: Word; Var f: SearchRec);
  647. {
  648. opens dir and calls FindWorkProc
  649. }
  650. Begin
  651. if Path='' then
  652. begin
  653. DosError:=3;
  654. exit;
  655. end;
  656. {Create Info}
  657. f.SearchSpec := Path;
  658. f.SearchAttr := Attr;
  659. f.SearchPos:=0;
  660. f.NamePos := Length(f.SearchSpec);
  661. while (f.NamePos>0) and (f.SearchSpec[f.NamePos]<>'/') do
  662. dec(f.NamePos);
  663. {Wildcards?}
  664. if (Pos('?',Path)=0) and (Pos('*',Path)=0) then
  665. begin
  666. if FindGetFileInfo(Path,f) then
  667. DosError:=0
  668. else
  669. begin
  670. if ErrNo=Sys_ENOENT then
  671. DosError:=3
  672. else
  673. DosError:=18;
  674. end;
  675. f.DirPtr:=0;
  676. f.SearchType:=1;
  677. f.searchnum:=-1;
  678. end
  679. else
  680. {Find Entry}
  681. begin
  682. Inc(CurrSearchNum);
  683. f.SearchNum:=CurrSearchNum;
  684. f.SearchType:=0;
  685. FindNext(f);
  686. end;
  687. End;
  688. {******************************************************************************
  689. --- File ---
  690. ******************************************************************************}
  691. Procedure FSplit(Path: PathStr; Var Dir: DirStr; Var Name: NameStr;Var Ext: ExtStr);
  692. Begin
  693. Linux.FSplit(Path,Dir,Name,Ext);
  694. End;
  695. Function FExpand(Const Path: PathStr): PathStr;
  696. Begin
  697. FExpand:=Linux.FExpand(Path);
  698. End;
  699. Function FSearch(path : pathstr;dirlist : string) : pathstr;
  700. Begin
  701. FSearch:=Linux.FSearch(path,dirlist);
  702. End;
  703. Procedure GetFAttr(var f; var attr : word);
  704. Var
  705. {$IFDEF CRTLIB}
  706. Info: RtlInfoType;
  707. {$ELSE}
  708. info : stat;
  709. {$ENDIF}
  710. LinAttr : longint;
  711. Begin
  712. {$IFDEF CRTLIB}
  713. _rtl_fstat(word(f), longint(@Info));
  714. attr := info.fmode;
  715. {$ELSE}
  716. if not FStat(strpas(@textrec(f).name),info) then
  717. begin
  718. Attr:=0;
  719. DosError:=3;
  720. exit;
  721. end
  722. else
  723. LinAttr:=Info.Mode;
  724. if S_ISDIR(LinAttr) then
  725. Attr:=$10
  726. else
  727. Attr:=$20;
  728. if not Access(strpas(@textrec(f).name),W_OK) then
  729. Attr:=Attr or $1;
  730. if (not S_ISDIR(LinAttr)) and (filerec(f).name[0]='.') then
  731. Attr:=Attr or $2;
  732. {$Endif}
  733. end;
  734. Procedure getftime (var f; var time : longint);
  735. Var
  736. {$IFDEF CRTLIB}
  737. Info: RtlInfoType;
  738. {$ELSE}
  739. info : stat;
  740. {$ENDIF}
  741. DT: DateTime;
  742. Begin
  743. doserror:=0;
  744. {$IFDEF CRTLIB}
  745. _rtl_fstat(word(f), longint(@Info));
  746. UnixDateToDT(Info.FMTime, DT);
  747. {$ELSE}
  748. if not fstat(filerec(f).handle,info) then
  749. begin
  750. Time:=0;
  751. doserror:=3;
  752. exit
  753. end
  754. else
  755. UnixDateToDT(Info.mTime,DT);
  756. {$ENDIF}
  757. PackTime(DT,Time);
  758. End;
  759. {******************************************************************************
  760. --- Environment ---
  761. ******************************************************************************}
  762. Function EnvCount: Longint;
  763. var
  764. envcnt : longint;
  765. p : ppchar;
  766. Begin
  767. {$IFDEF CRTLIB}
  768. EnvCount := _rtl_envcnt;
  769. {$ELSE}
  770. envcnt:=0;
  771. p:=envp; {defined in syslinux}
  772. while (p^<>nil) do
  773. begin
  774. inc(envcnt);
  775. p:=p+4
  776. end;
  777. EnvCount := envcnt
  778. {$ENDIF}
  779. End;
  780. Function EnvStr(Index: Integer): String;
  781. Var
  782. {$IFDEF CRTLIB}
  783. Buffer: Array[0..255] of Char;
  784. {$ELSE}
  785. i : longint;
  786. p : ppchar;
  787. {$ENDIF}
  788. Begin
  789. {$IFDEF CRTLIB}
  790. Buffer[0]:=#0; {Be sure there is at least nothing}
  791. _rtl_envstr(index, buffer);
  792. EnvStr:=StrPas(Buffer);
  793. {$ELSE}
  794. p:=envp; {defined in syslinux}
  795. i:=1;
  796. while (i<Index) and (p^<>nil) do
  797. begin
  798. inc(i);
  799. p:=p+4;
  800. end;
  801. if p=nil then
  802. envstr:=''
  803. else
  804. envstr:=strpas(p^)
  805. {$ENDIF}
  806. End;
  807. Function GetEnv(EnvVar: String): String;
  808. var
  809. {$IFDEF CRTLIB}
  810. Buffer,
  811. OutStr : Array[0..255] of Char;
  812. {$ELSE}
  813. p : pchar;
  814. {$ENDIF}
  815. Begin
  816. {$IFDEF CRTLIB}
  817. Move(EnvVar[1],Buffer,Length(EnvVar));
  818. Buffer[Length(EnvVar)]:=#0;
  819. OutStr[0]:=#0;
  820. _rtl_getenv(buffer,outstr);
  821. GetEnv:=StrPas(Buffer);
  822. {$ELSE}
  823. p:=Linux.GetEnv(EnvVar);
  824. if p=nil then
  825. GetEnv:=''
  826. else
  827. GetEnv:=StrPas(p);
  828. {$ENDIF}
  829. End;
  830. {******************************************************************************
  831. --- Do Nothing Procedures/Functions ---
  832. ******************************************************************************}
  833. Procedure Intr (intno: byte; var regs: registers);
  834. Begin
  835. {! No Linux equivalent !}
  836. End;
  837. Procedure msdos(var regs : registers);
  838. Begin
  839. {! No Linux equivalent !}
  840. End;
  841. Procedure getintvec(intno : byte;var vector : pointer);
  842. Begin
  843. {! No Linux equivalent !}
  844. End;
  845. Procedure setintvec(intno : byte;vector : pointer);
  846. Begin
  847. {! No Linux equivalent !}
  848. End;
  849. Procedure SwapVectors;
  850. Begin
  851. {! No Linux equivalent !}
  852. End;
  853. Procedure keep(exitcode : word);
  854. Begin
  855. {! No Linux equivalent !}
  856. End;
  857. Procedure setftime(var f; time : longint);
  858. Begin
  859. {! No Linux equivalent !}
  860. End;
  861. Procedure setfattr (var f;attr : word);
  862. Begin
  863. {! No Linux equivalent !}
  864. End;
  865. Procedure GetCBreak(Var BreakValue: Boolean);
  866. Begin
  867. {! No Linux equivalent !}
  868. breakvalue:=true
  869. End;
  870. Procedure SetCBreak(BreakValue: Boolean);
  871. Begin
  872. {! No Linux equivalent !}
  873. End;
  874. Procedure GetVerify(Var Verify: Boolean);
  875. Begin
  876. {! No Linux equivalent !}
  877. Verify:=true;
  878. End;
  879. Procedure SetVerify(Verify: Boolean);
  880. Begin
  881. {! No Linux equivalent !}
  882. End;
  883. {******************************************************************************
  884. --- Initialization ---
  885. ******************************************************************************}
  886. End.
  887. {
  888. $Log$
  889. Revision 1.10 1999-03-05 13:09:57 peter
  890. * fix for findfirst from the mailinglist
  891. Revision 1.9 1999/02/22 11:45:19 peter
  892. * fixed findlastused (from mailinglist)
  893. Revision 1.8 1999/01/28 12:54:13 michael
  894. + Fixed memory leak in findfirst/findnext
  895. Revision 1.7 1999/01/28 12:10:42 michael
  896. + Fixed findclose bug
  897. Revision 1.6 1998/11/23 12:32:31 peter
  898. * fix for findclose from the mailinglist
  899. Revision 1.5 1998/11/05 14:24:08 peter
  900. * findfirst fix from the mailinglist
  901. Revision 1.4 1998/11/04 10:15:54 peter
  902. * don't use getmem in startup (necessary for heaptrc)
  903. Revision 1.3 1998/05/06 12:35:26 michael
  904. + Removed log from before restored version.
  905. Revision 1.2 1998/05/04 17:40:43 peter
  906. * findfirst did some strange init with searchpos
  907. Revision 1.1.1.1 1998/03/25 11:18:43 root
  908. * Restored version
  909. }