dos.pp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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:=0;
  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. RtlFindRecs[ArrayPos].LastUsed:=0;
  600. end;
  601. {Main loop}
  602. SName:=Copy(f.SearchSpec,f.NamePos+1,255);
  603. Found:=False;
  604. Finished:=(f.dirptr=0);
  605. While Not Finished Do
  606. Begin
  607. {$IFDEF CRTLIB}
  608. _rtl_readdir(f.dirptr, @FBuf);
  609. FName:=StrPas(FBuf[0]);
  610. {$ELSE}
  611. p:=readdir(pdir(f.dirptr));
  612. if p=nil then
  613. FName:=''
  614. else
  615. FName:=Strpas(@p^.name);
  616. {$ENDIF}
  617. If FName='' Then
  618. Finished:=True
  619. Else
  620. Begin
  621. If FNMatch(SName,FName) Then
  622. Begin
  623. Found:=FindGetFileInfo(Copy(f.SearchSpec,1,f.NamePos)+FName,f);
  624. if Found then
  625. Finished:=true;
  626. End;
  627. End;
  628. End;
  629. {Shutdown}
  630. If Found Then
  631. Begin
  632. {$IFDEF CRTLIB}
  633. f.searchpos:=_rtl_telldir(f.dirptr);
  634. {$ELSE}
  635. f.searchpos:=telldir(pdir(f.dirptr));
  636. {$ENDIF}
  637. DosError:=0;
  638. End
  639. Else
  640. Begin
  641. FindClose(f);
  642. DosError:=18;
  643. End;
  644. End;
  645. Procedure FindFirst(Const Path: PathStr; Attr: Word; Var f: SearchRec);
  646. {
  647. opens dir and calls FindWorkProc
  648. }
  649. Begin
  650. if Path='' then
  651. begin
  652. DosError:=3;
  653. exit;
  654. end;
  655. {Create Info}
  656. f.SearchSpec := Path;
  657. f.SearchAttr := Attr;
  658. f.SearchPos:=0;
  659. f.NamePos := Length(f.SearchSpec);
  660. while (f.NamePos>0) and (f.SearchSpec[f.NamePos]<>'/') do
  661. dec(f.NamePos);
  662. {Wildcards?}
  663. if (Pos('?',Path)=0) and (Pos('*',Path)=0) then
  664. begin
  665. if FindGetFileInfo(Path,f) then
  666. DosError:=0
  667. else
  668. begin
  669. if ErrNo=Sys_ENOENT then
  670. DosError:=3
  671. else
  672. DosError:=18;
  673. end;
  674. f.DirPtr:=0;
  675. f.SearchType:=1;
  676. f.searchnum:=-1;
  677. end
  678. else
  679. {Find Entry}
  680. begin
  681. Inc(CurrSearchNum);
  682. f.SearchNum:=CurrSearchNum;
  683. f.SearchType:=0;
  684. FindNext(f);
  685. end;
  686. End;
  687. {******************************************************************************
  688. --- File ---
  689. ******************************************************************************}
  690. Procedure FSplit(Path: PathStr; Var Dir: DirStr; Var Name: NameStr;Var Ext: ExtStr);
  691. Begin
  692. Linux.FSplit(Path,Dir,Name,Ext);
  693. End;
  694. Function FExpand(Const Path: PathStr): PathStr;
  695. Begin
  696. FExpand:=Linux.FExpand(Path);
  697. End;
  698. Function FSearch(path : pathstr;dirlist : string) : pathstr;
  699. Begin
  700. FSearch:=Linux.FSearch(path,dirlist);
  701. End;
  702. Procedure GetFAttr(var f; var attr : word);
  703. Var
  704. {$IFDEF CRTLIB}
  705. Info: RtlInfoType;
  706. {$ELSE}
  707. info : stat;
  708. {$ENDIF}
  709. LinAttr : longint;
  710. Begin
  711. {$IFDEF CRTLIB}
  712. _rtl_fstat(word(f), longint(@Info));
  713. attr := info.fmode;
  714. {$ELSE}
  715. if not FStat(strpas(@textrec(f).name),info) then
  716. begin
  717. Attr:=0;
  718. DosError:=3;
  719. exit;
  720. end
  721. else
  722. LinAttr:=Info.Mode;
  723. if S_ISDIR(LinAttr) then
  724. Attr:=$10
  725. else
  726. Attr:=$20;
  727. if not Access(strpas(@textrec(f).name),W_OK) then
  728. Attr:=Attr or $1;
  729. if (not S_ISDIR(LinAttr)) and (filerec(f).name[0]='.') then
  730. Attr:=Attr or $2;
  731. {$Endif}
  732. end;
  733. Procedure getftime (var f; var time : longint);
  734. Var
  735. {$IFDEF CRTLIB}
  736. Info: RtlInfoType;
  737. {$ELSE}
  738. info : stat;
  739. {$ENDIF}
  740. DT: DateTime;
  741. Begin
  742. doserror:=0;
  743. {$IFDEF CRTLIB}
  744. _rtl_fstat(word(f), longint(@Info));
  745. UnixDateToDT(Info.FMTime, DT);
  746. {$ELSE}
  747. if not fstat(filerec(f).handle,info) then
  748. begin
  749. Time:=0;
  750. doserror:=3;
  751. exit
  752. end
  753. else
  754. UnixDateToDT(Info.mTime,DT);
  755. {$ENDIF}
  756. PackTime(DT,Time);
  757. End;
  758. {******************************************************************************
  759. --- Environment ---
  760. ******************************************************************************}
  761. Function EnvCount: Longint;
  762. var
  763. envcnt : longint;
  764. p : ppchar;
  765. Begin
  766. {$IFDEF CRTLIB}
  767. EnvCount := _rtl_envcnt;
  768. {$ELSE}
  769. envcnt:=0;
  770. p:=envp; {defined in syslinux}
  771. while (p^<>nil) do
  772. begin
  773. inc(envcnt);
  774. p:=p+4
  775. end;
  776. EnvCount := envcnt
  777. {$ENDIF}
  778. End;
  779. Function EnvStr(Index: Integer): String;
  780. Var
  781. {$IFDEF CRTLIB}
  782. Buffer: Array[0..255] of Char;
  783. {$ELSE}
  784. i : longint;
  785. p : ppchar;
  786. {$ENDIF}
  787. Begin
  788. {$IFDEF CRTLIB}
  789. Buffer[0]:=#0; {Be sure there is at least nothing}
  790. _rtl_envstr(index, buffer);
  791. EnvStr:=StrPas(Buffer);
  792. {$ELSE}
  793. p:=envp; {defined in syslinux}
  794. i:=1;
  795. while (i<Index) and (p^<>nil) do
  796. begin
  797. inc(i);
  798. p:=p+4;
  799. end;
  800. if p=nil then
  801. envstr:=''
  802. else
  803. envstr:=strpas(p^)
  804. {$ENDIF}
  805. End;
  806. Function GetEnv(EnvVar: String): String;
  807. var
  808. {$IFDEF CRTLIB}
  809. Buffer,
  810. OutStr : Array[0..255] of Char;
  811. {$ELSE}
  812. p : pchar;
  813. {$ENDIF}
  814. Begin
  815. {$IFDEF CRTLIB}
  816. Move(EnvVar[1],Buffer,Length(EnvVar));
  817. Buffer[Length(EnvVar)]:=#0;
  818. OutStr[0]:=#0;
  819. _rtl_getenv(buffer,outstr);
  820. GetEnv:=StrPas(Buffer);
  821. {$ELSE}
  822. p:=Linux.GetEnv(EnvVar);
  823. if p=nil then
  824. GetEnv:=''
  825. else
  826. GetEnv:=StrPas(p);
  827. {$ENDIF}
  828. End;
  829. {******************************************************************************
  830. --- Do Nothing Procedures/Functions ---
  831. ******************************************************************************}
  832. Procedure Intr (intno: byte; var regs: registers);
  833. Begin
  834. {! No Linux equivalent !}
  835. End;
  836. Procedure msdos(var regs : registers);
  837. Begin
  838. {! No Linux equivalent !}
  839. End;
  840. Procedure getintvec(intno : byte;var vector : pointer);
  841. Begin
  842. {! No Linux equivalent !}
  843. End;
  844. Procedure setintvec(intno : byte;vector : pointer);
  845. Begin
  846. {! No Linux equivalent !}
  847. End;
  848. Procedure SwapVectors;
  849. Begin
  850. {! No Linux equivalent !}
  851. End;
  852. Procedure keep(exitcode : word);
  853. Begin
  854. {! No Linux equivalent !}
  855. End;
  856. Procedure setftime(var f; time : longint);
  857. Begin
  858. {! No Linux equivalent !}
  859. End;
  860. Procedure setfattr (var f;attr : word);
  861. Begin
  862. {! No Linux equivalent !}
  863. End;
  864. Procedure GetCBreak(Var BreakValue: Boolean);
  865. Begin
  866. {! No Linux equivalent !}
  867. breakvalue:=true
  868. End;
  869. Procedure SetCBreak(BreakValue: Boolean);
  870. Begin
  871. {! No Linux equivalent !}
  872. End;
  873. Procedure GetVerify(Var Verify: Boolean);
  874. Begin
  875. {! No Linux equivalent !}
  876. Verify:=true;
  877. End;
  878. Procedure SetVerify(Verify: Boolean);
  879. Begin
  880. {! No Linux equivalent !}
  881. End;
  882. {******************************************************************************
  883. --- Initialization ---
  884. ******************************************************************************}
  885. End.
  886. {
  887. $Log$
  888. Revision 1.8 1999-01-28 12:54:13 michael
  889. + Fixed memory leak in findfirst/findnext
  890. Revision 1.7 1999/01/28 12:10:42 michael
  891. + Fixed findclose bug
  892. Revision 1.6 1998/11/23 12:32:31 peter
  893. * fix for findclose from the mailinglist
  894. Revision 1.5 1998/11/05 14:24:08 peter
  895. * findfirst fix from the mailinglist
  896. Revision 1.4 1998/11/04 10:15:54 peter
  897. * don't use getmem in startup (necessary for heaptrc)
  898. Revision 1.3 1998/05/06 12:35:26 michael
  899. + Removed log from before restored version.
  900. Revision 1.2 1998/05/04 17:40:43 peter
  901. * findfirst did some strange init with searchpos
  902. Revision 1.1.1.1 1998/03/25 11:18:43 root
  903. * Restored version
  904. }