unix.pp 35 KB

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