123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521 |
- {
- $Id$
- This file is part of the Free Pascal run time library.
- Copyright (c) 1999-2000 by Michael Van Canneyt,
- BSD parts (c) 2000 by Marco van de Voort
- members of the Free Pascal development team.
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY;without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- Unit Unix;
- Interface
- Uses UnixUtil,BaseUnix;
- { Get Types and Constants }
- {$i sysconst.inc}
- {$i systypes.inc}
- {Get error numbers, some more signal definitions and other OS dependant
- types (that are not POSIX) }
- {i errno.inc}
- {$I signal.inc}
- {$i ostypes.inc}
- {********************
- File
- ********************}
- Const
- P_IN = 1; // pipes (?)
- P_OUT = 2;
- Const
- LOCK_SH = 1; // flock constants ?
- LOCK_EX = 2;
- LOCK_UN = 8;
- LOCK_NB = 4;
- Type
- Tpipe = baseunix.tfildes; // compability.
- pglob = ^tglob;
- tglob = record
- name : pchar;
- next : pglob;
- end;
- {******************************************************************************
- Procedure/Functions
- ******************************************************************************}
- {**************************
- Time/Date Handling
- ***************************}
- var
- tzdaylight : boolean;
- tzname : array[boolean] of pchar;
- { timezone support }
- procedure GetLocalTimezone(timer:cint;var leap_correct,leap_hit:cint);
- procedure GetLocalTimezone(timer:cint);
- procedure ReadTimezoneFile(fn:string);
- function GetTimezoneFile:string;
- Function GetEpochTime: cint;
- procedure GetTime (var hour,min,sec,msec,usec:word);
- procedure GetTime (var hour,min,sec,sec100:word);
- procedure GetTime (var hour,min,sec:word);
- Procedure GetDate (Var Year,Month,Day:Word);
- Procedure GetDateTime (Var Year,Month,Day,hour,minute,second:Word);
- function SetTime (Hour,Min,Sec:word) : Boolean;
- function SetDate (Year,Month,Day:Word) : Boolean;
- function SetDateTime (Year,Month,Day,hour,minute,second:Word) : Boolean;
- {**************************
- Process Handling
- ***************************}
- function CreateShellArgV (const prog:string):ppchar;
- function CreateShellArgV (const prog:Ansistring):ppchar;
- Function Execv (const path:pathstr;args:ppchar):cint;
- Function Execv (const path: AnsiString;args:ppchar):cint;
- Function Execvp (Path: Pathstr;Args:ppchar;Ep:ppchar):cint;
- Function Execvp (Path: AnsiString; Args:ppchar;Ep:ppchar):cint;
- Function Execl (const Todo: String):cint;
- Function Execl (const Todo: Ansistring):cint;
- Function Execle (Todo: String;Ep:ppchar):cint;
- Function Execle (Todo: AnsiString;Ep:ppchar):cint;
- Function Execlp (Todo: string;Ep:ppchar):cint;
- Function Execlp (Todo: Ansistring;Ep:ppchar):cint;
- Function Shell (const Command:String):cint;
- Function Shell (const Command:AnsiString):cint;
- 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}
- Function WIFSTOPPED (Status: Integer): Boolean;
- Function W_EXITCODE (ReturnCode, Signal: Integer): Integer;
- Function W_STOPCODE (Signal: Integer): Integer;
- {**************************
- File Handling
- ***************************}
- {$ifndef FPC_USE_LIBC} // defined using cdecl for libc.
- Function fsync (fd : cint) : cint;
- Function Flock (fd,mode : cint) : cint ;
- Function fStatFS (Fd: cint;Var Info:tstatfs):cint;
- Function StatFS (Path:pchar;Var Info:tstatfs):cint;
- {$endif}
- Function Flock (var T : text;mode : cint) : cint;
- Function Flock (var F : File;mode : cint) : cint;
- Function SelectText (var T:Text;TimeOut :PTimeVal):cint;
- Function SelectText (var T:Text;TimeOut :cint):cint;
- {**************************
- Directory Handling
- ***************************}
- procedure SeekDir(p:pdir;loc:clong);
- function TellDir(p:pdir):clong;
- {**************************
- Pipe/Fifo/Stream
- ***************************}
- Function AssignPipe (var pipe_in,pipe_out:cint):cint;
- Function AssignPipe (var pipe_in,pipe_out:text):cint;
- Function AssignPipe (var pipe_in,pipe_out:file):cint;
- //Function PClose (Var F:text) : cint;
- //Function PClose (Var F:file) : cint;
- Function POpen (var F:text;const Prog:String;rw:char):cint;
- Function POpen (var F:file;const Prog:String;rw:char):cint;
- function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : cint;
- function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String): cint;
- {$ifndef BSD}
- Function GetDomainName:String;
- Function GetHostName:String;
- {$endif}
- {**************************
- Memory functions
- ***************************}
- const
- PROT_READ = $1; { page can be read }
- PROT_WRITE = $2; { page can be written }
- PROT_EXEC = $4; { page can be executed }
- PROT_NONE = $0; { page can not be accessed }
- MAP_SHARED = $1; { Share changes }
- // MAP_PRIVATE = $2; { Changes are private }
- MAP_TYPE = $f; { Mask for type of mapping }
- MAP_FIXED = $10; { Interpret addr exactly }
- // MAP_ANONYMOUS = $20; { don't use a file }
- MAP_GROWSDOWN = $100; { stack-like segment }
- MAP_DENYWRITE = $800; { ETXTBSY }
- MAP_EXECUTABLE = $1000; { mark it as an executable }
- MAP_LOCKED = $2000; { pages are locked }
- MAP_NORESERVE = $4000; { don't check for reservations }
- {**************************
- Utility functions
- ***************************}
- Function FExpand (Const Path: PathStr):PathStr;
- Function FSearch (const path:pathstr;dirlist:string):pathstr;
- Function Glob (Const path:pathstr):pglob;
- Procedure Globfree (var p:pglob);
- procedure SigRaise (sig:integer);
- {$ifdef FPC_USE_LIBC}
- {$i unxdeclh.inc}
- {$else}
- {$i unxsysch.inc} // calls used in system and not reexported from baseunix
- {$endif}
- {******************************************************************************
- Implementation
- ******************************************************************************}
- {$i unxovlh.inc}
- Implementation
- Uses Strings{$ifndef FPC_USE_LIBC},Syscall{$endif};
- {$i unxovl.inc}
- {$ifndef FPC_USE_LIBC}
- {$i syscallh.inc}
- {$i ossysch.inc}
- {$i unxsysc.inc}
- {$endif}
- { Get the definitions of textrec and filerec }
- {$i textrec.inc}
- {$i filerec.inc}
- {$ifndef FPC_USE_LIBC}
- { Raw System calls are in Syscalls.inc}
- {$ifdef Linux} // Linux has more "oldlinux" compability.
- {$i sysc11.inc}
- {$else}
- {$i syscalls.inc}
- {$endif}
- {$endif}
- {$i unixsysc.inc} {Has platform specific libc part under ifdef, besides
- syscalls}
- Function getenv(name:string):Pchar; external name 'FPC_SYSC_FPGETENV';
- {******************************************************************************
- Process related calls
- ******************************************************************************}
- { Most calls of WaitPID do not handle the result correctly, this funktion treats errors more correctly }
- 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}
- var ret,r,s : cint;
- begin
- s:=$7F00;
- repeat
- r:=fpWaitPid(Pid,@s,0);
- if (r=-1) and (fpgeterrno=ESysEIntr) Then
- r:=0;
- until (r<>0);
- if (r=-1) or (r=0) then // 0 is not a valid return and should never occur (it means status invalid when using WNOHANG)
- WaitProcess:=-1 // return -1 to indicate an error. fpwaitpid updated it.
- else
- begin
- {$ifndef Solaris}
- { at least correct for Linux and Darwin (JM) }
- if (s and $7F)=0 then // Only this is a valid returncode
- {$else}
- if (s and $FF)=0 then // Only this is a valid returncode
- {$endif}
- WaitProcess:=s shr 8
- else if (s>0) then // Until now there is not use of the highest bit , but check this for the future
- WaitProcess:=-s // normal case
- else
- WaitProcess:=s; // s<0 should not occur, but wie return also a negativ value
- end;
- end;
- function InternalCreateShellArgV(cmd:pChar; len:cint):ppchar;
- {
- Create an argv which executes a command in a shell using /bin/sh -c
- }
- const Shell = '/bin/sh'#0'-c'#0;
- var
- pp,p : ppchar;
- // temp : string; !! Never pass a local var back!!
- begin
- getmem(pp,4*4);
- p:=pp;
- p^:=@Shell[1];
- inc(p);
- p^:=@Shell[9];
- inc(p);
- getmem(p^,len+1);
- move(cmd^,p^^,len);
- pchar(p^)[len]:=#0;
- inc(p);
- p^:=Nil;
- InternalCreateShellArgV:=pp;
- end;
- function CreateShellArgV(const prog:string):ppchar;
- begin
- CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog));
- end;
- function CreateShellArgV(const prog:Ansistring):ppchar;
- {
- Create an argv which executes a command in a shell using /bin/sh -c
- using a AnsiString;
- }
- begin
- CreateShellArgV:=InternalCreateShellArgV(@prog[1],length(prog)); // if ppc works like delphi this also work when @prog[1] is invalid (len=0)
- end;
- procedure FreeShellArgV(p:ppchar);
- begin
- if (p<>nil) then begin
- freemem(p[2]);
- freemem(p);
- end;
- end;
- Function Execv(const path: AnsiString;args:ppchar):cint;
- {
- Overloaded ansistring version.
- }
- begin
- Execv:=fpExecVe(Path,Args,envp);
- end;
- Function Execvp(Path: AnsiString; Args:ppchar;Ep:ppchar):cint;
- {
- Overloaded ansistring version
- }
- var
- thepath : Ansistring;
- begin
- if path[1]<>'/' then
- begin
- Thepath:=strpas(fpgetenv('PATH'));
- if thepath='' then
- thepath:='.';
- Path:=FSearch(path,thepath)
- end
- else
- Path:='';
- if Path='' then
- Begin
- fpsetErrno(ESysEnoEnt);
- Exit(-1);
- end
- else
- Execvp:=fpExecve(Path,args,ep);
- end;
- Function Execv(const path:pathstr;args:ppchar):cint;
- {
- Replaces the current program by the program specified in path,
- arguments in args are passed to Execve.
- the current environment is passed on.
- }
- begin
- Execv:=fpExecve(path,args,envp);
- end;
- Function Execvp(Path:Pathstr;Args:ppchar;Ep:ppchar):cint;
- {
- This does the same as Execve, only it searches the PATH environment
- for the place of the Executable, except when Path starts with a slash.
- if the PATH environment variable is unavailable, the path is set to '.'
- }
- var
- thepath : string;
- begin
- if path[1]<>'/' then
- begin
- Thepath:=strpas(fpgetenv('PATH'));
- if thepath='' then
- thepath:='.';
- Path:=FSearch(path,thepath)
- end
- else
- Path:='';
- if Path='' then
- Begin
- fpsetErrno(ESysEnoEnt);
- Exit(-1);
- end
- else
- execvp:=fpExecve(Path,args,ep);
- end;
- Function Execle(Todo:string;Ep:ppchar):cint;
- {
- This procedure takes the string 'Todo', parses it for command and
- command options, and Executes the command with the given options.
- The string 'Todo' shoud be of the form 'command options', options
- separated by commas.
- the PATH environment is not searched for 'command'.
- The specified environment(in 'ep') is passed on to command
- }
- var
- p : ppchar;
- begin
- p:=StringToPPChar(ToDo);
- if (p=nil) or (p^=nil) then
- Begin
- fpsetErrno(ESysEnoEnt);
- Exit(-1);
- end
- else
- execle:=fpExecVE(p^,p,EP);
- end;
- function Execle(Todo:AnsiString;Ep:ppchar):cint;
- {
- This procedure takes the string 'Todo', parses it for command and
- command options, and Executes the command with the given options.
- The string 'Todo' shoud be of the form 'command options', options
- separated by commas.
- the PATH environment is not searched for 'command'.
- The specified environment(in 'ep') is passed on to command
- }
- var
- p : ppchar;
- begin
- p:=StringToPPChar(ToDo);
- if (p=nil) or (p^=nil) then
- Begin
- fpsetErrno(ESysEnoEnt);
- Exit(-1);
- end;
- ExecLe:=fpExecVE(p^,p,EP);
- end;
- Function Execl(const Todo:string):cint;
- {
- This procedure takes the string 'Todo', parses it for command and
- command options, and Executes the command with the given options.
- The string 'Todo' shoud be of the form 'command options', options
- separated by commas.
- the PATH environment is not searched for 'command'.
- The current environment is passed on to command
- }
- begin
- Execl:=ExecLE(ToDo,EnvP);
- end;
- Function Execlp(Todo:string;Ep:ppchar):cint;
- {
- This procedure takes the string 'Todo', parses it for command and
- command options, and Executes the command with the given options.
- The string 'Todo' shoud be of the form 'command options', options
- separated by commas.
- the PATH environment is searched for 'command'.
- The specified environment (in 'ep') is passed on to command
- }
- var
- p : ppchar;
- begin
- p:=StringToPPchar(todo);
- if (p=nil) or (p^=nil) then
- Begin
- fpsetErrno(ESysEnoEnt);
- Exit(-1);
- end;
- Execlp:=ExecVP(StrPas(p^),p,EP);
- end;
- Function Execlp(Todo: Ansistring;Ep:ppchar):cint;
- {
- Overloaded ansistring version.
- }
- var
- p : ppchar;
- begin
- p:=StringToPPchar(todo);
- if (p=nil) or (p^=nil) then
- Begin
- fpsetErrno(ESysEnoEnt);
- Exit(-1);
- end;
- execlp:=ExecVP(StrPas(p^),p,EP);
- end;
- Function Shell(const Command:String):cint;
- {
- Executes the shell, and passes it the string Command. (Through /bin/sh -c)
- The current environment is passed to the shell.
- It waits for the shell to exit, and returns its exit status.
- If the Exec call failed exit status 127 is reported.
- }
- { Changed the structure:
- - the previous version returns an undefinied value if fork fails
- - it returns the status of Waitpid instead of the Process returnvalue (see the doc to Shell)
- - it uses exit(127) not ExitProc (The Result in pp386: going on Compiling in 2 processes!)
- - ShellArgs are now released
- - The Old CreateShellArg gives back pointers to a local var
- }
- var
- p : ppchar;
- pid : cint;
- begin
- p:=CreateShellArgv(command);
- pid:=fpfork;
- if pid=0 then // We are in the Child
- begin
- {This is the child.}
- fpExecve(p^,p,envp);
- fpExit(127); // was Exit(127)
- end
- else if (pid<>-1) then // Successfull started
- Shell:=WaitProcess(pid)
- else // no success
- Shell:=-1; // indicate an error
- FreeShellArgV(p);
- end;
- Function Shell(const Command:AnsiString):cint;
- {
- AnsiString version of Shell
- }
- var
- p : ppchar;
- pid : cint;
- begin { Changes as above }
- p:=CreateShellArgv(command);
- pid:=fpfork;
- if pid=0 then // We are in the Child
- begin
- fpExecve(p^,p,envp);
- fpExit(127); // was exit(127)!! We must exit the Process, not the function
- end
- else if (pid<>-1) then // Successfull started
- Shell:=WaitProcess(pid)
- else // no success
- Shell:=-1;
- FreeShellArgV(p);
- end;
- Function WIFSTOPPED(Status: Integer): Boolean;
- begin
- WIFSTOPPED:=((Status and $FF)=$7F);
- end;
- Function W_EXITCODE(ReturnCode, Signal: Integer): Integer;
- begin
- W_EXITCODE:=(ReturnCode shl 8) or Signal;
- end;
- Function W_STOPCODE(Signal: Integer): Integer;
- begin
- W_STOPCODE:=(Signal shl 8) or $7F;
- end;
- {******************************************************************************
- Date and Time related calls
- ******************************************************************************}
- Function GetEpochTime: cint;
- {
- Get the number of seconds since 00:00, January 1 1970, GMT
- the time NOT corrected any way
- }
- begin
- GetEpochTime:=fptime;
- end;
- procedure GetTime(var hour,min,sec,msec,usec:word);
- {
- Gets the current time, adjusted to local time
- }
- var
- year,day,month:Word;
- tz:timeval;
- begin
- fpgettimeofday(@tz,nil);
- EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
- msec:=tz.tv_usec div 1000;
- usec:=tz.tv_usec mod 1000;
- end;
- procedure GetTime(var hour,min,sec,sec100:word);
- {
- Gets the current time, adjusted to local time
- }
- var
- usec : word;
- begin
- gettime(hour,min,sec,sec100,usec);
- sec100:=sec100 div 10;
- end;
- Procedure GetTime(Var Hour,Min,Sec:Word);
- {
- Gets the current time, adjusted to local time
- }
- var
- msec,usec : Word;
- Begin
- gettime(hour,min,sec,msec,usec);
- End;
- Procedure GetDate(Var Year,Month,Day:Word);
- {
- Gets the current date, adjusted to local time
- }
- var
- hour,minute,second : word;
- Begin
- EpochToLocal(fptime,year,month,day,hour,minute,second);
- End;
- Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
- {
- Gets the current date, adjusted to local time
- }
- Begin
- EpochToLocal(fptime,year,month,day,hour,minute,second);
- End;
- {$ifndef BSD} // this can be done nicer, but I still have
- // to think about what to do with this func.
-
- {$ifdef linux}
- {$ifdef FPC_USE_LIBC}
- function intstime (t:ptime_t):cint; external name 'stime';
- {$endif}
- Function stime (t : cint) : boolean;
- begin
- {$ifdef FPC_USE_LIBC}
- stime:=intstime(@t)=0;
- {$else}
- stime:=do_SysCall(Syscall_nr_stime,cint(@t))=0;
- {$endif}
- end;
- {$endif}
- {$endif}
- {$ifdef BSD}
- Function stime (t : cint) : Boolean;
- begin
- end;
- {$endif}
- Function SetTime(Hour,Min,Sec:word) : boolean;
- var
- Year, Month, Day : Word;
- begin
- GetDate (Year, Month, Day);
- SetTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Min, Sec ) );
- end;
- Function SetDate(Year,Month,Day:Word) : boolean;
- var
- Hour, Minute, Second, Sec100 : Word;
- begin
- GetTime ( Hour, Minute, Second, Sec100 );
- SetDate:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
- end;
- Function SetDateTime(Year,Month,Day,hour,minute,second:Word) : Boolean;
- begin
- SetDateTime:=stime ( LocalToEpoch ( Year, Month, Day, Hour, Minute, Second ) );
- end;
- { Include timezone handling routines which use /usr/share/timezone info }
- {$i timezone.inc}
- {******************************************************************************
- FileSystem calls
- ******************************************************************************}
- Function Execl(const Todo:Ansistring):cint;
- {
- Overloaded AnsiString Version of ExecL.
- }
- begin
- Execl:=ExecLE(ToDo,EnvP);
- end;
- Function Flock (var T : text;mode : cint) : cint;
- begin
- Flock:=Flock(TextRec(T).Handle,mode);
- end;
- Function Flock (var F : File;mode : cint) :cint;
- begin
- Flock:=Flock(FileRec(F).Handle,mode);
- end;
- Function SelectText(var T:Text;TimeOut :PTimeval):cint;
- Var
- F:TfdSet;
- begin
- if textrec(t).mode=fmclosed then
- begin
- fpseterrno(ESysEBADF);
- exit(-1);
- end;
- FpFD_ZERO(f);
- fpFD_SET(textrec(T).handle,f);
- if textrec(T).mode=fminput then
- SelectText:=fpselect(textrec(T).handle+1,@f,nil,nil,TimeOut)
- else
- SelectText:=fpselect(textrec(T).handle+1,nil,@f,nil,TimeOut);
- end;
- Function SelectText(var T:Text;TimeOut :cint):cint;
- var
- p : PTimeVal;
- tv : TimeVal;
- begin
- if TimeOut=-1 then
- p:=nil
- else
- begin
- tv.tv_Sec:=Timeout div 1000;
- tv.tv_Usec:=(Timeout mod 1000)*1000;
- p:=@tv;
- end;
- SelectText:=SelectText(T,p);
- end;
- {******************************************************************************
- Directory
- ******************************************************************************}
- procedure SeekDir(p:pdir;loc:clong);
- begin
- if p=nil then
- begin
- fpseterrno(ESysEBADF);
- exit;
- end;
- {$ifndef bsd}
- p^.dd_nextoff:=fplseek(p^.dd_fd,loc,seek_set);
- {$endif}
- p^.dd_size:=0;
- p^.dd_loc:=0;
- end;
- function TellDir(p:pdir):clong;
- begin
- if p=nil then
- begin
- fpseterrno(ESysEBADF);
- telldir:=-1;
- exit;
- end;
- telldir:=fplseek(p^.dd_fd,0,seek_cur)
- { We could try to use the nextoff field here, but on my 1.2.13
- kernel, this gives nothing... This may have to do with
- the readdir implementation of libc... I also didn't find any trace of
- the field in the kernel code itself, So I suspect it is an artifact of libc.
- Michael. }
- end;
- {******************************************************************************
- Pipes/Fifo
- ******************************************************************************}
- Procedure OpenPipe(var F:Text);
- begin
- case textrec(f).mode of
- fmoutput :
- if textrec(f).userdata[1]<>P_OUT then
- textrec(f).mode:=fmclosed;
- fminput :
- if textrec(f).userdata[1]<>P_IN then
- textrec(f).mode:=fmclosed;
- else
- textrec(f).mode:=fmclosed;
- end;
- end;
- Function IOPipe(var F:text):cint;
- begin
- IOPipe:=0;
- case textrec(f).mode of
- fmoutput :
- begin
- { first check if we need something to write, else we may
- get a SigPipe when Close() is called (PFV) }
- if textrec(f).bufpos>0 then
- IOPipe:=fpwrite(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufpos);
- end;
- fminput : Begin
- textrec(f).bufend:=fpread(textrec(f).handle,pchar(textrec(f).bufptr),textrec(f).bufsize);
- IOPipe:=textrec(f).bufend;
- End;
- end;
- textrec(f).bufpos:=0;
- end;
- Function FlushPipe(var F:Text):cint;
- begin
- FlushPipe:=0;
- if (textrec(f).mode=fmoutput) and (textrec(f).bufpos<>0) then
- FlushPipe:=IOPipe(f);
- textrec(f).bufpos:=0;
- end;
- Function ClosePipe(var F:text):cint;
- begin
- textrec(f).mode:=fmclosed;
- ClosePipe:=fpclose(textrec(f).handle);
- end;
- Function AssignPipe(var pipe_in,pipe_out:text):cint;
- {
- Sets up a pair of file variables, which act as a pipe. The first one can
- be read from, the second one can be written to.
- }
- var
- f_in,f_out : cint;
- begin
- if AssignPipe(f_in,f_out)=-1 then
- exit(-1);
- { Set up input }
- Assign(Pipe_in,'');
- Textrec(Pipe_in).Handle:=f_in;
- Textrec(Pipe_in).Mode:=fmInput;
- Textrec(Pipe_in).userdata[1]:=P_IN;
- TextRec(Pipe_in).OpenFunc:=@OpenPipe;
- TextRec(Pipe_in).InOutFunc:=@IOPipe;
- TextRec(Pipe_in).FlushFunc:=@FlushPipe;
- TextRec(Pipe_in).CloseFunc:=@ClosePipe;
- { Set up output }
- Assign(Pipe_out,'');
- Textrec(Pipe_out).Handle:=f_out;
- Textrec(Pipe_out).Mode:=fmOutput;
- Textrec(Pipe_out).userdata[1]:=P_OUT;
- TextRec(Pipe_out).OpenFunc:=@OpenPipe;
- TextRec(Pipe_out).InOutFunc:=@IOPipe;
- TextRec(Pipe_out).FlushFunc:=@FlushPipe;
- TextRec(Pipe_out).CloseFunc:=@ClosePipe;
- AssignPipe:=0;
- end;
- Function AssignPipe(var pipe_in,pipe_out:file):cint;
- {
- Sets up a pair of file variables, which act as a pipe. The first one can
- be read from, the second one can be written to.
- If the operation was unsuccesful,
- }
- var
- f_in,f_out : cint;
- begin
- if AssignPipe(f_in,f_out)=-1 then
- exit(-1);
- { Set up input }
- Assign(Pipe_in,'');
- Filerec(Pipe_in).Handle:=f_in;
- Filerec(Pipe_in).Mode:=fmInput;
- Filerec(Pipe_in).recsize:=1;
- Filerec(Pipe_in).userdata[1]:=P_IN;
- { Set up output }
- Assign(Pipe_out,'');
- Filerec(Pipe_out).Handle:=f_out;
- Filerec(Pipe_out).Mode:=fmoutput;
- Filerec(Pipe_out).recsize:=1;
- Filerec(Pipe_out).userdata[1]:=P_OUT;
- AssignPipe:=0;
- end;
- Function PCloseText(Var F:text):cint;
- {
- May not use @PClose due overloading
- }
- begin
- PCloseText:=PClose(f);
- end;
- function POpen(var F:text;const Prog:String;rw:char):cint;
- {
- Starts the program in 'Prog' and makes it's input or out put the
- other end of a pipe. If rw is 'w' or 'W', then whatever is written to
- F, will be read from stdin by the program in 'Prog'. The inverse is true
- for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
- read from 'f'.
- }
- var
- pipi,
- pipo : text;
- pid : pid_t;
- pl : ^cint;
- pp : ppchar;
- ret : cint;
- begin
- rw:=upcase(rw);
- if not (rw in ['R','W']) then
- begin
- FpSetErrno(ESysEnoent);
- exit(-1);
- end;
- if AssignPipe(pipi,pipo)=-1 Then
- Exit(-1);
- pid:=fpfork;
- if pid=-1 then
- begin
- close(pipi);
- close(pipo);
- exit(-1);
- end;
- if pid=0 then
- begin
- { We're in the child }
- if rw='W' then
- begin
- close(pipo);
- ret:=fpdup2(pipi,input);
- close(pipi);
- if ret=-1 then
- halt(127);
- end
- else
- begin
- close(pipi);
- ret:=fpdup2(pipo,output);
- close(pipo);
- if ret=-1 then
- halt(127);
- end;
- pp:=createshellargv(prog);
- fpExecve(pp^,pp,envp);
- halt(127);
- end
- else
- begin
- { We're in the parent }
- if rw='W' then
- begin
- close(pipi);
- f:=pipo;
- textrec(f).bufptr:=@textrec(f).buffer;
- end
- else
- begin
- close(pipo);
- f:=pipi;
- textrec(f).bufptr:=@textrec(f).buffer;
- end;
- {Save the process ID - needed when closing }
- pl:=@(textrec(f).userdata[2]);
- pl^:=pid;
- textrec(f).closefunc:=@PCloseText;
- end;
- ret:=0;
- end;
- Function POpen(var F:file;const Prog:String;rw:char):cint;
- {
- Starts the program in 'Prog' and makes it's input or out put the
- other end of a pipe. If rw is 'w' or 'W', then whatever is written to
- F, will be read from stdin by the program in 'Prog'. The inverse is true
- for 'r' or 'R' : whatever the program in 'Prog' writes to stdout, can be
- read from 'f'.
- }
- var
- pipi,
- pipo : file;
- pid : cint;
- pl : ^cint;
- p,pp : ppchar;
- temp : string[255];
- ret : cint;
- begin
- rw:=upcase(rw);
- if not (rw in ['R','W']) then
- begin
- FpSetErrno(ESysEnoent);
- exit(-1);
- end;
- ret:=AssignPipe(pipi,pipo);
- if ret=-1 then
- exit(-1);
- pid:=fpfork;
- if pid=-1 then
- begin
- close(pipi);
- close(pipo);
- exit(-1);
- end;
- if pid=0 then
- begin
- { We're in the child }
- if rw='W' then
- begin
- close(pipo);
- ret:=fpdup2(filerec(pipi).handle,stdinputhandle);
- close(pipi);
- if ret=-1 then
- halt(127);
- end
- else
- begin
- close(pipi);
- ret:=fpdup2(filerec(pipo).handle,stdoutputhandle);
- close(pipo);
- if ret=1 then
- halt(127);
- end;
- getmem(pp,sizeof(pchar)*4);
- temp:='/bin/sh'#0'-c'#0+prog+#0;
- p:=pp;
- p^:=@temp[1];
- inc(p);
- p^:=@temp[9];
- inc(p);
- p^:=@temp[12];
- inc(p);
- p^:=Nil;
- fpExecve(ansistring('/bin/sh'),pp,envp);
- halt(127);
- end
- else
- begin
- { We're in the parent }
- if rw='W' then
- begin
- close(pipi);
- f:=pipo;
- end
- else
- begin
- close(pipo);
- f:=pipi;
- end;
- {Save the process ID - needed when closing }
- pl:=@(filerec(f).userdata[2]);
- pl^:=pid;
- end;
- POpen:=0;
- end;
- Function AssignStream(Var StreamIn,Streamout:text;Const Prog:String) : cint;
- {
- Starts the program in 'Prog' and makes its input and output the
- other end of two pipes, which are the stdin and stdout of a program
- specified in 'Prog'.
- streamout can be used to write to the program, streamin can be used to read
- the output of the program. See the following diagram :
- Parent Child
- STreamout --> Input
- Streamin <-- Output
- Return value is the process ID of the process being spawned, or -1 in case of failure.
- }
- var
- pipi,
- pipo : text;
- pid : cint;
- pl : ^cint;
- begin
- AssignStream:=-1;
- if AssignPipe(streamin,pipo)=-1 Then
- exit(-1);
- if AssignPipe(pipi,streamout)=-1 Then // shouldn't this close streamin and pipo?
- exit(-1);
- pid:=fpfork;
- if pid=-1 then
- begin
- close(pipi);
- close(pipo);
- close (streamin);
- close (streamout);
- exit;
- end;
- if pid=0 then
- begin
- { We're in the child }
- { Close what we don't need }
- close(streamout);
- close(streamin);
- if fpdup2(pipi,input)=-1 Then
- halt(127);
- close(pipi);
- If fpdup2(pipo,output)=-1 Then
- halt (127);
- close(pipo);
- Execl(Prog);
- halt(127);
- end
- else
- begin
- { we're in the parent}
- close(pipo);
- close(pipi);
- {Save the process ID - needed when closing }
- pl:=@(textrec(StreamIn).userdata[2]);
- pl^:=pid;
- textrec(StreamIn).closefunc:=@PCloseText;
- {Save the process ID - needed when closing }
- pl:=@(textrec(StreamOut).userdata[2]);
- pl^:=pid;
- textrec(StreamOut).closefunc:=@PCloseText;
- AssignStream:=Pid;
- end;
- end;
- function AssignStream(var StreamIn, StreamOut, StreamErr: Text; const prog: String):cint;
- {
- Starts the program in 'prog' and makes its input, output and error output the
- other end of three pipes, which are the stdin, stdout and stderr of a program
- specified in 'prog'.
- StreamOut can be used to write to the program, StreamIn can be used to read
- the output of the program, StreamErr reads the error output of the program.
- See the following diagram :
- Parent Child
- StreamOut --> StdIn (input)
- StreamIn <-- StdOut (output)
- StreamErr <-- StdErr (error output)
- }
- var
- PipeIn, PipeOut, PipeErr: text;
- pid: cint;
- pl: ^cint;
- begin
- AssignStream := -1;
- // Assign pipes
- if AssignPipe(StreamIn, PipeOut)=-1 Then
- Exit(-1);
- If AssignPipe(StreamErr, PipeErr)=-1 Then
- begin
- Close(StreamIn);
- Close(PipeOut);
- exit(-1);
- end;
- if AssignPipe(PipeIn, StreamOut)=-1 Then
- begin
- Close(StreamIn);
- Close(PipeOut);
- Close(StreamErr);
- Close(PipeErr);
- exit(-1);
- end;
- // Fork
- pid := fpFork;
- if pid=-1 then begin
- Close(StreamIn);
- Close(PipeOut);
- Close(StreamErr);
- Close(PipeErr);
- Close(PipeIn);
- Close(StreamOut);
- exit(-1);
- end;
- if pid = 0 then begin
- // *** We are in the child ***
- // Close what we don not need
- Close(StreamOut);
- Close(StreamIn);
- Close(StreamErr);
- // Connect pipes
- if fpdup2(PipeIn, Input)=-1 Then
- Halt(127);
- Close(PipeIn);
- if fpdup2(PipeOut, Output)=-1 Then
- Halt(127);
- Close(PipeOut);
- if fpdup2(PipeErr, StdErr)=-1 Then
- Halt(127);
- Close(PipeErr);
- // Execute program
- Execl(Prog);
- Halt(127);
- end else begin
- // *** We are in the parent ***
- Close(PipeErr);
- Close(PipeOut);
- Close(PipeIn);
- // Save the process ID - needed when closing
- pl := @(TextRec(StreamIn).userdata[2]);
- pl^ := pid;
- TextRec(StreamIn).closefunc := @PCloseText;
- // Save the process ID - needed when closing
- pl := @(TextRec(StreamOut).userdata[2]);
- pl^ := pid;
- TextRec(StreamOut).closefunc := @PCloseText;
- // Save the process ID - needed when closing
- pl := @(TextRec(StreamErr).userdata[2]);
- pl^ := pid;
- TextRec(StreamErr).closefunc := @PCloseText;
- AssignStream := pid;
- end;
- end;
- {******************************************************************************
- General information calls
- ******************************************************************************}
- {$ifndef BSD}
- Function GetDomainName:String; { linux only!}
- // domainname is a glibc extension.
- {
- Get machines domain name. Returns empty string if not set.
- }
- Var
- Sysn : utsname;
- begin
- If fpUname(sysn)<>0 then
- getdomainname:=''
- else
- getdomainname:=strpas(@Sysn.domain[0]);
- end;
- {$endif}
- Function GetHostName:String;
- {
- Get machines name. Returns empty string if not set.
- }
- Var
- Sysn : utsname;
- begin
- If fpuname(sysn)=-1 then
- gethostname:=''
- else
- gethostname:=strpas(@Sysn.nodename[0]);
- end;
- {******************************************************************************
- Signal handling calls
- ******************************************************************************}
- procedure SigRaise(sig:integer);
- begin
- fpKill(fpGetPid,Sig);
- end;
- {******************************************************************************
- Utility calls
- ******************************************************************************}
- {
- Function Octal(l:cint):cint;
- {
- Convert an octal specified number to decimal;
- }
- var
- octnr,
- oct : cint;
- begin
- octnr:=0;
- oct:=0;
- while (l>0) do
- begin
- oct:=oct or ((l mod 10) shl octnr);
- l:=l div 10;
- inc(octnr,3);
- end;
- Octal:=oct;
- end;
- }
- {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
- {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
- {$I fexpand.inc}
- {$UNDEF FPC_FEXPAND_GETENVPCHAR}
- {$UNDEF FPC_FEXPAND_TILDE}
- Function FSearch(const path:pathstr;dirlist:string):pathstr;
- {
- Searches for a file 'path' in the list of direcories in 'dirlist'.
- returns an empty string if not found. Wildcards are NOT allowed.
- If dirlist is empty, it is set to '.'
- }
- Var
- NewDir : PathStr;
- p1 : cint;
- Info : Stat;
- Begin
- {Replace ':' with ';'}
- for p1:=1to length(dirlist) do
- if dirlist[p1]=':' then
- dirlist[p1]:=';';
- {Check for WildCards}
- If (Pos('?',Path) <> 0) or (Pos('*',Path) <> 0) Then
- FSearch:='' {No wildcards allowed in these things.}
- Else
- Begin
- Dirlist:='.;'+dirlist;{Make sure current dir is first to be searched.}
- Repeat
- p1:=Pos(';',DirList);
- If p1=0 Then
- p1:=255;
- NewDir:=Copy(DirList,1,P1 - 1);
- if NewDir[Length(NewDir)]<>'/' then
- NewDir:=NewDir+'/';
- NewDir:=NewDir+Path;
- Delete(DirList,1,p1);
- if (FpStat(NewDir,Info)>=0) and
- (not fpS_ISDIR(Info.st_Mode)) then
- Begin
- If Pos('./',NewDir)=1 Then
- Delete(NewDir,1,2);
- {DOS strips off an initial .\}
- End
- Else
- NewDir:='';
- Until (DirList='') or (Length(NewDir) > 0);
- FSearch:=NewDir;
- End;
- End;
- Procedure Globfree(var p : pglob);
- {
- Release memory occupied by pglob structure, and names in it.
- sets p to nil.
- }
- var
- temp : pglob;
- begin
- while assigned(p) do
- begin
- temp:=p^.next;
- if assigned(p^.name) then
- freemem(p^.name);
- dispose(p);
- p:=temp;
- end;
- end;
- Function Glob(Const path:pathstr):pglob;
- {
- Fills a tglob structure with entries matching path,
- and returns a pointer to it. Returns nil on error,
- linuxerror is set accordingly.
- }
- var
- temp,
- temp2 : string[255];
- thedir : pdir;
- buffer : pdirent;
- root,
- current : pglob;
- begin
- { Get directory }
- temp:=dirname(path);
- if temp='' then
- temp:='.';
- temp:=temp+#0;
- thedir:=fpopendir(@temp[1]);
- if thedir=nil then
- exit(nil);
- temp:=basename(path,''); { get the pattern }
- if thedir^.dd_fd<0 then
- exit(nil);
- {get the entries}
- root:=nil;
- current:=nil;
- repeat
- buffer:=fpreaddir(thedir^);
- if buffer=nil then
- break;
- temp2:=strpas(@(buffer^.d_name[0]));
- if fnmatch(temp,temp2) then
- begin
- if root=nil then
- begin
- new(root);
- current:=root;
- end
- else
- begin
- new(current^.next);
- current:=current^.next;
- end;
- if current=nil then
- begin
- fpseterrno(ESysENOMEM);
- globfree(root);
- break;
- end;
- current^.next:=nil;
- getmem(current^.name,length(temp2)+1);
- if current^.name=nil then
- begin
- fpseterrno(ESysENOMEM);
- globfree(root);
- break;
- end;
- move(buffer^.d_name[0],current^.name^,length(temp2)+1);
- end;
- until false;
- fpclosedir(thedir^);
- glob:=root;
- end;
- {--------------------------------
- Stat.Mode Macro's
- --------------------------------}
- Initialization
- InitLocalTime;
- finalization
- DoneLocalTime;
- End.
- {
- $Log$
- Revision 1.56 2004-01-04 16:24:05 jonas
- * fixed WaitProcess in case of SysEintr
- Revision 1.55 2003/12/31 20:24:25 marco
- * export statfs(pchar)
- Revision 1.54 2003/12/30 15:43:20 marco
- * linux now compiles with FPC_USE_LIBC
- Revision 1.53 2003/12/30 12:24:01 marco
- * FPC_USE_LIBC
- Revision 1.52 2003/12/08 17:16:30 peter
- * fsearch should only find files
- Revision 1.51 2003/11/19 17:11:40 marco
- * termio unit
- Revision 1.50 2003/11/19 10:54:32 marco
- * some simple restructures
- Revision 1.49 2003/11/17 11:28:08 marco
- * Clone moved to linux, + few small unit unix changes
- Revision 1.48 2003/11/17 10:05:51 marco
- * threads for FreeBSD. Not working tho
- Revision 1.47 2003/11/14 17:30:14 marco
- * weeehoo linuxerror is no more :-)
- Revision 1.46 2003/11/14 16:44:48 marco
- * stream functions converted to work without linuxerror
- Revision 1.45 2003/11/13 18:44:06 marco
- * small fi
- Revision 1.44 2003/11/12 22:19:45 marco
- * more linuxeror fixes
- Revision 1.43 2003/11/03 09:42:28 marco
- * Peter's Cardinal<->Longint fixes patch
- Revision 1.42 2003/10/30 16:42:58 marco
- * fixes for old syscall() convention removing
- Revision 1.41 2003/10/12 19:40:43 marco
- * ioctl fixes. IDE now starts, but
- Revision 1.40 2003/09/29 14:36:06 peter
- * fixed for stricter compiler
- Revision 1.39 2003/09/27 12:51:33 peter
- * fpISxxx macros renamed to C compliant fpS_ISxxx
- Revision 1.38 2003/09/20 12:38:29 marco
- * FCL now compiles for FreeBSD with new 1.1. Now Linux.
- Revision 1.37 2003/09/17 19:07:44 marco
- * more fixes for Unix<->unixutil
- Revision 1.36 2003/09/17 17:30:46 marco
- * Introduction of unixutil
- Revision 1.35 2003/09/16 21:46:27 marco
- * small fixes, checking things on linux
- Revision 1.34 2003/09/16 20:52:24 marco
- * small cleanups. Mostly killing of already commented code in unix etc
- Revision 1.33 2003/09/16 16:13:56 marco
- * fdset functions renamed to fp<posix name>
- Revision 1.32 2003/09/15 20:08:49 marco
- * small fixes. FreeBSD now cycles
- Revision 1.31 2003/09/14 20:15:01 marco
- * Unix reform stage two. Remove all calls from Unix that exist in Baseunix.
- Revision 1.30 2003/07/08 21:23:24 peter
- * sparc fixes
- Revision 1.29 2003/05/30 19:58:40 marco
- * Getting NetBSD/i386 to compile.
- Revision 1.28 2003/05/29 19:16:16 marco
- * fixed a small *BSD gotcha
- Revision 1.27 2003/05/24 20:39:54 jonas
- * fixed ExitCode translation in WaitProcess for Linux and Darwin (and
- probably other BSD's as well)
- Revision 1.26 2003/03/11 08:27:59 michael
- * stringtoppchar should use tabs instead of backspace as delimiter
- Revision 1.25 2002/12/18 16:50:39 marco
- * Unix RTL generic parts. Linux working, *BSD will follow shortly
- Revision 1.24 2002/09/07 16:01:28 peter
- * old logs removed and tabs fixed
- Revision 1.23 2002/08/06 13:30:46 sg
- * replaced some Longints with Cardinals, to mach the C headers
- * updated the termios record
- Revision 1.22 2002/03/05 20:04:25 michael
- + Patch from Sebastian for FCNTL call
- Revision 1.21 2002/01/02 12:22:54 marco
- * Removed ifdef arround getepoch.
- }
|