unix.pp 36 KB

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