unix.pp 38 KB

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