dos.pp 23 KB

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