unix.pp 35 KB

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