unix.pp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt,
  5. BSD parts (c) 2000 by Marco van de Voort
  6. members of the Free Pascal development team.
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY;without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. Unit Unix;
  14. Interface
  15. Uses UnixUtil,BaseUnix;
  16. { Get Types and Constants }
  17. {$i sysconst.inc}
  18. {$i systypes.inc}
  19. {Get error numbers, some more signal definitions and other OS dependant
  20. types (that are not POSIX) }
  21. {i errno.inc}
  22. {$I signal.inc}
  23. {$i ostypes.inc}
  24. {********************
  25. File
  26. ********************}
  27. Const
  28. P_IN = 1; // pipes (?)
  29. P_OUT = 2;
  30. Const
  31. LOCK_SH = 1; // flock constants ?
  32. LOCK_EX = 2;
  33. LOCK_UN = 8;
  34. LOCK_NB = 4;
  35. Type
  36. Tpipe = baseunix.tfildes; // compability.
  37. pglob = ^tglob;
  38. tglob = record
  39. name : pchar;
  40. next : pglob;
  41. end;
  42. {******************************************************************************
  43. Procedure/Functions
  44. ******************************************************************************}
  45. {**************************
  46. Time/Date Handling
  47. ***************************}
  48. var
  49. tzdaylight : boolean;
  50. tzname : array[boolean] of pchar;
  51. { timezone support }
  52. procedure GetLocalTimezone(timer:cint;var leap_correct,leap_hit:cint);
  53. procedure GetLocalTimezone(timer:cint);
  54. procedure ReadTimezoneFile(fn:string);
  55. function GetTimezoneFile:string;
  56. Function GetEpochTime: cint;
  57. procedure GetTime (var hour,min,sec,msec,usec:word);
  58. procedure GetTime (var hour,min,sec,sec100:word);
  59. procedure GetTime (var hour,min,sec:word);
  60. Procedure GetDate (Var Year,Month,Day:Word);
  61. Procedure GetDateTime (Var Year,Month,Day,hour,minute,second:Word);
  62. function SetTime (Hour,Min,Sec:word) : Boolean;
  63. function SetDate (Year,Month,Day:Word) : Boolean;
  64. function SetDateTime (Year,Month,Day,hour,minute,second:Word) : Boolean;
  65. {**************************
  66. Process Handling
  67. ***************************}
  68. function CreateShellArgV (const prog:string):ppchar;
  69. function CreateShellArgV (const prog:Ansistring):ppchar;
  70. Function Execv (const path:pathstr;args:ppchar):cint;
  71. Function Execv (const path: AnsiString;args:ppchar):cint;
  72. Function Execvp (Path: Pathstr;Args:ppchar;Ep:ppchar):cint;
  73. Function Execvp (Path: AnsiString; Args:ppchar;Ep:ppchar):cint;
  74. Function Execl (const Todo: String):cint;
  75. Function Execl (const Todo: Ansistring):cint;
  76. Function Execle (Todo: String;Ep:ppchar):cint;
  77. Function Execle (Todo: AnsiString;Ep:ppchar):cint;
  78. Function Execlp (Todo: string;Ep:ppchar):cint;
  79. Function Execlp (Todo: Ansistring;Ep:ppchar):cint;
  80. Function Shell (const Command:String):cint;
  81. Function Shell (const Command:AnsiString):cint;
  82. 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}
  83. Function WIFSTOPPED (Status: Integer): Boolean;
  84. Function W_EXITCODE (ReturnCode, Signal: Integer): Integer;
  85. Function W_STOPCODE (Signal: Integer): Integer;
  86. {**************************
  87. File Handling
  88. ***************************}
  89. {$ifndef FPC_USE_LIBC} // defined using cdecl for libc.
  90. Function fsync (fd : cint) : cint;
  91. Function Flock (fd,mode : cint) : cint ;
  92. Function fStatFS (Fd: cint;Var Info:tstatfs):cint;
  93. Function StatFS (Path:pchar;Var Info:tstatfs):cint;
  94. {$endif}
  95. Function Flock (var T : text;mode : cint) : cint;
  96. Function Flock (var F : File;mode : cint) : cint;
  97. Function SelectText (var T:Text;TimeOut :PTimeVal):cint;
  98. Function SelectText (var T:Text;TimeOut :cint):cint;
  99. {**************************
  100. Directory Handling
  101. ***************************}
  102. procedure SeekDir(p:pdir;loc:clong);
  103. function TellDir(p:pdir):clong;
  104. {**************************
  105. Pipe/Fifo/Stream
  106. ***************************}
  107. Function AssignPipe (var pipe_in,pipe_out:cint):cint;
  108. Function AssignPipe (var pipe_in,pipe_out:text):cint;
  109. Function AssignPipe (var pipe_in,pipe_out:file):cint;
  110. //Function PClose (Var F:text) : cint;
  111. //Function PClose (Var F:file) : cint;
  112. Function POpen (var F:text;const Prog:String;rw:char):cint;
  113. Function POpen (var F:file;const Prog:String;rw:char):cint;
  114. function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : cint;
  115. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): cint;
  116. {$ifndef BSD}
  117. Function GetDomainName:String;
  118. Function GetHostName:String;
  119. {$endif}
  120. {**************************
  121. Memory functions
  122. ***************************}
  123. const
  124. PROT_READ = $1; { page can be read }
  125. PROT_WRITE = $2; { page can be written }
  126. PROT_EXEC = $4; { page can be executed }
  127. PROT_NONE = $0; { page can not be accessed }
  128. MAP_SHARED = $1; { Share changes }
  129. // MAP_PRIVATE = $2; { Changes are private }
  130. MAP_TYPE = $f; { Mask for type of mapping }
  131. MAP_FIXED = $10; { Interpret addr exactly }
  132. // MAP_ANONYMOUS = $20; { don't use a file }
  133. MAP_GROWSDOWN = $100; { stack-like segment }
  134. MAP_DENYWRITE = $800; { ETXTBSY }
  135. MAP_EXECUTABLE = $1000; { mark it as an executable }
  136. MAP_LOCKED = $2000; { pages are locked }
  137. MAP_NORESERVE = $4000; { don't check for reservations }
  138. {**************************
  139. Utility functions
  140. ***************************}
  141. Function FExpand (Const Path: PathStr):PathStr;
  142. Function FSearch (const path:pathstr;dirlist:string):pathstr;
  143. Function Glob (Const path:pathstr):pglob;
  144. Procedure Globfree (var p:pglob);
  145. procedure SigRaise (sig:integer);
  146. {$ifdef FPC_USE_LIBC}
  147. {$i unxdeclh.inc}
  148. {$else}
  149. {$i unxsysch.inc} // calls used in system and not reexported from baseunix
  150. {$endif}
  151. {******************************************************************************
  152. Implementation
  153. ******************************************************************************}
  154. {$i unxovlh.inc}
  155. Implementation
  156. Uses Strings{$ifndef FPC_USE_LIBC},Syscall{$endif};
  157. {$i unxovl.inc}
  158. {$ifndef FPC_USE_LIBC}
  159. {$i syscallh.inc}
  160. {$i ossysch.inc}
  161. {$i unxsysc.inc}
  162. {$endif}
  163. { Get the definitions of textrec and filerec }
  164. {$i textrec.inc}
  165. {$i filerec.inc}
  166. {$ifndef FPC_USE_LIBC}
  167. { Raw System calls are in Syscalls.inc}
  168. {$ifdef Linux} // Linux has more "oldlinux" compability.
  169. {$i sysc11.inc}
  170. {$else}
  171. {$i syscalls.inc}
  172. {$endif}
  173. {$endif}
  174. {$i unixsysc.inc} {Has platform specific libc part under ifdef, besides
  175. syscalls}
  176. Function getenv(name:string):Pchar; external name 'FPC_SYSC_FPGETENV';
  177. {******************************************************************************
  178. Process related calls
  179. ******************************************************************************}
  180. { Most calls of WaitPID do not handle the result correctly, this funktion treats errors more correctly }
  181. 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}
  182. var ret,r,s : cint;
  183. begin
  184. s:=$7F00;
  185. repeat
  186. r:=fpWaitPid(Pid,@s,0);
  187. if (r=-1) and (fpgeterrno=ESysEIntr) Then
  188. r:=0;
  189. until (r<>0);
  190. if (r=-1) or (r=0) then // 0 is not a valid return and should never occur (it means status invalid when using WNOHANG)
  191. WaitProcess:=-1 // return -1 to indicate an error. fpwaitpid updated it.
  192. else
  193. begin
  194. {$ifndef Solaris}
  195. { at least correct for Linux and Darwin (JM) }
  196. if (s and $7F)=0 then // Only this is a valid returncode
  197. {$else}
  198. if (s and $FF)=0 then // Only this is a valid returncode
  199. {$endif}
  200. WaitProcess:=s shr 8
  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 InternalCreateShellArgV(cmd:pChar; len:cint):ppchar;
  208. {
  209. Create an argv which executes a command in a shell using /bin/sh -c
  210. }
  211. const Shell = '/bin/sh'#0'-c'#0;
  212. var
  213. pp,p : ppchar;
  214. // temp : string; !! Never pass a local var back!!
  215. begin
  216. getmem(pp,4*4);
  217. p:=pp;
  218. p^:=@Shell[1];
  219. inc(p);
  220. p^:=@Shell[9];
  221. inc(p);
  222. getmem(p^,len+1);
  223. move(cmd^,p^^,len);
  224. pchar(p^)[len]:=#0;
  225. inc(p);
  226. p^:=Nil;
  227. InternalCreateShellArgV:=pp;
  228. end;
  229. function CreateShellArgV(const prog:string):ppchar;
  230. begin
  231. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog));
  232. end;
  233. function CreateShellArgV(const prog:Ansistring):ppchar;
  234. {
  235. Create an argv which executes a command in a shell using /bin/sh -c
  236. using a AnsiString;
  237. }
  238. begin
  239. CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog)); // if ppc works like delphi this also work when @prog[1] is invalid (len=0)
  240. end;
  241. procedure FreeShellArgV(p:ppchar);
  242. begin
  243. if (p<>nil) then begin
  244. freemem(p[2]);
  245. freemem(p);
  246. end;
  247. end;
  248. Function Execv(const path: AnsiString;args:ppchar):cint;
  249. {
  250. Overloaded ansistring version.
  251. }
  252. begin
  253. Execv:=fpExecVe(Path,Args,envp);
  254. end;
  255. Function Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar):cint;
  256. {
  257. Overloaded ansistring version
  258. }
  259. var
  260. thepath : Ansistring;
  261. begin
  262. if path[1]<>'/' then
  263. begin
  264. Thepath:=strpas(fpgetenv('PATH'));
  265. if thepath='' then
  266. thepath:='.';
  267. Path:=FSearch(path,thepath)
  268. end
  269. else
  270. Path:='';
  271. if Path='' then
  272. Begin
  273. fpsetErrno(ESysEnoEnt);
  274. Exit(-1);
  275. end
  276. else
  277. Execvp:=fpExecve(Path,args,ep);
  278. end;
  279. Function Execv(const path:pathstr;args:ppchar):cint;
  280. {
  281. Replaces the current program by the program specified in path,
  282. arguments in args are passed to Execve.
  283. the current environment is passed on.
  284. }
  285. begin
  286. Execv:=fpExecve(path,args,envp);
  287. end;
  288. Function Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar):cint;
  289. {
  290. This does the same as Execve, only it searches the PATH environment
  291. for the place of the Executable, except when Path starts with a slash.
  292. if the PATH environment variable is unavailable, the path is set to '.'
  293. }
  294. var
  295. thepath : string;
  296. begin
  297. if path[1]<>'/' then
  298. begin
  299. Thepath:=strpas(fpgetenv('PATH'));
  300. if thepath='' then
  301. thepath:='.';
  302. Path:=FSearch(path,thepath)
  303. end
  304. else
  305. Path:='';
  306. if Path='' then
  307. Begin
  308. fpsetErrno(ESysEnoEnt);
  309. Exit(-1);
  310. end
  311. else
  312. execvp:=fpExecve(Path,args,ep);
  313. end;
  314. Function Execle(Todo:string;Ep:ppchar):cint;
  315. {
  316. This procedure takes the string 'Todo', parses it for command and
  317. command options, and Executes the command with the given options.
  318. The string 'Todo' shoud be of the form 'command options', options
  319. separated by commas.
  320. the PATH environment is not searched for 'command'.
  321. The specified environment(in 'ep') is passed on to command
  322. }
  323. var
  324. p : ppchar;
  325. begin
  326. p:=StringToPPChar(ToDo);
  327. if (p=nil) or (p^=nil) then
  328. Begin
  329. fpsetErrno(ESysEnoEnt);
  330. Exit(-1);
  331. end
  332. else
  333. execle:=fpExecVE(p^,p,EP);
  334. end;
  335. function Execle(Todo:AnsiString;Ep:ppchar):cint;
  336. {
  337. This procedure takes the string 'Todo', parses it for command and
  338. command options, and Executes the command with the given options.
  339. The string 'Todo' shoud be of the form 'command options', options
  340. separated by commas.
  341. the PATH environment is not searched for 'command'.
  342. The specified environment(in 'ep') is passed on to command
  343. }
  344. var
  345. p : ppchar;
  346. begin
  347. p:=StringToPPChar(ToDo);
  348. if (p=nil) or (p^=nil) then
  349. Begin
  350. fpsetErrno(ESysEnoEnt);
  351. Exit(-1);
  352. end;
  353. ExecLe:=fpExecVE(p^,p,EP);
  354. end;
  355. Function Execl(const Todo:string):cint;
  356. {
  357. This procedure takes the string 'Todo', parses it for command and
  358. command options, and Executes the command with the given options.
  359. The string 'Todo' shoud be of the form 'command options', options
  360. separated by commas.
  361. the PATH environment is not searched for 'command'.
  362. The current environment is passed on to command
  363. }
  364. begin
  365. Execl:=ExecLE(ToDo,EnvP);
  366. end;
  367. Function Execlp(Todo:string;Ep:ppchar):cint;
  368. {
  369. This procedure takes the string 'Todo', parses it for command and
  370. command options, and Executes the command with the given options.
  371. The string 'Todo' shoud be of the form 'command options', options
  372. separated by commas.
  373. the PATH environment is searched for 'command'.
  374. The specified environment (in 'ep') is passed on to command
  375. }
  376. var
  377. p : ppchar;
  378. begin
  379. p:=StringToPPchar(todo);
  380. if (p=nil) or (p^=nil) then
  381. Begin
  382. fpsetErrno(ESysEnoEnt);
  383. Exit(-1);
  384. end;
  385. Execlp:=ExecVP(StrPas(p^),p,EP);
  386. end;
  387. Function Execlp(Todo: Ansistring;Ep:ppchar):cint;
  388. {
  389. Overloaded ansistring version.
  390. }
  391. var
  392. p : ppchar;
  393. begin
  394. p:=StringToPPchar(todo);
  395. if (p=nil) or (p^=nil) then
  396. Begin
  397. fpsetErrno(ESysEnoEnt);
  398. Exit(-1);
  399. end;
  400. execlp:=ExecVP(StrPas(p^),p,EP);
  401. end;
  402. Function Shell(const Command:String):cint;
  403. {
  404. Executes the shell, and passes it the string Command. (Through /bin/sh -c)
  405. The current environment is passed to the shell.
  406. It waits for the shell to exit, and returns its exit status.
  407. If the Exec call failed exit status 127 is reported.
  408. }
  409. { Changed the structure:
  410. - the previous version returns an undefinied value if fork fails
  411. - it returns the status of Waitpid instead of the Process returnvalue (see the doc to Shell)
  412. - it uses exit(127) not ExitProc (The Result in pp386: going on Compiling in 2 processes!)
  413. - ShellArgs are now released
  414. - The Old CreateShellArg gives back pointers to a local var
  415. }
  416. var
  417. p : ppchar;
  418. pid : cint;
  419. begin
  420. p:=CreateShellArgv(command);
  421. pid:=fpfork;
  422. if pid=0 then // We are in the Child
  423. begin
  424. {This is the child.}
  425. fpExecve(p^,p,envp);
  426. fpExit(127); // was Exit(127)
  427. end
  428. else if (pid<>-1) then // Successfull started
  429. Shell:=WaitProcess(pid)
  430. else // no success
  431. Shell:=-1; // indicate an error
  432. FreeShellArgV(p);
  433. end;
  434. Function Shell(const Command:AnsiString):cint;
  435. {
  436. AnsiString version of Shell
  437. }
  438. var
  439. p : ppchar;
  440. pid : cint;
  441. begin { Changes as above }
  442. p:=CreateShellArgv(command);
  443. pid:=fpfork;
  444. if pid=0 then // We are in the Child
  445. begin
  446. fpExecve(p^,p,envp);
  447. fpExit(127); // was exit(127)!! We must exit the Process, not the function
  448. end
  449. else if (pid<>-1) then // Successfull started
  450. Shell:=WaitProcess(pid)
  451. else // no success
  452. Shell:=-1;
  453. FreeShellArgV(p);
  454. end;
  455. Function WIFSTOPPED(Status: Integer): Boolean;
  456. begin
  457. WIFSTOPPED:=((Status and $FF)=$7F);
  458. end;
  459. Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
  460. begin
  461. W_EXITCODE:=(ReturnCode shl 8) or Signal;
  462. end;
  463. Function W_STOPCODE(Signal: Integer): Integer;
  464. begin
  465. W_STOPCODE:=(Signal shl 8) or $7F;
  466. end;
  467. {******************************************************************************
  468. Date and Time related calls
  469. ******************************************************************************}
  470. Function GetEpochTime: cint;
  471. {
  472. Get the number of seconds since 00:00, January 1 1970, GMT
  473. the time NOT corrected any way
  474. }
  475. begin
  476. GetEpochTime:=fptime;
  477. end;
  478. procedure GetTime(var hour,min,sec,msec,usec:word);
  479. {
  480. Gets the current time, adjusted to local time
  481. }
  482. var
  483. year,day,month:Word;
  484. tz:timeval;
  485. begin
  486. fpgettimeofday(@tz,nil);
  487. EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
  488. msec:=tz.tv_usec div 1000;
  489. usec:=tz.tv_usec mod 1000;
  490. end;
  491. procedure GetTime(var hour,min,sec,sec100:word);
  492. {
  493. Gets the current time, adjusted to local time
  494. }
  495. var
  496. usec : word;
  497. begin
  498. gettime(hour,min,sec,sec100,usec);
  499. sec100:=sec100 div 10;
  500. end;
  501. Procedure GetTime(Var Hour,Min,Sec:Word);
  502. {
  503. Gets the current time, adjusted to local time
  504. }
  505. var
  506. msec,usec : Word;
  507. Begin
  508. gettime(hour,min,sec,msec,usec);
  509. End;
  510. Procedure GetDate(Var Year,Month,Day:Word);
  511. {
  512. Gets the current date, adjusted to local time
  513. }
  514. var
  515. hour,minute,second : word;
  516. Begin
  517. EpochToLocal(fptime,year,month,day,hour,minute,second);
  518. End;
  519. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  520. {
  521. Gets the current date, adjusted to local time
  522. }
  523. Begin
  524. EpochToLocal(fptime,year,month,day,hour,minute,second);
  525. End;
  526. {$ifndef BSD} // this can be done nicer, but I still have
  527. // to think about what to do with this func.
  528. {$ifdef linux}
  529. {$ifdef FPC_USE_LIBC}
  530. function intstime (t:ptime_t):cint; external name 'stime';
  531. {$endif}
  532. Function stime (t : cint) : boolean;
  533. begin
  534. {$ifdef FPC_USE_LIBC}
  535. stime:=intstime(@t)=0;
  536. {$else}
  537. stime:=do_SysCall(Syscall_nr_stime,cint(@t))=0;
  538. {$endif}
  539. end;
  540. {$endif}
  541. {$endif}
  542. {$ifdef BSD}
  543. Function stime (t : cint) : Boolean;
  544. begin
  545. end;
  546. {$endif}
  547. Function SetTime(Hour,Min,Sec:word) : boolean;
  548. var
  549. Year, Month, Day : Word;
  550. begin
  551. GetDate (Year, Month, Day);
  552. SetTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Min, Sec ) );
  553. end;
  554. Function SetDate(Year,Month,Day:Word) : boolean;
  555. var
  556. Hour, Minute, Second, Sec100 : Word;
  557. begin
  558. GetTime ( Hour, Minute, Second, Sec100 );
  559. SetDate:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  560. end;
  561. Function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
  562. begin
  563. SetDateTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
  564. end;
  565. { Include timezone handling routines which use /usr/share/timezone info }
  566. {$i timezone.inc}
  567. {******************************************************************************
  568. FileSystem calls
  569. ******************************************************************************}
  570. Function Execl(const Todo:Ansistring):cint;
  571. {
  572. Overloaded AnsiString Version of ExecL.
  573. }
  574. begin
  575. Execl:=ExecLE(ToDo,EnvP);
  576. end;
  577. Function Flock (var T : text;mode : cint) : cint;
  578. begin
  579. Flock:=Flock(TextRec(T).Handle,mode);
  580. end;
  581. Function Flock (var F : File;mode : cint) :cint;
  582. begin
  583. Flock:=Flock(FileRec(F).Handle,mode);
  584. end;
  585. Function SelectText(var T:Text;TimeOut :PTimeval):cint;
  586. Var
  587. F:TfdSet;
  588. begin
  589. if textrec(t).mode=fmclosed then
  590. begin
  591. fpseterrno(ESysEBADF);
  592. exit(-1);
  593. end;
  594. FpFD_ZERO(f);
  595. fpFD_SET(textrec(T).handle,f);
  596. if textrec(T).mode=fminput then
  597. SelectText:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
  598. else
  599. SelectText:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
  600. end;
  601. Function SelectText(var T:Text;TimeOut :cint):cint;
  602. var
  603. p : PTimeVal;
  604. tv : TimeVal;
  605. begin
  606. if TimeOut=-1 then
  607. p:=nil
  608. else
  609. begin
  610. tv.tv_Sec:=Timeout div 1000;
  611. tv.tv_Usec:=(Timeout mod 1000)*1000;
  612. p:=@tv;
  613. end;
  614. SelectText:=SelectText(T,p);
  615. end;
  616. {******************************************************************************
  617. Directory
  618. ******************************************************************************}
  619. procedure SeekDir(p:pdir;loc:clong);
  620. begin
  621. if p=nil then
  622. begin
  623. fpseterrno(ESysEBADF);
  624. exit;
  625. end;
  626. {$ifndef bsd}
  627. p^.dd_nextoff:=fplseek(p^.dd_fd,loc,seek_set);
  628. {$endif}
  629. p^.dd_size:=0;
  630. p^.dd_loc:=0;
  631. end;
  632. function TellDir(p:pdir):clong;
  633. begin
  634. if p=nil then
  635. begin
  636. fpseterrno(ESysEBADF);
  637. telldir:=-1;
  638. exit;
  639. end;
  640. telldir:=fplseek(p^.dd_fd,0,seek_cur)
  641. { We could try to use the nextoff field here, but on my 1.2.13
  642. kernel, this gives nothing... This may have to do with
  643. the readdir implementation of libc... I also didn't find any trace of
  644. the field in the kernel code itself, So I suspect it is an artifact of libc.
  645. Michael. }
  646. end;
  647. {******************************************************************************
  648. Pipes/Fifo
  649. ******************************************************************************}
  650. Procedure OpenPipe(var F:Text);
  651. begin
  652. case textrec(f).mode of
  653. fmoutput :
  654. if textrec(f).userdata[1]<>P_OUT then
  655. textrec(f).mode:=fmclosed;
  656. fminput :
  657. if textrec(f).userdata[1]<>P_IN then
  658. textrec(f).mode:=fmclosed;
  659. else
  660. textrec(f).mode:=fmclosed;
  661. end;
  662. end;
  663. Function IOPipe(var F:text):cint;
  664. begin
  665. IOPipe:=0;
  666. case textrec(f).mode of
  667. fmoutput :
  668. begin
  669. { first check if we need something to write, else we may
  670. get a SigPipe when Close() is called (PFV) }
  671. if textrec(f).bufpos>0 then
  672. IOPipe:=fpwrite(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
  673. end;
  674. fminput : Begin
  675. textrec(f).bufend:=fpread(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
  676. IOPipe:=textrec(f).bufend;
  677. End;
  678. end;
  679. textrec(f).bufpos:=0;
  680. end;
  681. Function FlushPipe(var F:Text):cint;
  682. begin
  683. FlushPipe:=0;
  684. if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
  685. FlushPipe:=IOPipe(f);
  686. textrec(f).bufpos:=0;
  687. end;
  688. Function ClosePipe(var F:text):cint;
  689. begin
  690. textrec(f).mode:=fmclosed;
  691. ClosePipe:=fpclose(textrec(f).handle);
  692. end;
  693. Function AssignPipe(var pipe_in,pipe_out:text):cint;
  694. {
  695. Sets up a pair of file variables, which act as a pipe. The first one can
  696. be read from, the second one can be written to.
  697. }
  698. var
  699. f_in,f_out : cint;
  700. begin
  701. if AssignPipe(f_in,f_out)=-1 then
  702. exit(-1);
  703. { Set up input }
  704. Assign(Pipe_in,'');
  705. Textrec(Pipe_in).Handle:=f_in;
  706. Textrec(Pipe_in).Mode:=fmInput;
  707. Textrec(Pipe_in).userdata[1]:=P_IN;
  708. TextRec(Pipe_in).OpenFunc:=@OpenPipe;
  709. TextRec(Pipe_in).InOutFunc:=@IOPipe;
  710. TextRec(Pipe_in).FlushFunc:=@FlushPipe;
  711. TextRec(Pipe_in).CloseFunc:=@ClosePipe;
  712. { Set up output }
  713. Assign(Pipe_out,'');
  714. Textrec(Pipe_out).Handle:=f_out;
  715. Textrec(Pipe_out).Mode:=fmOutput;
  716. Textrec(Pipe_out).userdata[1]:=P_OUT;
  717. TextRec(Pipe_out).OpenFunc:=@OpenPipe;
  718. TextRec(Pipe_out).InOutFunc:=@IOPipe;
  719. TextRec(Pipe_out).FlushFunc:=@FlushPipe;
  720. TextRec(Pipe_out).CloseFunc:=@ClosePipe;
  721. AssignPipe:=0;
  722. end;
  723. Function AssignPipe(var pipe_in,pipe_out:file):cint;
  724. {
  725. Sets up a pair of file variables, which act as a pipe. The first one can
  726. be read from, the second one can be written to.
  727. If the operation was unsuccesful,
  728. }
  729. var
  730. f_in,f_out : cint;
  731. begin
  732. if AssignPipe(f_in,f_out)=-1 then
  733. exit(-1);
  734. { Set up input }
  735. Assign(Pipe_in,'');
  736. Filerec(Pipe_in).Handle:=f_in;
  737. Filerec(Pipe_in).Mode:=fmInput;
  738. Filerec(Pipe_in).recsize:=1;
  739. Filerec(Pipe_in).userdata[1]:=P_IN;
  740. { Set up output }
  741. Assign(Pipe_out,'');
  742. Filerec(Pipe_out).Handle:=f_out;
  743. Filerec(Pipe_out).Mode:=fmoutput;
  744. Filerec(Pipe_out).recsize:=1;
  745. Filerec(Pipe_out).userdata[1]:=P_OUT;
  746. AssignPipe:=0;
  747. end;
  748. Function PCloseText(Var F:text):cint;
  749. {
  750. May not use @PClose due overloading
  751. }
  752. begin
  753. PCloseText:=PClose(f);
  754. end;
  755. function POpen(var F:text;const Prog:String;rw:char):cint;
  756. {
  757. Starts the program in 'Prog' and makes it's input or out put the
  758. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  759. F, will be read from stdin by the program in 'Prog'. The inverse is true
  760. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  761. read from 'f'.
  762. }
  763. var
  764. pipi,
  765. pipo : text;
  766. pid : pid_t;
  767. pl : ^cint;
  768. pp : ppchar;
  769. ret : cint;
  770. begin
  771. rw:=upcase(rw);
  772. if not (rw in ['R','W']) then
  773. begin
  774. FpSetErrno(ESysEnoent);
  775. exit(-1);
  776. end;
  777. if AssignPipe(pipi,pipo)=-1 Then
  778. Exit(-1);
  779. pid:=fpfork;
  780. if pid=-1 then
  781. begin
  782. close(pipi);
  783. close(pipo);
  784. exit(-1);
  785. end;
  786. if pid=0 then
  787. begin
  788. { We're in the child }
  789. if rw='W' then
  790. begin
  791. close(pipo);
  792. ret:=fpdup2(pipi,input);
  793. close(pipi);
  794. if ret=-1 then
  795. halt(127);
  796. end
  797. else
  798. begin
  799. close(pipi);
  800. ret:=fpdup2(pipo,output);
  801. close(pipo);
  802. if ret=-1 then
  803. halt(127);
  804. end;
  805. pp:=createshellargv(prog);
  806. fpExecve(pp^,pp,envp);
  807. halt(127);
  808. end
  809. else
  810. begin
  811. { We're in the parent }
  812. if rw='W' then
  813. begin
  814. close(pipi);
  815. f:=pipo;
  816. textrec(f).bufptr:=@textrec(f).buffer;
  817. end
  818. else
  819. begin
  820. close(pipo);
  821. f:=pipi;
  822. textrec(f).bufptr:=@textrec(f).buffer;
  823. end;
  824. {Save the process ID - needed when closing }
  825. pl:=@(textrec(f).userdata[2]);
  826. pl^:=pid;
  827. textrec(f).closefunc:=@PCloseText;
  828. end;
  829. ret:=0;
  830. end;
  831. Function POpen(var F:file;const Prog:String;rw:char):cint;
  832. {
  833. Starts the program in 'Prog' and makes it's input or out put the
  834. other end of a pipe. If rw is 'w' or 'W', then whatever is written to
  835. F, will be read from stdin by the program in 'Prog'. The inverse is true
  836. for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
  837. read from 'f'.
  838. }
  839. var
  840. pipi,
  841. pipo : file;
  842. pid : cint;
  843. pl : ^cint;
  844. p,pp : ppchar;
  845. temp : string[255];
  846. ret : cint;
  847. begin
  848. rw:=upcase(rw);
  849. if not (rw in ['R','W']) then
  850. begin
  851. FpSetErrno(ESysEnoent);
  852. exit(-1);
  853. end;
  854. ret:=AssignPipe(pipi,pipo);
  855. if ret=-1 then
  856. exit(-1);
  857. pid:=fpfork;
  858. if pid=-1 then
  859. begin
  860. close(pipi);
  861. close(pipo);
  862. exit(-1);
  863. end;
  864. if pid=0 then
  865. begin
  866. { We're in the child }
  867. if rw='W' then
  868. begin
  869. close(pipo);
  870. ret:=fpdup2(filerec(pipi).handle,stdinputhandle);
  871. close(pipi);
  872. if ret=-1 then
  873. halt(127);
  874. end
  875. else
  876. begin
  877. close(pipi);
  878. ret:=fpdup2(filerec(pipo).handle,stdoutputhandle);
  879. close(pipo);
  880. if ret=1 then
  881. halt(127);
  882. end;
  883. getmem(pp,sizeof(pchar)*4);
  884. temp:='/bin/sh'#0'-c'#0+prog+#0;
  885. p:=pp;
  886. p^:=@temp[1];
  887. inc(p);
  888. p^:=@temp[9];
  889. inc(p);
  890. p^:=@temp[12];
  891. inc(p);
  892. p^:=Nil;
  893. fpExecve(ansistring('/bin/sh'),pp,envp);
  894. halt(127);
  895. end
  896. else
  897. begin
  898. { We're in the parent }
  899. if rw='W' then
  900. begin
  901. close(pipi);
  902. f:=pipo;
  903. end
  904. else
  905. begin
  906. close(pipo);
  907. f:=pipi;
  908. end;
  909. {Save the process ID - needed when closing }
  910. pl:=@(filerec(f).userdata[2]);
  911. pl^:=pid;
  912. end;
  913. POpen:=0;
  914. end;
  915. Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : cint;
  916. {
  917. Starts the program in 'Prog' and makes its input and output the
  918. other end of two pipes, which are the stdin and stdout of a program
  919. specified in 'Prog'.
  920. streamout can be used to write to the program, streamin can be used to read
  921. the output of the program. See the following diagram :
  922. Parent Child
  923. STreamout --> Input
  924. Streamin <-- Output
  925. Return value is the process ID of the process being spawned, or -1 in case of failure.
  926. }
  927. var
  928. pipi,
  929. pipo : text;
  930. pid : cint;
  931. pl : ^cint;
  932. begin
  933. AssignStream:=-1;
  934. if AssignPipe(streamin,pipo)=-1 Then
  935. exit(-1);
  936. if AssignPipe(pipi,streamout)=-1 Then // shouldn't this close streamin and pipo?
  937. exit(-1);
  938. pid:=fpfork;
  939. if pid=-1 then
  940. begin
  941. close(pipi);
  942. close(pipo);
  943. close (streamin);
  944. close (streamout);
  945. exit;
  946. end;
  947. if pid=0 then
  948. begin
  949. { We're in the child }
  950. { Close what we don't need }
  951. close(streamout);
  952. close(streamin);
  953. if fpdup2(pipi,input)=-1 Then
  954. halt(127);
  955. close(pipi);
  956. If fpdup2(pipo,output)=-1 Then
  957. halt (127);
  958. close(pipo);
  959. Execl(Prog);
  960. halt(127);
  961. end
  962. else
  963. begin
  964. { we're in the parent}
  965. close(pipo);
  966. close(pipi);
  967. {Save the process ID - needed when closing }
  968. pl:=@(textrec(StreamIn).userdata[2]);
  969. pl^:=pid;
  970. textrec(StreamIn).closefunc:=@PCloseText;
  971. {Save the process ID - needed when closing }
  972. pl:=@(textrec(StreamOut).userdata[2]);
  973. pl^:=pid;
  974. textrec(StreamOut).closefunc:=@PCloseText;
  975. AssignStream:=Pid;
  976. end;
  977. end;
  978. function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String):cint;
  979. {
  980. Starts the program in 'prog' and makes its input, output and error output the
  981. other end of three pipes, which are the stdin, stdout and stderr of a program
  982. specified in 'prog'.
  983. StreamOut can be used to write to the program, StreamIn can be used to read
  984. the output of the program, StreamErr reads the error output of the program.
  985. See the following diagram :
  986. Parent Child
  987. StreamOut --> StdIn (input)
  988. StreamIn <-- StdOut (output)
  989. StreamErr <-- StdErr (error output)
  990. }
  991. var
  992. PipeIn, PipeOut, PipeErr: text;
  993. pid: cint;
  994. pl: ^cint;
  995. begin
  996. AssignStream := -1;
  997. // Assign pipes
  998. if AssignPipe(StreamIn, PipeOut)=-1 Then
  999. Exit(-1);
  1000. If AssignPipe(StreamErr, PipeErr)=-1 Then
  1001. begin
  1002. Close(StreamIn);
  1003. Close(PipeOut);
  1004. exit(-1);
  1005. end;
  1006. if AssignPipe(PipeIn, StreamOut)=-1 Then
  1007. begin
  1008. Close(StreamIn);
  1009. Close(PipeOut);
  1010. Close(StreamErr);
  1011. Close(PipeErr);
  1012. exit(-1);
  1013. end;
  1014. // Fork
  1015. pid := fpFork;
  1016. if pid=-1 then begin
  1017. Close(StreamIn);
  1018. Close(PipeOut);
  1019. Close(StreamErr);
  1020. Close(PipeErr);
  1021. Close(PipeIn);
  1022. Close(StreamOut);
  1023. exit(-1);
  1024. end;
  1025. if pid = 0 then begin
  1026. // *** We are in the child ***
  1027. // Close what we don not need
  1028. Close(StreamOut);
  1029. Close(StreamIn);
  1030. Close(StreamErr);
  1031. // Connect pipes
  1032. if fpdup2(PipeIn, Input)=-1 Then
  1033. Halt(127);
  1034. Close(PipeIn);
  1035. if fpdup2(PipeOut, Output)=-1 Then
  1036. Halt(127);
  1037. Close(PipeOut);
  1038. if fpdup2(PipeErr, StdErr)=-1 Then
  1039. Halt(127);
  1040. Close(PipeErr);
  1041. // Execute program
  1042. Execl(Prog);
  1043. Halt(127);
  1044. end else begin
  1045. // *** We are in the parent ***
  1046. Close(PipeErr);
  1047. Close(PipeOut);
  1048. Close(PipeIn);
  1049. // Save the process ID - needed when closing
  1050. pl := @(TextRec(StreamIn).userdata[2]);
  1051. pl^ := pid;
  1052. TextRec(StreamIn).closefunc := @PCloseText;
  1053. // Save the process ID - needed when closing
  1054. pl := @(TextRec(StreamOut).userdata[2]);
  1055. pl^ := pid;
  1056. TextRec(StreamOut).closefunc := @PCloseText;
  1057. // Save the process ID - needed when closing
  1058. pl := @(TextRec(StreamErr).userdata[2]);
  1059. pl^ := pid;
  1060. TextRec(StreamErr).closefunc := @PCloseText;
  1061. AssignStream := pid;
  1062. end;
  1063. end;
  1064. {******************************************************************************
  1065. General information calls
  1066. ******************************************************************************}
  1067. {$ifndef BSD}
  1068. Function GetDomainName:String; { linux only!}
  1069. // domainname is a glibc extension.
  1070. {
  1071. Get machines domain name. Returns empty string if not set.
  1072. }
  1073. Var
  1074. Sysn : utsname;
  1075. begin
  1076. If fpUname(sysn)<>0 then
  1077. getdomainname:=''
  1078. else
  1079. getdomainname:=strpas(@Sysn.domain[0]);
  1080. end;
  1081. {$endif}
  1082. Function GetHostName:String;
  1083. {
  1084. Get machines name. Returns empty string if not set.
  1085. }
  1086. Var
  1087. Sysn : utsname;
  1088. begin
  1089. If fpuname(sysn)=-1 then
  1090. gethostname:=''
  1091. else
  1092. gethostname:=strpas(@Sysn.nodename[0]);
  1093. end;
  1094. {******************************************************************************
  1095. Signal handling calls
  1096. ******************************************************************************}
  1097. procedure SigRaise(sig:integer);
  1098. begin
  1099. fpKill(fpGetPid,Sig);
  1100. end;
  1101. {******************************************************************************
  1102. Utility calls
  1103. ******************************************************************************}
  1104. {
  1105. Function Octal(l:cint):cint;
  1106. {
  1107. Convert an octal specified number to decimal;
  1108. }
  1109. var
  1110. octnr,
  1111. oct : cint;
  1112. begin
  1113. octnr:=0;
  1114. oct:=0;
  1115. while (l>0) do
  1116. begin
  1117. oct:=oct or ((l mod 10) shl octnr);
  1118. l:=l div 10;
  1119. inc(octnr,3);
  1120. end;
  1121. Octal:=oct;
  1122. end;
  1123. }
  1124. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  1125. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  1126. {$I fexpand.inc}
  1127. {$UNDEF FPC_FEXPAND_GETENVPCHAR}
  1128. {$UNDEF FPC_FEXPAND_TILDE}
  1129. Function FSearch(const path:pathstr;dirlist:string):pathstr;
  1130. {
  1131. Searches for a file 'path' in the list of direcories in 'dirlist'.
  1132. returns an empty string if not found. Wildcards are NOT allowed.
  1133. If dirlist is empty, it is set to '.'
  1134. }
  1135. Var
  1136. NewDir : PathStr;
  1137. p1 : cint;
  1138. Info : Stat;
  1139. Begin
  1140. {Replace ':' with ';'}
  1141. for p1:=1to length(dirlist) do
  1142. if dirlist[p1]=':' then
  1143. dirlist[p1]:=';';
  1144. {Check for WildCards}
  1145. If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
  1146. FSearch:='' {No wildcards allowed in these things.}
  1147. Else
  1148. Begin
  1149. Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
  1150. Repeat
  1151. p1:=Pos(';',DirList);
  1152. If p1=0 Then
  1153. p1:=255;
  1154. NewDir:=Copy(DirList,1,P1 - 1);
  1155. if NewDir[Length(NewDir)]<>'/' then
  1156. NewDir:=NewDir+'/';
  1157. NewDir:=NewDir+Path;
  1158. Delete(DirList,1,p1);
  1159. if (FpStat(NewDir,Info)>=0) and
  1160. (not fpS_ISDIR(Info.st_Mode)) then
  1161. Begin
  1162. If Pos('./',NewDir)=1 Then
  1163. Delete(NewDir,1,2);
  1164. {DOS strips off an initial .\}
  1165. End
  1166. Else
  1167. NewDir:='';
  1168. Until (DirList='') or (Length(NewDir) > 0);
  1169. FSearch:=NewDir;
  1170. End;
  1171. End;
  1172. Procedure Globfree(var p : pglob);
  1173. {
  1174. Release memory occupied by pglob structure, and names in it.
  1175. sets p to nil.
  1176. }
  1177. var
  1178. temp : pglob;
  1179. begin
  1180. while assigned(p) do
  1181. begin
  1182. temp:=p^.next;
  1183. if assigned(p^.name) then
  1184. freemem(p^.name);
  1185. dispose(p);
  1186. p:=temp;
  1187. end;
  1188. end;
  1189. Function Glob(Const path:pathstr):pglob;
  1190. {
  1191. Fills a tglob structure with entries matching path,
  1192. and returns a pointer to it. Returns nil on error,
  1193. linuxerror is set accordingly.
  1194. }
  1195. var
  1196. temp,
  1197. temp2 : string[255];
  1198. thedir : pdir;
  1199. buffer : pdirent;
  1200. root,
  1201. current : pglob;
  1202. begin
  1203. { Get directory }
  1204. temp:=dirname(path);
  1205. if temp='' then
  1206. temp:='.';
  1207. temp:=temp+#0;
  1208. thedir:=fpopendir(@temp[1]);
  1209. if thedir=nil then
  1210. exit(nil);
  1211. temp:=basename(path,''); { get the pattern }
  1212. if thedir^.dd_fd<0 then
  1213. exit(nil);
  1214. {get the entries}
  1215. root:=nil;
  1216. current:=nil;
  1217. repeat
  1218. buffer:=fpreaddir(thedir^);
  1219. if buffer=nil then
  1220. break;
  1221. temp2:=strpas(@(buffer^.d_name[0]));
  1222. if fnmatch(temp,temp2) then
  1223. begin
  1224. if root=nil then
  1225. begin
  1226. new(root);
  1227. current:=root;
  1228. end
  1229. else
  1230. begin
  1231. new(current^.next);
  1232. current:=current^.next;
  1233. end;
  1234. if current=nil then
  1235. begin
  1236. fpseterrno(ESysENOMEM);
  1237. globfree(root);
  1238. break;
  1239. end;
  1240. current^.next:=nil;
  1241. getmem(current^.name,length(temp2)+1);
  1242. if current^.name=nil then
  1243. begin
  1244. fpseterrno(ESysENOMEM);
  1245. globfree(root);
  1246. break;
  1247. end;
  1248. move(buffer^.d_name[0],current^.name^,length(temp2)+1);
  1249. end;
  1250. until false;
  1251. fpclosedir(thedir^);
  1252. glob:=root;
  1253. end;
  1254. {--------------------------------
  1255. Stat.Mode Macro's
  1256. --------------------------------}
  1257. Initialization
  1258. InitLocalTime;
  1259. finalization
  1260. DoneLocalTime;
  1261. End.
  1262. {
  1263. $Log$
  1264. Revision 1.56 2004-01-04 16:24:05 jonas
  1265. * fixed WaitProcess in case of SysEintr
  1266. Revision 1.55 2003/12/31 20:24:25 marco
  1267. * export statfs(pchar)
  1268. Revision 1.54 2003/12/30 15:43:20 marco
  1269. * linux now compiles with FPC_USE_LIBC
  1270. Revision 1.53 2003/12/30 12:24:01 marco
  1271. * FPC_USE_LIBC
  1272. Revision 1.52 2003/12/08 17:16:30 peter
  1273. * fsearch should only find files
  1274. Revision 1.51 2003/11/19 17:11:40 marco
  1275. * termio unit
  1276. Revision 1.50 2003/11/19 10:54:32 marco
  1277. * some simple restructures
  1278. Revision 1.49 2003/11/17 11:28:08 marco
  1279. * Clone moved to linux, + few small unit unix changes
  1280. Revision 1.48 2003/11/17 10:05:51 marco
  1281. * threads for FreeBSD. Not working tho
  1282. Revision 1.47 2003/11/14 17:30:14 marco
  1283. * weeehoo linuxerror is no more :-)
  1284. Revision 1.46 2003/11/14 16:44:48 marco
  1285. * stream functions converted to work without linuxerror
  1286. Revision 1.45 2003/11/13 18:44:06 marco
  1287. * small fi
  1288. Revision 1.44 2003/11/12 22:19:45 marco
  1289. * more linuxeror fixes
  1290. Revision 1.43 2003/11/03 09:42:28 marco
  1291. * Peter's Cardinal<->Longint fixes patch
  1292. Revision 1.42 2003/10/30 16:42:58 marco
  1293. * fixes for old syscall() convention removing
  1294. Revision 1.41 2003/10/12 19:40:43 marco
  1295. * ioctl fixes. IDE now starts, but
  1296. Revision 1.40 2003/09/29 14:36:06 peter
  1297. * fixed for stricter compiler
  1298. Revision 1.39 2003/09/27 12:51:33 peter
  1299. * fpISxxx macros renamed to C compliant fpS_ISxxx
  1300. Revision 1.38 2003/09/20 12:38:29 marco
  1301. * FCL now compiles for FreeBSD with new 1.1. Now Linux.
  1302. Revision 1.37 2003/09/17 19:07:44 marco
  1303. * more fixes for Unix<->unixutil
  1304. Revision 1.36 2003/09/17 17:30:46 marco
  1305. * Introduction of unixutil
  1306. Revision 1.35 2003/09/16 21:46:27 marco
  1307. * small fixes, checking things on linux
  1308. Revision 1.34 2003/09/16 20:52:24 marco
  1309. * small cleanups. Mostly killing of already commented code in unix etc
  1310. Revision 1.33 2003/09/16 16:13:56 marco
  1311. * fdset functions renamed to fp<posix name>
  1312. Revision 1.32 2003/09/15 20:08:49 marco
  1313. * small fixes. FreeBSD now cycles
  1314. Revision 1.31 2003/09/14 20:15:01 marco
  1315. * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
  1316. Revision 1.30 2003/07/08 21:23:24 peter
  1317. * sparc fixes
  1318. Revision 1.29 2003/05/30 19:58:40 marco
  1319. * Getting NetBSD/i386 to compile.
  1320. Revision 1.28 2003/05/29 19:16:16 marco
  1321. * fixed a small *BSD gotcha
  1322. Revision 1.27 2003/05/24 20:39:54 jonas
  1323. * fixed ExitCode translation in WaitProcess for Linux and Darwin (and
  1324. probably other BSD's as well)
  1325. Revision 1.26 2003/03/11 08:27:59 michael
  1326. * stringtoppchar should use tabs instead of backspace as delimiter
  1327. Revision 1.25 2002/12/18 16:50:39 marco
  1328. * Unix RTL generic parts. Linux working, *BSD will follow shortly
  1329. Revision 1.24 2002/09/07 16:01:28 peter
  1330. * old logs removed and tabs fixed
  1331. Revision 1.23 2002/08/06 13:30:46 sg
  1332. * replaced some Longints with Cardinals, to mach the C headers
  1333. * updated the termios record
  1334. Revision 1.22 2002/03/05 20:04:25 michael
  1335. + Patch from Sebastian for FCNTL call
  1336. Revision 1.21 2002/01/02 12:22:54 marco
  1337. * Removed ifdef arround getepoch.
  1338. }