unix.pp 35 KB

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