fpc/rtl/bsd/osmain.inc
peter 86025bbcb6 * moved file and dir functions to sysfile/sysdir
* win32 thread in systemunit
2005-02-06 13:06:20 +00:00

279 lines
6.3 KiB
PHP

{
$Id$
This file is part of the Free Pascal run time library.
Main OS dependant body of the system unit, loosely modelled
after POSIX. *BSD version (Linux version is near identical)
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.
**********************************************************************}
{*****************************************************************************
Misc. System Dependent Functions
*****************************************************************************}
procedure System_exit;
begin
Fpexit(cint(ExitCode));
End;
Function ParamCount: Longint;
Begin
Paramcount:=argc-1
End;
function BackPos(c:char; const s: shortstring): integer;
var
i: integer;
Begin
for i:=length(s) downto 0 do
if s[i] = c then break;
if i=0 then
BackPos := 0
else
BackPos := i;
end;
{ variable where full path and filename and executable is stored }
{ is setup by the startup of the system unit. }
var
execpathstr : shortstring;
function paramstr(l: longint) : string;
var
s: string;
s1: string;
begin
{ stricly conforming POSIX applications }
{ have the executing filename as argv[0] }
// if l=0 then
// begin
// paramstr := execpathstr;
// end
// else
paramstr:=strpas(argv[l]);
end;
Procedure Randomize;
Begin
randseed:=longint(Fptime(nil));
End;
{*****************************************************************************
Low Level File Routines
*****************************************************************************}
{
The lowlevel file functions should take care of setting the InOutRes to the
correct value if an error has occured, else leave it untouched
}
Function PosixToRunError (PosixErrno : longint) : longint;
{
Convert ErrNo error to the correct Inoutres value
}
begin
if PosixErrNo=0 then { Else it will go through all the cases }
exit(0);
case PosixErrNo of
ESysENFILE,
ESysEMFILE : Inoutres:=4;
ESysENOENT : Inoutres:=2;
ESysEBADF : Inoutres:=6;
ESysENOMEM,
ESysEFAULT : Inoutres:=217;
ESysEINVAL : Inoutres:=218;
ESysEPIPE,
ESysEINTR,
ESysEIO,
ESysEAGAIN,
ESysENOSPC : Inoutres:=101;
ESysENAMETOOLONG : Inoutres := 3;
ESysEROFS,
ESysEEXIST,
ESysENOTEMPTY,
ESysEACCES : Inoutres:=5;
ESysEISDIR : InOutRes:=5;
else
begin
InOutRes := Integer(PosixErrno);
end;
end;
PosixToRunError:=InOutRes;
end;
Function Errno2InoutRes : longint;
begin
Errno2InoutRes:=PosixToRunError(getErrno);
InoutRes:=Errno2InoutRes;
end;
{*****************************************************************************
SystemUnit Initialization
*****************************************************************************}
function reenable_signal(sig : longint) : boolean;
var
e,oe : TSigSet;
i,j : byte;
begin
fillchar(e,sizeof(e),#0);
fillchar(oe,sizeof(oe),#0);
{ set is 1 based PM }
dec(sig);
i:=sig mod 32;
j:=sig div 32;
e[j]:=1 shl i;
fpsigprocmask(SIG_UNBLOCK,@e,@oe);
reenable_signal:=geterrno=0;
end;
{$i sighnd.inc}
var
act: SigActionRec;
Procedure InstallSignals;
var
oldact: SigActionRec;
begin
{ Initialize the sigaction structure }
{ all flags and information set to zero }
FillChar(act, sizeof(SigActionRec),0);
{ initialize handler }
act.sa_handler :=@SignalToRunError;
act.sa_flags:=SA_SIGINFO;
FpSigAction(SIGFPE,act,oldact);
FpSigAction(SIGSEGV,act,oldact);
FpSigAction(SIGBUS,act,oldact);
FpSigAction(SIGILL,act,oldact);
end;
procedure SetupCmdLine;
var
bufsize,
len,j,
size,i : longint;
found : boolean;
buf : pchar;
procedure AddBuf;
begin
reallocmem(cmdline,size+bufsize);
move(buf^,cmdline[size],bufsize);
inc(size,bufsize);
bufsize:=0;
end;
begin
GetMem(buf,ARG_MAX);
size:=0;
bufsize:=0;
i:=0;
while (i<argc) do
begin
len:=strlen(argv[i]);
if len>ARG_MAX-2 then
len:=ARG_MAX-2;
found:=false;
for j:=1 to len do
if argv[i][j]=' ' then
begin
found:=true;
break;
end;
if bufsize+len>=ARG_MAX-2 then
AddBuf;
if found then
begin
buf[bufsize]:='"';
inc(bufsize);
end;
move(argv[i]^,buf[bufsize],len);
inc(bufsize,len);
if found then
begin
buf[bufsize]:='"';
inc(bufsize);
end;
if i<argc then
buf[bufsize]:=' '
else
buf[bufsize]:=#0;
inc(bufsize);
inc(i);
end;
AddBuf;
FreeMem(buf,ARG_MAX);
end;
{
$Log$
Revision 1.17 2005-02-06 13:06:20 peter
* moved file and dir functions to sysfile/sysdir
* win32 thread in systemunit
Revision 1.16 2004/10/25 15:38:59 peter
* compiler defined HEAP and HEAPSIZE removed
Revision 1.15 2004/07/17 15:20:55 jonas
* don't use O_CREATE when opening a file for appending (fixes tw1744)
Revision 1.14 2004/05/16 18:51:20 peter
* use thandle in do_*
Revision 1.13 2004/04/22 21:10:56 peter
* do_read/do_write addr argument changed to pointer
Revision 1.12 2004/01/06 15:42:05 marco
* o_creat added when o_append
Revision 1.11 2004/01/03 14:56:10 marco
* typo fix
Revision 1.10 2004/01/03 12:35:39 marco
* sighnd to separate file, like linux. Some comments removed
Revision 1.9 2003/12/30 12:26:21 marco
* FPC_USE_LIBC
Revision 1.8 2003/12/21 20:31:50 peter
* fix getdir when directory contains files that give EACCESS
Revision 1.7 2003/12/14 14:47:02 marco
* fix for repeating 'x' bug
Revision 1.6 2003/11/18 10:12:25 marco
* Small fixes for EAGAIN. bunxfunc only has comments added.
Revision 1.5 2003/10/27 17:12:45 marco
* fixes for signal handling.
Revision 1.4 2003/10/26 17:01:04 marco
* moved sigprocmask to system
Revision 1.3 2003/09/27 13:04:58 peter
* fpISxxx renamed
Revision 1.2 2003/05/29 20:54:09 marco
* progname fix.
Revision 1.1 2003/01/05 19:01:28 marco
* FreeBSD compiles now with baseunix mods.
}