unix.pp 38 KB

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