unix.pp 36 KB

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