unix.pp 37 KB

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