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