unix.pp 35 KB

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