2
0

unix.pp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Michael Van Canneyt,
  4. BSD parts (c) 2000 by Marco van de Voort
  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 Unix;
  13. Interface
  14. Uses BaseUnix,UnixType;
  15. {$if (defined(BSD) or defined(SUNOS)) and defined(FPC_USE_LIBC)}
  16. {$define USE_VFORK}
  17. {$endif}
  18. {$i aliasptp.inc}
  19. {$i unxconst.inc} { Get Types and Constants only exported in this unit }
  20. {** File handling **}
  21. Const
  22. P_IN = 1; // pipes (?)
  23. P_OUT = 2;
  24. LOCK_SH = 1; // flock constants ?
  25. LOCK_EX = 2;
  26. LOCK_UN = 8;
  27. LOCK_NB = 4;
  28. // The portable MAP_* and PROT_ constants are exported from unit Unix for compability.
  29. PROT_READ = baseunix.PROT_READ; { page can be read }
  30. PROT_WRITE = baseunix.PROT_WRITE; { page can be written }
  31. PROT_EXEC = baseunix.PROT_EXEC; { page can be executed }
  32. PROT_NONE = baseunix.PROT_NONE; { page can not be accessed }
  33. MAP_FAILED = baseunix.MAP_FAILED; { mmap() failed }
  34. MAP_SHARED = baseunix.MAP_SHARED; { Share changes }
  35. MAP_PRIVATE = baseunix.MAP_PRIVATE; { Changes are private }
  36. MAP_TYPE = baseunix.MAP_TYPE; { Mask for type of mapping }
  37. MAP_FIXED = baseunix.MAP_FIXED; { Interpret addr exactly }
  38. { Flags to `msync'. There is non msync() call in this unit? }
  39. MS_ASYNC = 1; { Sync memory asynchronously. }
  40. MS_SYNC = 4; { Synchronous memory sync. }
  41. MS_INVALIDATE = 2; { Invalidate the caches. }
  42. Type
  43. Tpipe = baseunix.tfildes; // compability.
  44. {** Time/Date Handling **}
  45. var
  46. tzdaylight : boolean;
  47. tzname : array[boolean] of pchar;
  48. {************ Procedure/Functions ************}
  49. {$IFNDEF DONT_READ_TIMEZONE} // allows to disable linking in and trying for platforms
  50. // it doesn't (yet) work for.
  51. { timezone support }
  52. procedure GetLocalTimezone(timer:cint;var leap_correct,leap_hit:cint);
  53. procedure GetLocalTimezone(timer:cint);
  54. procedure ReadTimezoneFile(fn:string);
  55. function GetTimezoneFile:string;
  56. {$ENDIF}
  57. {** Process Handling **}
  58. function FpExecLE (Const PathName:AnsiString;const S:Array Of AnsiString;MyEnv:ppchar):cint;
  59. function FpExecL (Const PathName:AnsiString;const S:Array Of AnsiString):cint;
  60. function FpExecLP (Const PathName:AnsiString;const S:Array Of AnsiString):cint;
  61. function FpExecLPE(Const PathName:AnsiString;const S:Array Of AnsiString;env:ppchar):cint;
  62. function FpExecV (Const PathName:AnsiString;args:ppchar):cint;
  63. function FpExecVP (Const PathName:AnsiString;args:ppchar):cint;
  64. function FpExecVPE(Const PathName:AnsiString;args,env:ppchar):cint;
  65. Function Shell (const Command:String):cint; deprecated;
  66. Function Shell (const Command:AnsiString):cint; deprecated;
  67. Function fpSystem(const Command:string):cint;
  68. Function fpSystem(const Command:AnsiString):cint;
  69. Function WaitProcess (Pid:cint):cint;
  70. Function WIFSTOPPED (Status: Integer): Boolean;
  71. Function W_EXITCODE (ReturnCode, Signal: Integer): Integer;
  72. Function W_STOPCODE (Signal: Integer): Integer;
  73. {** File Handling **}
  74. // some of these are formally listed as deprecated, but specially statfs will remain for a while, no rush.
  75. Function fsync (fd : cint) : cint; deprecated;
  76. Function fStatFS (Fd: cint;Var Info:tstatfs):cint; deprecated;
  77. Function StatFS (Path:pchar;Var Info:tstatfs):cint; deprecated;
  78. Function fpFlock (var T : text;mode : cint) : cint;
  79. Function fpFlock (var F : File;mode : cint) : cint;
  80. Function SelectText (var T:Text;TimeOut :PTimeVal):cint; deprecated;
  81. Function SelectText (var T:Text;TimeOut :cint):cint; deprecated;
  82. {** Directory Handling **}
  83. procedure SeekDir(p:pdir;loc:clong);
  84. function TellDir(p:pdir):TOff;
  85. {** Pipe/Fifo/Stream **}
  86. Function AssignPipe (var pipe_in,pipe_out:cint):cint;
  87. Function AssignPipe (var pipe_in,pipe_out:text):cint;
  88. Function AssignPipe (var pipe_in,pipe_out:file):cint;
  89. Function POpen (var F:text;const Prog:Ansistring;rw:char):cint;
  90. Function POpen (var F:file;const Prog:Ansistring;rw:char):cint;
  91. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:ansiString;const args : array of ansistring) : cint;
  92. Function AssignStream(Var StreamIn,Streamout,streamerr:text;Const Prog:ansiString;const args : array of ansistring) : cint;
  93. Function GetDomainName:String; deprecated; // because linux only.
  94. Function GetHostName:String;
  95. {** Utility functions **}
  96. Type
  97. TFSearchOption = (NoCurrentDirectory,
  98. CurrentDirectoryFirst,
  99. CurrentDirectoryLast);
  100. Function FSearch (const path:AnsiString;dirlist:Ansistring;CurrentDirStrategy:TFSearchOption):AnsiString;
  101. Function FSearch (const path:AnsiString;dirlist:AnsiString):AnsiString;
  102. procedure SigRaise (sig:integer); deprecated;
  103. {$ifdef FPC_USE_LIBC}
  104. const clib = 'c';
  105. {$i unxdeclh.inc}
  106. {$else}
  107. {$i unxsysch.inc} // calls used in system and not reexported from baseunix
  108. {$endif}
  109. {******************************************************************************
  110. Implementation
  111. ******************************************************************************}
  112. {$i unxovlh.inc}
  113. Implementation
  114. Uses
  115. UnixUtil // tzseconds
  116. {$ifndef FPC_USE_LIBC},Syscall{$endif}
  117. ;
  118. {$i unxovl.inc}
  119. {$ifndef FPC_USE_LIBC}
  120. {$i syscallh.inc}
  121. {$i unxsysc.inc}
  122. {$endif}
  123. { Get the definitions of textrec and filerec }
  124. {$i textrec.inc}
  125. {$i filerec.inc}
  126. {$i unxfunc.inc} { Platform specific implementations }
  127. Function getenv(name:string):Pchar; external name 'FPC_SYSC_FPGETENV';
  128. {******************************************************************************
  129. Process related calls
  130. ******************************************************************************}
  131. { Most calls of WaitPID do not handle the result correctly, this funktion treats errors more correctly }
  132. Function WaitProcess(Pid:cint):cint; { like WaitPid(PID,@result,0) Handling of Signal interrupts (errno=EINTR), returning the Exitcode of Process (>=0) or -Status if terminated}
  133. var
  134. r,s : cint;
  135. begin
  136. s:=$7F00;
  137. repeat
  138. r:=fpWaitPid(Pid,@s,0);
  139. if (r=-1) and (fpgeterrno=ESysEIntr) Then
  140. r:=0;
  141. until (r<>0);
  142. if (r=-1) or (r=0) then // 0 is not a valid return and should never occur (it means status invalid when using WNOHANG)
  143. WaitProcess:=-1 // return -1 to indicate an error. fpwaitpid updated it.
  144. else
  145. begin
  146. if wifexited(s) then
  147. WaitProcess:=wexitstatus(s)
  148. else if (s>0) then // Until now there is not use of the highest bit , but check this for the future
  149. WaitProcess:=-s // normal case
  150. else
  151. WaitProcess:=s; // s<0 should not occur, but wie return also a negativ value
  152. end;
  153. end;
  154. function intFpExecVEMaybeP (Const PathName:AnsiString;Args,MyEnv:ppchar;SearchPath:Boolean):cint;
  155. // does an ExecVE, but still has to handle P
  156. // execv variants call this directly, execl variants indirectly via
  157. // intfpexecl
  158. Var
  159. NewCmd : ansistring;
  160. ThePath : AnsiString;
  161. Begin
  162. If SearchPath and (pos('/',pathname)=0) Then
  163. Begin
  164. // The above could be better. (check if not escaped/quoted '/'s) ?
  165. // (Jilles says this is ok)
  166. // Stevens says only search if newcmd contains no '/'
  167. // fsearch is not ansistring clean yet.
  168. ThePath:=fpgetenv('PATH');
  169. if thepath='' then
  170. thepath:='.'; // FreeBSD uses _PATH_DEFPATH = /usr/bin:/bin
  171. // but a quick check showed that _PATH_DEFPATH
  172. // varied from OS to OS
  173. newcmd:=FSearch(pathname,thepath,NoCurrentDirectory);
  174. // FreeBSD libc keeps on trying till a file is successfully run.
  175. // Stevens says "try each path prefix"
  176. // execp puts newcmd here.
  177. args^:=pchar(newcmd);
  178. End else
  179. newcmd:=pathname;
  180. // repeat
  181. // if searchpath then args^:=pchar(commandtorun)
  182. IntFpExecVEMaybeP:=fpExecVE(newcmd,Args,MyEnv);
  183. {
  184. // Code that if exec fails due to permissions, tries to run it with sh
  185. // Should we deallocate p on fail? -> no fpexit is run no matter what
  186. //
  187. }
  188. // if intfpexecvemaybep=-1 then zoekvolgende file.
  189. // until (Goexit) or SearchExit;
  190. {
  191. If IntFpExec=-1 Then
  192. Begin
  193. Error:=fpGetErrno
  194. Case Error of
  195. ESysE2Big : Exit(-1);
  196. ESysELoop,
  197. : Exit(-1);
  198. }
  199. end;
  200. function intFpExecl (Const PathName:AnsiString;const s:array of ansistring;MyEnv:ppchar;SearchPath:Boolean):cint;
  201. { Handles the array of ansistring -> ppchar conversion.
  202. Base for the the "l" variants.
  203. }
  204. var p:ppchar;
  205. begin
  206. If PathName='' Then
  207. Begin
  208. fpsetErrno(ESysEnoEnt);
  209. Exit(-1); // Errno?
  210. End;
  211. p:=ArrayStringToPPchar(s,1);
  212. if p=NIL Then
  213. Begin
  214. GetMem(p,2*sizeof(pchar));
  215. if p=nil then
  216. begin
  217. {$ifdef xunix}
  218. fpseterrno(ESysEnoMem);
  219. {$endif}
  220. fpseterrno(ESysEnoEnt);
  221. exit(-1);
  222. end;
  223. p[1]:=nil;
  224. End;
  225. p^:=pchar(PathName);
  226. IntFPExecL:=intFpExecVEMaybeP(PathName,p,MyEnv,SearchPath);
  227. // If we come here, no attempts were executed successfully.
  228. Freemem(p);
  229. end;
  230. function FpExecLE (Const PathName:AnsiString;const S:Array Of AnsiString;MyEnv:ppchar):cint;
  231. Begin
  232. FpExecLE:=intFPExecl(PathName,s,MyEnv,false);
  233. End;
  234. function FpExecL(Const PathName:AnsiString;const S:Array Of AnsiString):cint;
  235. Begin
  236. FpExecL:=intFPExecl(PathName,S,EnvP,false);
  237. End;
  238. function FpExecLP(Const PathName:AnsiString;const S:Array Of AnsiString):cint;
  239. Begin
  240. FpExecLP:=intFPExecl(PathName,S,EnvP,True);
  241. End;
  242. function FpExecLPE(Const PathName:AnsiString;const S:Array Of AnsiString;env:ppchar):cint;
  243. Begin
  244. FpExecLPE:=intFPExecl(PathName,S,Env,True);
  245. End;
  246. function FpExecV(Const PathName:AnsiString;args:ppchar):cint;
  247. Begin
  248. fpexecV:=intFpExecVEMaybeP (PathName,args,envp,false);
  249. End;
  250. function FpExecVP(Const PathName:AnsiString;args:ppchar):cint;
  251. Begin
  252. fpexecVP:=intFpExecVEMaybeP (PathName,args,envp,true);
  253. End;
  254. function FpExecVPE(Const PathName:AnsiString;args,env:ppchar):cint;
  255. Begin
  256. fpexecVPE:=intFpExecVEMaybeP (PathName,args,env,true);
  257. End;
  258. // exect and execvP (ExecCapitalP) are not implement
  259. // Non POSIX anyway.
  260. // Exect turns on tracing for the process
  261. // execvP has the searchpath as array of ansistring ( const char *search_path)
  262. {$define FPC_USE_FPEXEC}
  263. {$if defined(FPC_USE_FPEXEC) and not defined(USE_VFORK)}
  264. {$define SHELL_USE_FPEXEC}
  265. {$endif}
  266. Function Shell(const Command:String):cint; deprecated;
  267. {
  268. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  269. The current environment is passed to the shell.
  270. It waits for the shell to exit, and returns its exit status.
  271. If the Exec call failed exit status 127 is reported.
  272. }
  273. { Changed the structure:
  274. - the previous version returns an undefinied value if fork fails
  275. - it returns the status of Waitpid instead of the Process returnvalue (see the doc to Shell)
  276. - it uses exit(127) not ExitProc (The Result in pp386: going on Compiling in 2 processes!)
  277. - ShellArgs are now released
  278. - The Old CreateShellArg gives back pointers to a local var
  279. }
  280. var
  281. {$ifndef SHELL_USE_FPEXEC}
  282. p : ppchar;
  283. {$endif}
  284. pid : cint;
  285. begin
  286. {$ifndef SHELL_USE_FPEXEC}
  287. p:=CreateShellArgv(command);
  288. {$endif}
  289. {$ifdef USE_VFORK}
  290. pid:=fpvfork;
  291. {$else USE_VFORK}
  292. pid:=fpfork;
  293. {$endif USE_VFORK}
  294. if pid=0 then // We are in the Child
  295. begin
  296. {This is the child.}
  297. {$ifndef SHELL_USE_FPEXEC}
  298. fpExecve(p^,p,envp);
  299. {$else}
  300. fpexecl('/bin/sh',['-c',Command]);
  301. {$endif}
  302. fpExit(127); // was Exit(127)
  303. end
  304. else if (pid<>-1) then // Successfull started
  305. Shell:=WaitProcess(pid)
  306. else // no success
  307. Shell:=-1; // indicate an error
  308. {$ifndef SHELL_USE_FPEXEC}
  309. FreeShellArgV(p);
  310. {$endif}
  311. end;
  312. Function Shell(const Command:AnsiString):cint;
  313. {
  314. AnsiString version of Shell
  315. }
  316. var
  317. {$ifndef SHELL_USE_FPEXEC}
  318. p : ppchar;
  319. {$endif}
  320. pid : cint;
  321. begin { Changes as above }
  322. {$ifndef SHELL_USE_FPEXEC}
  323. p:=CreateShellArgv(command);
  324. {$endif}
  325. {$ifdef USE_VFORK}
  326. pid:=fpvfork;
  327. {$else USE_VFORK}
  328. pid:=fpfork;
  329. {$endif USE_VFORK}
  330. if pid=0 then // We are in the Child
  331. begin
  332. {$ifdef SHELL_USE_FPEXEC}
  333. fpexecl('/bin/sh',['-c',Command]);
  334. {$else}
  335. fpExecve(p^,p,envp);
  336. {$endif}
  337. fpExit(127); // was exit(127)!! We must exit the Process, not the function
  338. end
  339. else if (pid<>-1) then // Successfull started
  340. Shell:=WaitProcess(pid)
  341. else // no success
  342. Shell:=-1;
  343. {$ifndef SHELL_USE_FPEXEC}
  344. FreeShellArgV(p);
  345. {$ENDIF}
  346. end;
  347. {$ifdef FPC_USE_LIBC}
  348. function xfpsystem(p:pchar):cint; cdecl; external clib name 'system';
  349. function fpsystem(const Command:string):cint;
  350. var c:array[0..255] of char;
  351. begin
  352. move(command[1],c[0],length(command));
  353. c[length(command)]:=#0;
  354. fpsystem:=xfpsystem(@c);
  355. end;
  356. Function fpSystem(const Command:AnsiString):cint;
  357. begin
  358. fpsystem:=xfpsystem(pchar(command));
  359. end;
  360. {$else}
  361. Function fpSystem(const Command:string):cint;
  362. var
  363. pid,savedpid : cint;
  364. pstat : cint;
  365. ign,intact,
  366. quitact : SigactionRec;
  367. newsigblock,
  368. oldsigblock : tsigset;
  369. begin { Changes as above }
  370. if command='' then exit(1);
  371. ign.sa_handler:=SigActionHandler(SIG_IGN);
  372. fpsigemptyset(ign.sa_mask);
  373. ign.sa_flags:=0;
  374. fpsigaction(SIGINT, @ign, @intact);
  375. fpsigaction(SIGQUIT, @ign, @quitact);
  376. fpsigemptyset(newsigblock);
  377. fpsigaddset(newsigblock,SIGCHLD);
  378. fpsigprocmask(SIG_BLOCK,newsigblock,oldsigblock);
  379. pid:=fpfork;
  380. if pid=0 then // We are in the Child
  381. begin
  382. fpsigaction(SIGINT,@intact,NIL);
  383. fpsigaction(SIGQUIT,@quitact,NIL);
  384. fpsigprocmask(SIG_SETMASK,@oldsigblock,NIL);
  385. fpexecl('/bin/sh',['-c',Command]);
  386. fpExit(127); // was exit(127)!! We must exit the Process, not the function
  387. end
  388. else if (pid<>-1) then // Successfull started
  389. begin
  390. savedpid:=pid;
  391. repeat
  392. pid:=fpwaitpid(savedpid,@pstat,0);
  393. until (pid<>-1) and (fpgeterrno()<>ESysEintr);
  394. if pid=-1 Then
  395. fpsystem:=-1
  396. else
  397. fpsystem:=pstat;
  398. end
  399. else // no success
  400. fpsystem:=-1;
  401. fpsigaction(SIGINT,@intact,NIL);
  402. fpsigaction(SIGQUIT,@quitact,NIL);
  403. fpsigprocmask(SIG_SETMASK,@oldsigblock,NIL);
  404. end;
  405. Function fpSystem(const Command:AnsiString):cint;
  406. {
  407. AnsiString version of Shell
  408. }
  409. var
  410. pid,savedpid : cint;
  411. pstat : cint;
  412. ign,intact,
  413. quitact : SigactionRec;
  414. newsigblock,
  415. oldsigblock : tsigset;
  416. begin { Changes as above }
  417. if command='' then exit(1);
  418. ign.sa_handler:=SigActionHandler(SIG_IGN);
  419. fpsigemptyset(ign.sa_mask);
  420. ign.sa_flags:=0;
  421. fpsigaction(SIGINT, @ign, @intact);
  422. fpsigaction(SIGQUIT, @ign, @quitact);
  423. fpsigemptyset(newsigblock);
  424. fpsigaddset(newsigblock,SIGCHLD);
  425. fpsigprocmask(SIG_BLOCK,newsigblock,oldsigblock);
  426. pid:=fpfork;
  427. if pid=0 then // We are in the Child
  428. begin
  429. fpsigaction(SIGINT,@intact,NIL);
  430. fpsigaction(SIGQUIT,@quitact,NIL);
  431. fpsigprocmask(SIG_SETMASK,@oldsigblock,NIL);
  432. fpexecl('/bin/sh',['-c',Command]);
  433. fpExit(127); // was exit(127)!! We must exit the Process, not the function
  434. end
  435. else if (pid<>-1) then // Successfull started
  436. begin
  437. savedpid:=pid;
  438. repeat
  439. pid:=fpwaitpid(savedpid,@pstat,0);
  440. until (pid<>-1) and (fpgeterrno()<>ESysEintr);
  441. if pid=-1 Then
  442. fpsystem:=-1
  443. else
  444. fpsystem:=pstat;
  445. end
  446. else // no success
  447. fpsystem:=-1;
  448. fpsigaction(SIGINT,@intact,NIL);
  449. fpsigaction(SIGQUIT,@quitact,NIL);
  450. fpsigprocmask(SIG_SETMASK,@oldsigblock,NIL);
  451. end;
  452. {$endif}
  453. Function WIFSTOPPED(Status: Integer): Boolean;
  454. begin
  455. WIFSTOPPED:=((Status and $FF)=$7F);
  456. end;
  457. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  458. begin
  459. W_EXITCODE:=(ReturnCode shl 8) or Signal;
  460. end;
  461. Function W_STOPCODE(Signal: Integer): Integer;
  462. begin
  463. W_STOPCODE:=(Signal shl 8) or $7F;
  464. end;
  465. {$IFNDEF DONT_READ_TIMEZONE}
  466. { Include timezone handling routines which use /usr/share/timezone info }
  467. {$i timezone.inc}
  468. {$endif}
  469. {******************************************************************************
  470. FileSystem calls
  471. ******************************************************************************}
  472. Function fpFlock (var T : text;mode : cint) : cint;
  473. begin
  474. {$ifndef beos}
  475. fpFlock:=fpFlock(TextRec(T).Handle,mode);
  476. {$endif}
  477. end;
  478. Function fpFlock (var F : File;mode : cint) :cint;
  479. begin
  480. {$ifndef beos}
  481. fpFlock:=fpFlock(FileRec(F).Handle,mode);
  482. {$endif}
  483. end;
  484. Function SelectText(var T:Text;TimeOut :PTimeval):cint;
  485. Var
  486. F:TfdSet;
  487. begin
  488. if textrec(t).mode=fmclosed then
  489. begin
  490. fpseterrno(ESysEBADF);
  491. exit(-1);
  492. end;
  493. FpFD_ZERO(f);
  494. fpFD_SET(textrec(T).handle,f);
  495. if textrec(T).mode=fminput then
  496. SelectText:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  497. else
  498. SelectText:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  499. end;
  500. Function SelectText(var T:Text;TimeOut :cint):cint;
  501. var
  502. p : PTimeVal;
  503. tv : TimeVal;
  504. begin
  505. if TimeOut=-1 then
  506. p:=nil
  507. else
  508. begin
  509. tv.tv_Sec:=Timeout div 1000;
  510. tv.tv_Usec:=(Timeout mod 1000)*1000;
  511. p:=@tv;
  512. end;
  513. SelectText:=SelectText(T,p);
  514. end;
  515. {******************************************************************************
  516. Directory
  517. ******************************************************************************}
  518. procedure SeekDir(p:pdir;loc:clong);
  519. begin
  520. if p=nil then
  521. begin
  522. fpseterrno(ESysEBADF);
  523. exit;
  524. end;
  525. {$if not(defined(bsd)) and not(defined(solaris)) and not(defined(beos)) }
  526. p^.dd_nextoff:=fplseek(p^.dd_fd,loc,seek_set);
  527. {$endif}
  528. {$if not(defined(beos))}
  529. p^.dd_size:=0;
  530. p^.dd_loc:=0;
  531. {$endif}
  532. end;
  533. function TellDir(p:pdir):TOff;
  534. begin
  535. if p=nil then
  536. begin
  537. fpseterrno(ESysEBADF);
  538. telldir:=-1;
  539. exit;
  540. end;
  541. {$ifndef beos}
  542. telldir:=fplseek(p^.dd_fd,0,seek_cur)
  543. {$endif}
  544. { We could try to use the nextoff field here, but on my 1.2.13
  545. kernel, this gives nothing... This may have to do with
  546. the readdir implementation of libc... I also didn't find any trace of
  547. the field in the kernel code itself, So I suspect it is an artifact of libc.
  548. Michael. }
  549. end;
  550. {******************************************************************************
  551. Pipes/Fifo
  552. ******************************************************************************}
  553. Procedure OpenPipe(var F:Text);
  554. begin
  555. case textrec(f).mode of
  556. fmoutput :
  557. if textrec(f).userdata[1]<>P_OUT then
  558. textrec(f).mode:=fmclosed;
  559. fminput :
  560. if textrec(f).userdata[1]<>P_IN then
  561. textrec(f).mode:=fmclosed;
  562. else
  563. textrec(f).mode:=fmclosed;
  564. end;
  565. end;
  566. Function IOPipe(var F:text):cint;
  567. begin
  568. IOPipe:=0;
  569. case textrec(f).mode of
  570. fmoutput :
  571. begin
  572. { first check if we need something to write, else we may
  573. get a SigPipe when Close() is called (PFV) }
  574. if textrec(f).bufpos>0 then
  575. IOPipe:=fpwrite(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  576. end;
  577. fminput : Begin
  578. textrec(f).bufend:=fpread(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  579. IOPipe:=textrec(f).bufend;
  580. End;
  581. end;
  582. textrec(f).bufpos:=0;
  583. end;
  584. Function FlushPipe(var F:Text):cint;
  585. begin
  586. FlushPipe:=0;
  587. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  588. FlushPipe:=IOPipe(f);
  589. textrec(f).bufpos:=0;
  590. end;
  591. Function ClosePipe(var F:text):cint;
  592. begin
  593. textrec(f).mode:=fmclosed;
  594. ClosePipe:=fpclose(textrec(f).handle);
  595. end;
  596. Function AssignPipe(var pipe_in,pipe_out:text):cint;
  597. {
  598. Sets up a pair of file variables, which act as a pipe. The first one can
  599. be read from, the second one can be written to.
  600. }
  601. var
  602. f_in,f_out : cint;
  603. begin
  604. if AssignPipe(f_in,f_out)=-1 then
  605. exit(-1);
  606. { Set up input }
  607. Assign(Pipe_in,'');
  608. Textrec(Pipe_in).Handle:=f_in;
  609. Textrec(Pipe_in).Mode:=fmInput;
  610. Textrec(Pipe_in).userdata[1]:=P_IN;
  611. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  612. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  613. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  614. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  615. { Set up output }
  616. Assign(Pipe_out,'');
  617. Textrec(Pipe_out).Handle:=f_out;
  618. Textrec(Pipe_out).Mode:=fmOutput;
  619. Textrec(Pipe_out).userdata[1]:=P_OUT;
  620. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  621. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  622. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  623. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  624. AssignPipe:=0;
  625. end;
  626. Function AssignPipe(var pipe_in,pipe_out:file):cint;
  627. {
  628. Sets up a pair of file variables, which act as a pipe. The first one can
  629. be read from, the second one can be written to.
  630. If the operation was unsuccesful,
  631. }
  632. var
  633. f_in,f_out : cint;
  634. begin
  635. if AssignPipe(f_in,f_out)=-1 then
  636. exit(-1);
  637. { Set up input }
  638. Assign(Pipe_in,'');
  639. Filerec(Pipe_in).Handle:=f_in;
  640. Filerec(Pipe_in).Mode:=fmInput;
  641. Filerec(Pipe_in).recsize:=1;
  642. Filerec(Pipe_in).userdata[1]:=P_IN;
  643. { Set up output }
  644. Assign(Pipe_out,'');
  645. Filerec(Pipe_out).Handle:=f_out;
  646. Filerec(Pipe_out).Mode:=fmoutput;
  647. Filerec(Pipe_out).recsize:=1;
  648. Filerec(Pipe_out).userdata[1]:=P_OUT;
  649. AssignPipe:=0;
  650. end;
  651. Function PCloseText(Var F:text):cint;
  652. {
  653. May not use @PClose due overloading
  654. }
  655. begin
  656. PCloseText:=PClose(f);
  657. end;
  658. Function POpen(var F:text;const Prog:Ansistring;rw:char):cint;
  659. {
  660. Starts the program in 'Prog' and makes it's input or out put the
  661. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  662. F, will be read from stdin by the program in 'Prog'. The inverse is true
  663. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  664. read from 'f'.
  665. }
  666. var
  667. pipi,
  668. pipo : text;
  669. pid : cint;
  670. pl : ^cint;
  671. {$if not defined(FPC_USE_FPEXEC) or defined(USE_VFORK)}
  672. pp : array[0..3] of pchar;
  673. temp : string[255];
  674. {$endif not FPC_USE_FPEXEC or USE_VFORK}
  675. ret : cint;
  676. begin
  677. rw:=upcase(rw);
  678. if not (rw in ['R','W']) then
  679. begin
  680. FpSetErrno(ESysEnoent);
  681. exit(-1);
  682. end;
  683. ret:=AssignPipe(pipi,pipo);
  684. if ret=-1 then
  685. exit(-1);
  686. {$ifdef USE_VFORK}
  687. pid:=fpvfork;
  688. {$else USE_VFORK}
  689. pid:=fpfork;
  690. {$endif USE_VFORK}
  691. if pid=-1 then
  692. begin
  693. close(pipi);
  694. close(pipo);
  695. exit(-1);
  696. end;
  697. if pid=0 then
  698. begin
  699. { We're in the child }
  700. if rw='W' then
  701. begin
  702. if (textrec(pipi).handle <> stdinputhandle) then
  703. begin
  704. ret:=fpdup2(pipi,input);
  705. {$ifdef USE_VFORK}
  706. fpclose(textrec(pipi).handle);
  707. {$else USE_VFORK}
  708. close(pipi);
  709. {$endif USE_VFORK}
  710. end;
  711. {$ifdef USE_VFORK}
  712. fpclose(textrec(pipo).handle);
  713. {$else USE_VFORK}
  714. close(pipo);
  715. {$endif USE_VFORK}
  716. if ret=-1 then
  717. fpexit(127);
  718. end
  719. else
  720. begin
  721. {$ifdef USE_VFORK}
  722. fpclose(textrec(pipi).handle);
  723. {$else USE_VFORK}
  724. close(pipi);
  725. {$endif USE_VFORK}
  726. if (textrec(pipo).handle <> stdoutputhandle) then
  727. begin
  728. ret:=fpdup2(pipo,output);
  729. {$ifdef USE_VFORK}
  730. fpclose(textrec(pipo).handle);
  731. {$else USE_VFORK}
  732. close(pipo);
  733. {$endif USE_VFORK}
  734. end;
  735. if ret=-1 then
  736. fpexit(127);
  737. end;
  738. {$if defined(FPC_USE_FPEXEC) and not defined(USE_VFORK)}
  739. fpexecl(pchar('/bin/sh'),['-c',Prog]);
  740. {$else}
  741. temp:='/bin/sh'#0'-c'#0;
  742. pp[0]:=@temp[1];
  743. pp[1]:=@temp[9];
  744. pp[2]:=@prog[1];
  745. pp[3]:=Nil;
  746. fpExecve('/bin/sh',@pp,envp);
  747. {$endif}
  748. fpexit(127);
  749. end
  750. else
  751. begin
  752. { We're in the parent }
  753. if rw='W' then
  754. begin
  755. close(pipi);
  756. f:=pipo;
  757. end
  758. else
  759. begin
  760. close(pipo);
  761. f:=pipi;
  762. end;
  763. textrec(f).bufptr:=@textrec(f).buffer;
  764. {Save the process ID - needed when closing }
  765. pl:=pcint(@textrec(f).userdata[2]);
  766. { avoid alignment error on sparc }
  767. move(pid,pl^,sizeof(pid));
  768. textrec(f).closefunc:=@PCloseText;
  769. end;
  770. POpen:=0;
  771. end;
  772. Function POpen(var F:file;const Prog:Ansistring;rw:char):cint;
  773. {
  774. Starts the program in 'Prog' and makes it's input or out put the
  775. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  776. F, will be read from stdin by the program in 'Prog'. The inverse is true
  777. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  778. read from 'f'.
  779. }
  780. var
  781. pipi,
  782. pipo : file;
  783. pid : cint;
  784. pl : ^cint;
  785. {$if not defined(FPC_USE_FPEXEC) or defined(USE_VFORK)}
  786. pp : array[0..3] of pchar;
  787. temp : string[255];
  788. {$endif not FPC_USE_FPEXEC or USE_VFORK}
  789. ret : cint;
  790. begin
  791. rw:=upcase(rw);
  792. if not (rw in ['R','W']) then
  793. begin
  794. FpSetErrno(ESysEnoent);
  795. exit(-1);
  796. end;
  797. ret:=AssignPipe(pipi,pipo);
  798. if ret=-1 then
  799. exit(-1);
  800. {$ifdef USE_VFORK}
  801. pid:=fpvfork;
  802. {$else USE_VFORK}
  803. pid:=fpfork;
  804. {$endif USE_VFORK}
  805. if pid=-1 then
  806. begin
  807. close(pipi);
  808. close(pipo);
  809. exit(-1);
  810. end;
  811. if pid=0 then
  812. begin
  813. { We're in the child }
  814. if rw='W' then
  815. begin
  816. if (filerec(pipi).handle <> stdinputhandle) then
  817. begin
  818. ret:=fpdup2(filerec(pipi).handle,stdinputhandle);
  819. {$ifdef USE_VFORK}
  820. fpclose(filerec(pipi).handle);
  821. {$else USE_VFORK}
  822. close(pipi);
  823. {$endif USE_VFORK}
  824. end;
  825. {$ifdef USE_VFORK}
  826. fpclose(filerec(pipo).handle);
  827. {$else USE_VFORK}
  828. close(pipo);
  829. {$endif USE_VFORK}
  830. if ret=-1 then
  831. fpexit(127);
  832. end
  833. else
  834. begin
  835. {$ifdef USE_VFORK}
  836. fpclose(filerec(pipi).handle);
  837. {$else USE_VFORK}
  838. close(pipi);
  839. {$endif USE_VFORK}
  840. if (filerec(pipo).handle <> stdoutputhandle) then
  841. begin
  842. ret:=fpdup2(filerec(pipo).handle,stdoutputhandle);
  843. {$ifdef USE_VFORK}
  844. fpclose(filerec(pipo).handle);
  845. {$else USE_VFORK}
  846. close(pipo);
  847. {$endif USE_VFORK}
  848. end;
  849. if ret=-1 then
  850. fpexit(127);
  851. end;
  852. {$if defined(FPC_USE_FPEXEC) and not defined(USE_VFORK)}
  853. fpexecl(pchar('/bin/sh'),['-c',Prog]);
  854. {$else}
  855. temp:='/bin/sh'#0'-c'#0;
  856. pp[0]:=@temp[1];
  857. pp[1]:=@temp[9];
  858. pp[2]:=@prog[1];
  859. pp[3]:=Nil;
  860. fpExecve('/bin/sh',@pp,envp);
  861. {$endif}
  862. fpexit(127);
  863. end
  864. else
  865. begin
  866. { We're in the parent }
  867. if rw='W' then
  868. begin
  869. close(pipi);
  870. f:=pipo;
  871. end
  872. else
  873. begin
  874. close(pipo);
  875. f:=pipi;
  876. end;
  877. {Save the process ID - needed when closing }
  878. pl:=pcint(@filerec(f).userdata[2]);
  879. { avoid alignment error on sparc }
  880. move(pid,pl^,sizeof(pid));
  881. end;
  882. POpen:=0;
  883. end;
  884. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:ansiString;const args : array of ansistring) : cint;
  885. {
  886. Starts the program in 'Prog' and makes its input and output the
  887. other end of two pipes, which are the stdin and stdout of a program
  888. specified in 'Prog'.
  889. streamout can be used to write to the program, streamin can be used to read
  890. the output of the program. See the following diagram :
  891. Parent Child
  892. STreamout --> Input
  893. Streamin <-- Output
  894. Return value is the process ID of the process being spawned, or -1 in case of failure.
  895. }
  896. var
  897. pipi,
  898. pipo : text;
  899. pid : cint;
  900. pl : ^cint;
  901. begin
  902. AssignStream:=-1;
  903. if AssignPipe(streamin,pipo)=-1 Then
  904. exit(-1);
  905. if AssignPipe(pipi,streamout)=-1 Then
  906. begin
  907. close(streamin);
  908. close(pipo);
  909. exit(-1);
  910. end;
  911. pid:=fpfork;
  912. if pid=-1 then
  913. begin
  914. close(pipi);
  915. close(pipo);
  916. close (streamin);
  917. close (streamout);
  918. exit;
  919. end;
  920. if pid=0 then
  921. begin
  922. { We're in the child }
  923. { Close what we don't need }
  924. close(streamout);
  925. close(streamin);
  926. if fpdup2(pipi,input)=-1 Then
  927. halt(127);
  928. close(pipi);
  929. If fpdup2(pipo,output)=-1 Then
  930. halt (127);
  931. close(pipo);
  932. fpExecl(Prog,args);
  933. halt(127);
  934. end
  935. else
  936. begin
  937. { we're in the parent}
  938. close(pipo);
  939. close(pipi);
  940. {Save the process ID - needed when closing }
  941. pl:=pcint(@textrec(StreamIn).userdata[2]);
  942. { avoid alignment error on sparc }
  943. move(pid,pl^,sizeof(pid));
  944. textrec(StreamIn).closefunc:=@PCloseText;
  945. {Save the process ID - needed when closing }
  946. pl:=pcint(@textrec(StreamOut).userdata[2]);
  947. { avoid alignment error on sparc }
  948. move(pid,pl^,sizeof(pid));
  949. textrec(StreamOut).closefunc:=@PCloseText;
  950. AssignStream:=Pid;
  951. end;
  952. end;
  953. Function AssignStream(Var StreamIn,Streamout,streamerr:text;Const Prog:ansiString;const args : array of ansistring) : cint;
  954. {
  955. Starts the program in 'prog' and makes its input, output and error output the
  956. other end of three pipes, which are the stdin, stdout and stderr of a program
  957. specified in 'prog'.
  958. StreamOut can be used to write to the program, StreamIn can be used to read
  959. the output of the program, StreamErr reads the error output of the program.
  960. See the following diagram :
  961. Parent Child
  962. StreamOut --> StdIn (input)
  963. StreamIn <-- StdOut (output)
  964. StreamErr <-- StdErr (error output)
  965. }
  966. var
  967. PipeIn, PipeOut, PipeErr: text;
  968. pid: cint;
  969. pl: ^cint;
  970. begin
  971. AssignStream := -1;
  972. // Assign pipes
  973. if AssignPipe(StreamIn, PipeOut)=-1 Then
  974. Exit(-1);
  975. If AssignPipe(StreamErr, PipeErr)=-1 Then
  976. begin
  977. Close(StreamIn);
  978. Close(PipeOut);
  979. exit(-1);
  980. end;
  981. if AssignPipe(PipeIn, StreamOut)=-1 Then
  982. begin
  983. Close(StreamIn);
  984. Close(PipeOut);
  985. Close(StreamErr);
  986. Close(PipeErr);
  987. exit(-1);
  988. end;
  989. // Fork
  990. pid := fpFork;
  991. if pid=-1 then begin
  992. Close(StreamIn);
  993. Close(PipeOut);
  994. Close(StreamErr);
  995. Close(PipeErr);
  996. Close(PipeIn);
  997. Close(StreamOut);
  998. exit(-1);
  999. end;
  1000. if pid = 0 then begin
  1001. // *** We are in the child ***
  1002. // Close what we don not need
  1003. Close(StreamOut);
  1004. Close(StreamIn);
  1005. Close(StreamErr);
  1006. // Connect pipes
  1007. if fpdup2(PipeIn, Input)=-1 Then
  1008. Halt(127);
  1009. Close(PipeIn);
  1010. if fpdup2(PipeOut, Output)=-1 Then
  1011. Halt(127);
  1012. Close(PipeOut);
  1013. if fpdup2(PipeErr, StdErr)=-1 Then
  1014. Halt(127);
  1015. Close(PipeErr);
  1016. // Execute program
  1017. fpExecl(Prog,args);
  1018. Halt(127);
  1019. end else begin
  1020. // *** We are in the parent ***
  1021. Close(PipeErr);
  1022. Close(PipeOut);
  1023. Close(PipeIn);
  1024. // Save the process ID - needed when closing
  1025. pl := pcint(@TextRec(StreamIn).userdata[2]);
  1026. { avoid alignment error on sparc }
  1027. move(pid,pl^,sizeof(pid));
  1028. TextRec(StreamIn).closefunc := @PCloseText;
  1029. // Save the process ID - needed when closing
  1030. pl := pcint(@TextRec(StreamOut).userdata[2]);
  1031. { avoid alignment error on sparc }
  1032. move(pid,pl^,sizeof(pid));
  1033. TextRec(StreamOut).closefunc := @PCloseText;
  1034. // Save the process ID - needed when closing
  1035. pl := pcint(@TextRec(StreamErr).userdata[2]);
  1036. { avoid alignment error on sparc }
  1037. move(pid,pl^,sizeof(pid));
  1038. TextRec(StreamErr).closefunc := @PCloseText;
  1039. AssignStream := pid;
  1040. end;
  1041. end;
  1042. {******************************************************************************
  1043. General information calls
  1044. ******************************************************************************}
  1045. {$ifdef Linux}
  1046. Function GetDomainName:String; { linux only!}
  1047. // domainname is a glibc extension.
  1048. {
  1049. Get machines domain name. Returns empty string if not set.
  1050. }
  1051. Var
  1052. Sysn : utsname;
  1053. begin
  1054. If fpUname(sysn)<>0 then
  1055. getdomainname:=''
  1056. else
  1057. getdomainname:=strpas(@Sysn.domain[0]);
  1058. end;
  1059. {$endif}
  1060. {$ifdef sunos}
  1061. { sunos doesn't support GetDomainName, see also
  1062. http://www.sun.com/software/solaris/programs/abi/appcert_faq.xml#q18
  1063. }
  1064. Function GetDomainName:String;
  1065. begin
  1066. GetDomainName:='';
  1067. end;
  1068. {$endif sunos}
  1069. {$ifdef BSD}
  1070. function intGetDomainName(Name:PChar; NameLen:Cint):cint;
  1071. {$ifndef FPC_USE_LIBC}
  1072. external name 'FPC_SYSC_GETDOMAINNAME';
  1073. {$else FPC_USE_LIBC}
  1074. cdecl; external clib name 'getdomainname';
  1075. {$endif FPC_USE_LIBC}
  1076. Function GetDomainName:String; { linux only!}
  1077. // domainname is a glibc extension.
  1078. {
  1079. Get machines domain name. Returns empty string if not set.
  1080. }
  1081. begin
  1082. if intGetDomainName(@getdomainname[1],255)=-1 then
  1083. getdomainname:=''
  1084. else
  1085. getdomainname[0]:=chr(strlen(@getdomainname[1]));
  1086. end;
  1087. {$endif}
  1088. Function GetHostName:String;
  1089. {
  1090. Get machines name. Returns empty string if not set.
  1091. }
  1092. Var
  1093. Sysn : utsname;
  1094. begin
  1095. If fpuname(sysn)=-1 then
  1096. gethostname:=''
  1097. else
  1098. gethostname:=strpas(@Sysn.nodename[0]);
  1099. end;
  1100. {******************************************************************************
  1101. Signal handling calls
  1102. ******************************************************************************}
  1103. procedure SigRaise(sig:integer);
  1104. begin
  1105. fpKill(fpGetPid,Sig);
  1106. end;
  1107. {******************************************************************************
  1108. Utility calls
  1109. ******************************************************************************}
  1110. Function FSearch(const path:AnsiString;dirlist:Ansistring;CurrentDirStrategy:TFSearchOption):AnsiString;
  1111. {
  1112. Searches for a file 'path' in the list of direcories in 'dirlist'.
  1113. returns an empty string if not found. Wildcards are NOT allowed.
  1114. If dirlist is empty, it is set to '.'
  1115. This function tries to make FSearch use ansistrings, and decrease
  1116. stringhandling overhead at the same time.
  1117. }
  1118. Var
  1119. mydir,NewDir : ansistring;
  1120. p1 : cint;
  1121. Info : Stat;
  1122. i,j : cint;
  1123. p : pchar;
  1124. Begin
  1125. if CurrentDirStrategy=CurrentDirectoryFirst Then
  1126. Dirlist:='.:'+dirlist; {Make sure current dir is first to be searched.}
  1127. if CurrentDirStrategy=CurrentDirectoryLast Then
  1128. Dirlist:=dirlist+':.'; {Make sure current dir is last to be searched.}
  1129. {Replace ':' and ';' with #0}
  1130. for p1:=1 to length(dirlist) do
  1131. if (dirlist[p1]=':') or (dirlist[p1]=';') then
  1132. dirlist[p1]:=#0;
  1133. {Check for WildCards}
  1134. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  1135. FSearch:='' {No wildcards allowed in these things.}
  1136. Else
  1137. Begin
  1138. p:=pchar(dirlist);
  1139. i:=length(dirlist);
  1140. j:=1;
  1141. Repeat
  1142. mydir:=ansistring(p);
  1143. if (length(mydir)>0) and (mydir[length(mydir)]<>'/') then
  1144. mydir:=mydir+'/';
  1145. NewDir:=mydir+Path;
  1146. if (FpStat(NewDir,Info)>=0) and
  1147. (not fpS_ISDIR(Info.st_Mode)) then
  1148. Begin
  1149. If Pos('./',NewDir)=1 Then
  1150. Delete(NewDir,1,2);
  1151. {DOS strips off an initial .\}
  1152. End
  1153. Else
  1154. NewDir:='';
  1155. while (j<=i) and (p^<>#0) do begin inc(j); inc(p); end;
  1156. if p^=#0 then inc(p);
  1157. Until (j>=i) or (Length(NewDir) > 0);
  1158. FSearch:=NewDir;
  1159. End;
  1160. End;
  1161. Function FSearch(const path:AnsiString;dirlist:Ansistring):AnsiString;
  1162. Begin
  1163. FSearch:=FSearch(path,dirlist,CurrentDirectoryFirst);
  1164. End;
  1165. Function fsync (fd : cint) : cint;
  1166. begin
  1167. fsync := fpFSync(fd);
  1168. end;
  1169. Function StatFS(Path:Pchar;Var Info:tstatfs):cint;
  1170. {
  1171. Get all information on a fileSystem, and return it in Info.
  1172. Path is the name of a file/directory on the fileSystem you wish to
  1173. investigate.
  1174. }
  1175. begin
  1176. StatFS:=fpStatFS(Path, @Info);;
  1177. end;
  1178. Function fStatFS(Fd:cint;Var Info:tstatfs):cint;
  1179. {
  1180. Get all information on a fileSystem, and return it in Info.
  1181. Fd is the file descriptor of a file/directory on the fileSystem
  1182. you wish to investigate.
  1183. }
  1184. begin
  1185. fStatFS:=fpfStatFS(fd, @Info);
  1186. end;
  1187. Initialization
  1188. {$IFNDEF DONT_READ_TIMEZONE}
  1189. InitLocalTime;
  1190. {$endif}
  1191. finalization
  1192. {$IFNDEF DONT_READ_TIMEZONE}
  1193. DoneLocalTime;
  1194. {$endif}
  1195. End.