unix.pp 35 KB

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