unix.pp 36 KB

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