dos.pp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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:=0;
  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. end;
  463. End;
  464. end;
  465. f.dirptr:=0;
  466. End;
  467. Function FindGetFileInfo(const s:string;var f:SearchRec):boolean;
  468. var
  469. DT : DateTime;
  470. Info : RtlInfoType;
  471. {$IFDEF CRTLIB}
  472. buf : array[0..255] of char;
  473. {$ELSE}
  474. st : stat;
  475. {$ENDIF}
  476. begin
  477. FindGetFileInfo:=false;
  478. {$IFDEF CRTLIB}
  479. move(s[1],buf,length(s));
  480. buf[length(s)]:=#0;
  481. _rtl_stat(@buf, LongInt(@Info));
  482. {$ELSE}
  483. if not Fstat(s,st) then
  484. exit;
  485. info.FSize:=st.Size;
  486. info.FMTime:=st.mtime;
  487. if (st.mode and STAT_IFMT)=STAT_IFDIR then
  488. info.fmode:=$10
  489. else
  490. info.fmode:=$20;
  491. if (st.mode and STAT_IWUSR)=0 then
  492. info.fmode:=info.fmode or 1;
  493. {$ENDIF}
  494. If ((Info.FMode and Not(f.searchattr))=0) Then
  495. Begin
  496. f.Name:=Copy(s,f.NamePos+1,255);
  497. f.Attr:=Info.FMode;
  498. f.Size:=Info.FSize;
  499. UnixDateToDT(Info.FMTime, DT);
  500. PackTime(DT,f.Time);
  501. FindGetFileInfo:=true;
  502. End;
  503. end;
  504. Function FindLastUsed: Longint;
  505. {
  506. Find unused or least recently used dirpointer slot in findrecs array
  507. }
  508. Var
  509. BestMatch,i : Longint;
  510. Found : Boolean;
  511. Begin
  512. BestMatch:=0;
  513. i:=1;
  514. Found:=False;
  515. While (i <= RtlFindSize) And (Not Found) Do
  516. Begin
  517. If (RtlFindRecs[i].SearchNum = 0) Then
  518. Begin
  519. BestMatch := i;
  520. Found := True;
  521. End
  522. Else
  523. Begin
  524. If RtlFindRecs[i].LastUsed > RtlFindRecs[BestMatch].LastUsed Then
  525. BestMatch := i;
  526. End;
  527. Inc(i);
  528. End;
  529. FindLastUsed := BestMatch;
  530. End;
  531. Procedure FindNext(Var f: SearchRec);
  532. {
  533. re-opens dir if not already in array and calls FindWorkProc
  534. }
  535. Var
  536. DirName : Array[0..256] of Char;
  537. i,
  538. ArrayPos : Longint;
  539. FName,
  540. SName : string;
  541. Found,
  542. Finished : boolean;
  543. {$IFNDEF CRTLIB}
  544. p : PDirEnt;
  545. {$ENDIF}
  546. Begin
  547. If f.SearchType=0 Then
  548. Begin
  549. ArrayPos:=0;
  550. For i:=1 to RtlFindSize Do
  551. Begin
  552. If RtlFindRecs[i].SearchNum = f.SearchNum Then
  553. ArrayPos:=i;
  554. Inc(RtlFindRecs[i].LastUsed);
  555. End;
  556. If ArrayPos=0 Then
  557. Begin
  558. If f.NamePos = 0 Then
  559. Begin
  560. DirName[0] := '.';
  561. DirName[1] := '/';
  562. DirName[2] := #0;
  563. End
  564. Else
  565. Begin
  566. Move(f.SearchSpec[1], DirName[0], f.NamePos);
  567. DirName[f.NamePos] := #0;
  568. End;
  569. {$IFDEF CRTLIB}
  570. f.DirPtr := _rtl_opendir(DirName);
  571. {$ELSE}
  572. f.DirPtr := longint(opendir(@(DirName)));
  573. {$ENDIF}
  574. If f.DirPtr > 0 Then
  575. begin
  576. ArrayPos:=FindLastUsed;
  577. If RtlFindRecs[ArrayPos].SearchNum > 0 Then
  578. Begin
  579. {$IFDEF CRTLIB}
  580. _rtl_closeDir(rtlfindrecs[arraypos].dirptr);
  581. {$ELSE}
  582. CloseDir(pdir(rtlfindrecs[arraypos].dirptr));
  583. {$ENDIF}
  584. End;
  585. RtlFindRecs[ArrayPos].SearchNum := f.SearchNum;
  586. RtlFindRecs[ArrayPos].DirPtr := f.DirPtr;
  587. if f.searchpos>0 then
  588. begin
  589. {$IFDEF CRTLIB}
  590. _rtl_seekdir(f.dirptr, f.searchpos);
  591. {$ELSE}
  592. seekdir(pdir(f.dirptr), f.searchpos);
  593. {$ENDIF}
  594. end;
  595. end;
  596. End;
  597. RtlFindRecs[ArrayPos].LastUsed:=0;
  598. end;
  599. {Main loop}
  600. SName:=Copy(f.SearchSpec,f.NamePos+1,255);
  601. Found:=False;
  602. Finished:=(f.dirptr=0);
  603. While Not Finished Do
  604. Begin
  605. {$IFDEF CRTLIB}
  606. _rtl_readdir(f.dirptr, @FBuf);
  607. FName:=StrPas(FBuf[0]);
  608. {$ELSE}
  609. p:=readdir(pdir(f.dirptr));
  610. if p=nil then
  611. FName:=''
  612. else
  613. FName:=Strpas(@p^.name);
  614. {$ENDIF}
  615. If FName='' Then
  616. Finished:=True
  617. Else
  618. Begin
  619. If FNMatch(SName,FName) Then
  620. Begin
  621. Found:=FindGetFileInfo(Copy(f.SearchSpec,1,f.NamePos)+FName,f);
  622. if Found then
  623. Finished:=true;
  624. End;
  625. End;
  626. End;
  627. {Shutdown}
  628. If Found Then
  629. Begin
  630. {$IFDEF CRTLIB}
  631. f.searchpos:=_rtl_telldir(f.dirptr);
  632. {$ELSE}
  633. f.searchpos:=telldir(pdir(f.dirptr));
  634. {$ENDIF}
  635. DosError:=0;
  636. End
  637. Else
  638. Begin
  639. FindClose(f);
  640. DosError:=18;
  641. End;
  642. End;
  643. Procedure FindFirst(Const Path: PathStr; Attr: Word; Var f: SearchRec);
  644. {
  645. opens dir and calls FindWorkProc
  646. }
  647. Begin
  648. if Path='' then
  649. begin
  650. DosError:=3;
  651. exit;
  652. end;
  653. {Create Info}
  654. f.SearchSpec := Path;
  655. f.SearchAttr := Attr;
  656. f.SearchPos:=0;
  657. f.NamePos := Length(f.SearchSpec);
  658. while (f.NamePos>0) and (f.SearchSpec[f.NamePos]<>'/') do
  659. dec(f.NamePos);
  660. {Wildcards?}
  661. if (Pos('?',Path)=0) and (Pos('*',Path)=0) then
  662. begin
  663. if FindGetFileInfo(Path,f) then
  664. DosError:=0
  665. else
  666. begin
  667. if ErrNo=Sys_ENOENT then
  668. DosError:=3
  669. else
  670. DosError:=18;
  671. end;
  672. f.DirPtr:=0;
  673. f.SearchType:=1;
  674. f.searchnum:=-1;
  675. end
  676. else
  677. {Find Entry}
  678. begin
  679. Inc(CurrSearchNum);
  680. f.SearchNum:=CurrSearchNum;
  681. f.SearchType:=0;
  682. FindNext(f);
  683. end;
  684. End;
  685. {******************************************************************************
  686. --- File ---
  687. ******************************************************************************}
  688. Procedure FSplit(Path: PathStr; Var Dir: DirStr; Var Name: NameStr;Var Ext: ExtStr);
  689. Begin
  690. Linux.FSplit(Path,Dir,Name,Ext);
  691. End;
  692. Function FExpand(Const Path: PathStr): PathStr;
  693. Begin
  694. FExpand:=Linux.FExpand(Path);
  695. End;
  696. Function FSearch(path : pathstr;dirlist : string) : pathstr;
  697. Begin
  698. FSearch:=Linux.FSearch(path,dirlist);
  699. End;
  700. Procedure GetFAttr(var f; var attr : word);
  701. Var
  702. {$IFDEF CRTLIB}
  703. Info: RtlInfoType;
  704. {$ELSE}
  705. info : stat;
  706. {$ENDIF}
  707. LinAttr : longint;
  708. Begin
  709. {$IFDEF CRTLIB}
  710. _rtl_fstat(word(f), longint(@Info));
  711. attr := info.fmode;
  712. {$ELSE}
  713. if not FStat(strpas(@textrec(f).name),info) then
  714. begin
  715. Attr:=0;
  716. DosError:=3;
  717. exit;
  718. end
  719. else
  720. LinAttr:=Info.Mode;
  721. if S_ISDIR(LinAttr) then
  722. Attr:=$10
  723. else
  724. Attr:=$20;
  725. if not Access(strpas(@textrec(f).name),W_OK) then
  726. Attr:=Attr or $1;
  727. if (not S_ISDIR(LinAttr)) and (filerec(f).name[0]='.') then
  728. Attr:=Attr or $2;
  729. {$Endif}
  730. end;
  731. Procedure getftime (var f; var time : longint);
  732. Var
  733. {$IFDEF CRTLIB}
  734. Info: RtlInfoType;
  735. {$ELSE}
  736. info : stat;
  737. {$ENDIF}
  738. DT: DateTime;
  739. Begin
  740. doserror:=0;
  741. {$IFDEF CRTLIB}
  742. _rtl_fstat(word(f), longint(@Info));
  743. UnixDateToDT(Info.FMTime, DT);
  744. {$ELSE}
  745. if not fstat(filerec(f).handle,info) then
  746. begin
  747. Time:=0;
  748. doserror:=3;
  749. exit
  750. end
  751. else
  752. UnixDateToDT(Info.mTime,DT);
  753. {$ENDIF}
  754. PackTime(DT,Time);
  755. End;
  756. {******************************************************************************
  757. --- Environment ---
  758. ******************************************************************************}
  759. Function EnvCount: Longint;
  760. var
  761. envcnt : longint;
  762. p : ppchar;
  763. Begin
  764. {$IFDEF CRTLIB}
  765. EnvCount := _rtl_envcnt;
  766. {$ELSE}
  767. envcnt:=0;
  768. p:=envp; {defined in syslinux}
  769. while (p^<>nil) do
  770. begin
  771. inc(envcnt);
  772. p:=p+4
  773. end;
  774. EnvCount := envcnt
  775. {$ENDIF}
  776. End;
  777. Function EnvStr(Index: Integer): String;
  778. Var
  779. {$IFDEF CRTLIB}
  780. Buffer: Array[0..255] of Char;
  781. {$ELSE}
  782. i : longint;
  783. p : ppchar;
  784. {$ENDIF}
  785. Begin
  786. {$IFDEF CRTLIB}
  787. Buffer[0]:=#0; {Be sure there is at least nothing}
  788. _rtl_envstr(index, buffer);
  789. EnvStr:=StrPas(Buffer);
  790. {$ELSE}
  791. p:=envp; {defined in syslinux}
  792. i:=1;
  793. while (i<Index) and (p^<>nil) do
  794. begin
  795. inc(i);
  796. p:=p+4;
  797. end;
  798. if p=nil then
  799. envstr:=''
  800. else
  801. envstr:=strpas(p^)
  802. {$ENDIF}
  803. End;
  804. Function GetEnv(EnvVar: String): String;
  805. var
  806. {$IFDEF CRTLIB}
  807. Buffer,
  808. OutStr : Array[0..255] of Char;
  809. {$ELSE}
  810. p : pchar;
  811. {$ENDIF}
  812. Begin
  813. {$IFDEF CRTLIB}
  814. Move(EnvVar[1],Buffer,Length(EnvVar));
  815. Buffer[Length(EnvVar)]:=#0;
  816. OutStr[0]:=#0;
  817. _rtl_getenv(buffer,outstr);
  818. GetEnv:=StrPas(Buffer);
  819. {$ELSE}
  820. p:=Linux.GetEnv(EnvVar);
  821. if p=nil then
  822. GetEnv:=''
  823. else
  824. GetEnv:=StrPas(p);
  825. {$ENDIF}
  826. End;
  827. {******************************************************************************
  828. --- Do Nothing Procedures/Functions ---
  829. ******************************************************************************}
  830. Procedure Intr (intno: byte; var regs: registers);
  831. Begin
  832. {! No Linux equivalent !}
  833. End;
  834. Procedure msdos(var regs : registers);
  835. Begin
  836. {! No Linux equivalent !}
  837. End;
  838. Procedure getintvec(intno : byte;var vector : pointer);
  839. Begin
  840. {! No Linux equivalent !}
  841. End;
  842. Procedure setintvec(intno : byte;vector : pointer);
  843. Begin
  844. {! No Linux equivalent !}
  845. End;
  846. Procedure SwapVectors;
  847. Begin
  848. {! No Linux equivalent !}
  849. End;
  850. Procedure keep(exitcode : word);
  851. Begin
  852. {! No Linux equivalent !}
  853. End;
  854. Procedure setftime(var f; time : longint);
  855. Begin
  856. {! No Linux equivalent !}
  857. End;
  858. Procedure setfattr (var f;attr : word);
  859. Begin
  860. {! No Linux equivalent !}
  861. End;
  862. Procedure GetCBreak(Var BreakValue: Boolean);
  863. Begin
  864. {! No Linux equivalent !}
  865. breakvalue:=true
  866. End;
  867. Procedure SetCBreak(BreakValue: Boolean);
  868. Begin
  869. {! No Linux equivalent !}
  870. End;
  871. Procedure GetVerify(Var Verify: Boolean);
  872. Begin
  873. {! No Linux equivalent !}
  874. Verify:=true;
  875. End;
  876. Procedure SetVerify(Verify: Boolean);
  877. Begin
  878. {! No Linux equivalent !}
  879. End;
  880. {******************************************************************************
  881. --- Initialization ---
  882. ******************************************************************************}
  883. End.
  884. {
  885. $Log$
  886. Revision 1.5 1998-11-05 14:24:08 peter
  887. * findfirst fix from the mailinglist
  888. Revision 1.4 1998/11/04 10:15:54 peter
  889. * don't use getmem in startup (necessary for heaptrc)
  890. Revision 1.3 1998/05/06 12:35:26 michael
  891. + Removed log from before restored version.
  892. Revision 1.2 1998/05/04 17:40:43 peter
  893. * findfirst did some strange init with searchpos
  894. Revision 1.1.1.1 1998/03/25 11:18:43 root
  895. * Restored version
  896. }