unix.pp 35 KB

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