unix.pp 35 KB

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