unix.pp 35 KB

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