unix.pp 35 KB

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