2
$Id: osmain.inc,v 1.18 2004/05/16 18:51:20 peter Exp $
3
This file is part of the Free Pascal run time library.
5
POSIX Interface to the system unit
7
See the file COPYING.FPC, included in this distribution,
8
for details about the copyright.
10
This is the core of the system unit *nix systems (now FreeBSD
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
**********************************************************************}
20
{ Default creation mode for directories and files }
22
{ read/write permission for everyone }
23
MODE_OPEN = S_IWUSR OR S_IRUSR OR
26
{ read/write search permission for everyone }
27
MODE_MKDIR = MODE_OPEN OR
28
S_IXUSR OR S_IXGRP OR S_IXOTH;
31
{*****************************************************************************
32
Misc. System Dependent Functions
33
*****************************************************************************}
35
procedure haltproc(e:longint);cdecl;external name '_haltproc';
37
procedure System_exit;
43
Function ParamCount: Longint;
49
function BackPos(c:char; const s: shortstring): integer;
53
for i:=length(s) downto 0 do
54
if s[i] = c then break;
62
{ variable where full path and filename and executable is stored }
63
{ is setup by the startup of the system unit. }
65
execpathstr : shortstring;
67
function paramstr(l: longint) : string;
72
{ stricly conforming POSIX applications }
73
{ have the executing filename as argv[0] }
76
paramstr := execpathstr;
79
paramstr:=strpas(argv[l]);
84
randseed:=longint(Fptime(nil));
88
{*****************************************************************************
90
*****************************************************************************}
93
_HEAP : longint;external name 'HEAP';
94
_HEAPSIZE : longint;external name 'HEAPSIZE';
96
{$ifndef SYSTEM_HAS_GETHEAPSTART}
97
function getheapstart:pointer;
99
getheapstart := @_HEAP;
104
{$ifndef SYSTEM_HAS_GETHEAPSIZE}
105
function getheapsize:longint;
107
getheapsize := _HEAPSIZE;
112
{*****************************************************************************
113
Low Level File Routines
114
*****************************************************************************}
117
The lowlevel file functions should take care of setting the InOutRes to the
118
correct value if an error has occured, else leave it untouched
121
Function PosixToRunError (PosixErrno : longint) : longint;
123
Convert ErrNo error to the correct Inoutres value
127
if PosixErrNo=0 then { Else it will go through all the cases }
131
ESysEMFILE : Inoutres:=4;
132
ESysENOENT : Inoutres:=2;
133
ESysEBADF : Inoutres:=6;
135
ESysEFAULT : Inoutres:=217;
136
ESysEINVAL : Inoutres:=218;
141
ESysENOSPC : Inoutres:=101;
142
ESysENAMETOOLONG : Inoutres := 3;
146
ESysEACCES : Inoutres:=5;
147
ESysEISDIR : InOutRes:=5;
150
InOutRes := Integer(PosixErrno);
153
PosixToRunError:=InOutRes;
157
Function Errno2InoutRes : longint;
159
Errno2InoutRes:=PosixToRunError(getErrno);
160
InoutRes:=Errno2InoutRes;
164
Procedure Do_Close(Handle:thandle);
166
Fpclose(cint(Handle));
170
Procedure Do_Erase(p:pchar);
174
{ verify if the filename is actually a directory }
175
{ if so return error and do nothing, as defined }
177
if Fpstat(p,fileinfo)<0 then
182
if FpS_ISDIR(fileinfo.st_mode) then
187
if Fpunlink(p)<0 then
193
{ truncate at a given position }
194
procedure do_truncate (handle:thandle;fpos:longint);
196
{ should be simulated in cases where it is not }
198
If Fpftruncate(handle,fpos)<0 Then
205
Procedure Do_Rename(p1,p2:pchar);
207
If Fprename(p1,p2)<0 Then
214
Function Do_Write(Handle:thandle;Addr:Pointer;Len:SizeInt):SizeInt;
217
Do_Write:=Fpwrite(Handle,addr,len);
218
until (Do_Write>=0) or (getErrNo<>ESysEINTR);
229
Function Do_Read(Handle:thandle;Addr:Pointer;Len:SizeInt):SizeInt;
232
Do_Read:=Fpread(Handle,addr,len);
233
until (Do_Read>=0) or (getErrNo<>ESysEINTR);
244
function Do_FilePos(Handle: thandle):longint;
246
do_FilePos:=Fplseek(Handle, 0, SEEK_CUR);
254
Procedure Do_Seek(Handle:thandle;Pos:Longint);
256
If Fplseek(Handle, pos, SEEK_SET)<0 Then
263
Function Do_SeekEnd(Handle:thandle): Longint;
265
Do_SeekEnd:=Fplseek(Handle,0,SEEK_END);
273
Function Do_FileSize(Handle:thandle): Longint;
278
Ret:=Fpfstat(handle,info);
280
Do_FileSize:=Info.st_size
290
Procedure Do_Open(var f;p:pchar;flags:longint);
292
FileRec and textrec have both Handle and mode as the first items so
293
they could use the same routine for opening/creating.
294
when (flags and $100) the file will be append
295
when (flags and $1000) the file will be truncate/rewritten
296
when (flags and $10000) there is no check for close (needed for textfiles)
301
{ close first if opened }
302
if ((flags and $10000)=0) then
304
case FileRec(f).mode of
305
fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
309
inoutres:=102; {not assigned}
314
{ reset file Handle }
315
FileRec(f).Handle:=UnusedHandle;
316
{ We do the conversion of filemodes here, concentrated on 1 place }
317
case (flags and 3) of
320
FileRec(f).mode:=fminput;
324
FileRec(f).mode:=fmoutput;
328
FileRec(f).mode:=fminout;
331
if (flags and $1000)=$1000 then
332
oflags:=oflags or (O_CREAT or O_TRUNC)
334
if (flags and $100)=$100 then
335
oflags:=oflags or (O_APPEND);
336
{ empty name is special }
339
case FileRec(f).mode of
341
FileRec(f).Handle:=StdInputHandle;
342
fminout, { this is set by rewrite }
344
FileRec(f).Handle:=StdOutputHandle;
347
FileRec(f).Handle:=StdOutputHandle;
348
FileRec(f).mode:=fmoutput; {fool fmappend}
354
FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
355
if (FileRec(f).Handle<0) and
356
(getErrNo=ESysEROFS) and
357
((OFlags and O_RDWR)<>0) then
359
Oflags:=Oflags and not(O_RDWR);
360
FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
362
If Filerec(f).Handle<0 Then
370
{*****************************************************************************
372
*****************************************************************************}
374
Procedure MkDir(Const s: String);[IOCheck];
376
Buffer: Array[0..255] of Char;
378
If (s='') or (InOutRes <> 0) then
380
Move(s[1], Buffer, Length(s));
381
Buffer[Length(s)] := #0;
382
If Fpmkdir(@buffer, MODE_MKDIR)<0 Then
388
Procedure RmDir(Const s: String);[IOCheck];
390
Buffer: Array[0..255] of Char;
394
If (s='') or (InOutRes <> 0) then
396
Move(s[1], Buffer, Length(s));
397
Buffer[Length(s)] := #0;
398
If Fprmdir(@buffer)<0 Then
405
Procedure ChDir(Const s: String);[IOCheck];
407
Buffer: Array[0..255] of Char;
409
If (s='') or (InOutRes <> 0) then
411
Move(s[1], Buffer, Length(s));
412
Buffer[Length(s)] := #0;
413
If Fpchdir(@buffer)<0 Then
417
{ file not exists is path not found under tp7 }
422
procedure getdir(drivenr : byte;var dir : shortstring);
427
thedir,dummy : string[255];
434
tmp : array[0..4095] of char;
439
if Fpgetcwd(@tmp,10240+512)<>NIL then
451
{ get root directory information }
453
if Fpstat(@tmp[1],rootinfo)<0 then
457
{ get current directory information }
458
if Fpstat(@tmp[1],cwdinfo)<0 then
461
{ open directory stream }
462
{ try to find the current inode number of the cwd }
463
dirstream:=Fpopendir(@tmp[1]);
464
if dirstream=nil then
468
d:=Fpreaddir(dirstream);
469
{ no more entries to read ... }
470
if not assigned(d) then
472
tmp:=dummy+'../'+strpas(d^.d_name) + #0;
473
if (Fpstat(@tmp[1],thisdir)=0) then
475
{ found the entry for this directory name }
476
if (cwdinfo.st_dev=thisdir.st_dev) and (cwdinfo.st_ino=thisdir.st_ino) then
478
{ are the filenames of type '.' or '..' ? }
479
{ then do not set the name. }
480
if (not ((d^.d_name[0]='.') and ((d^.d_name[1]=#0) or
481
((d^.d_name[1]='.') and (d^.d_name[2]=#0))))) then
482
name:='/'+strpas(d^.d_name);
486
If Fpclosedir(dirstream)<0 THen
490
if ((cwdinfo.st_dev=rootinfo.st_dev) and (cwdinfo.st_ino=rootinfo.st_ino)) then
503
{*****************************************************************************
504
SystemUnit Initialization
505
*****************************************************************************}
507
// signal handler is arch dependant due to processorexception to language
508
// exception translation
515
Procedure InstallSignals;
517
{ Initialize the sigaction structure }
518
{ all flags and information set to zero }
519
FillChar(act, sizeof(SigActionRec),0);
520
{ initialize handler }
521
act.sa_handler := signalhandler(@SignalToRunError);
529
FpSigAction(SIGFPE,@act,nil);
530
FpSigAction(SIGSEGV,@act,nil);
531
FpSigAction(SIGBUS,@act,nil);
532
FpSigAction(SIGILL,@act,nil);
535
procedure SetupCmdLine;
545
reallocmem(cmdline,size+bufsize);
546
move(buf^,cmdline[size],bufsize);
558
len:=strlen(argv[i]);
559
if len>ARG_MAX-2 then
563
if argv[i][j]=' ' then
568
if bufsize+len>=ARG_MAX-2 then
575
move(argv[i]^,buf[bufsize],len);
590
FreeMem(buf,ARG_MAX);
595
Revision 1.18 2004/05/16 18:51:20 peter
596
* use thandle in do_*
598
Revision 1.17 2004/05/01 15:59:17 florian
599
* x86_64 exception handling fixed
601
Revision 1.16 2004/04/27 20:47:00 florian
602
* tried to fix x86-64 signal handling
604
Revision 1.15 2004/04/22 21:16:35 peter
605
* do_write/do_read fix
607
Revision 1.14 2004/03/27 14:33:45 florian
608
* tell sigaction to pass siginfo on arm
610
Revision 1.13 2004/03/10 20:35:33 peter
611
* call _haltproc instead of exit(). This is required for libc linking
613
Revision 1.12 2004/01/01 14:19:55 marco
614
* use_getcwd updates because FPC_USE_LIBC uses that
616
Revision 1.11 2003/12/30 16:26:10 marco
617
* some more fixes. Testing on idefix
619
Revision 1.10 2003/12/21 20:30:49 peter
620
* don't exit in getdir when fpstat gives a failure
622
Revision 1.9 2003/12/14 14:28:36 peter
623
* only check errno if the syscall failed
625
Revision 1.8 2003/11/01 01:58:11 marco
628
Revision 1.7 2003/10/31 20:36:01 marco
629
* i386 specific fixes that hopefully fix texception4.
630
Only the "generic" signal handler was ported to the unix rtl.
632
Revision 1.6 2003/09/27 12:51:33 peter
633
* fpISxxx macros renamed to C compliant fpS_ISxxx
635
Revision 1.5 2003/05/01 08:05:23 florian
636
* started to make the rtl 64 bit save by introducing SizeInt and SizeUInt (similar to size_t of C)
638
Revision 1.4 2002/12/24 19:45:40 peter
639
* Fix do_erase which was wrong with inoutres setting
641
Revision 1.3 2002/12/23 22:23:43 peter
642
* fixed Getdir to not set Inoutres
643
* broken symlinks are now ignored in getdir instead of aborting
646
Revision 1.2 2002/12/18 20:43:27 peter
647
* removed stackcheck, the generic stackcheck is used
648
* fixed return value for error conversion when no error was passed
650
Revision 1.1 2002/12/18 16:43:26 marco
651
* new unix rtl, linux part.....
653
Revision 1.7 2002/11/14 12:18:03 marco
654
* fixed Fptime call to (NIL)
656
Revision 1.6 2002/10/27 17:21:29 marco
657
* Only "difficult" functions + execvp + termios + rewinddir left to do
659
Revision 1.5 2002/10/26 18:27:52 marco
660
* First series POSIX calls commits. Including getcwd.
662
Revision 1.4 2002/09/07 16:01:26 peter
663
* old logs removed and tabs fixed
665
Revision 1.3 2002/08/20 12:50:22 marco
666
* New errno handling. Should be libc compatible.
668
Revision 1.2 2002/08/10 13:42:36 marco
669
* Fixes Posix dir copied to devel branch
671
Revision 1.1.2.18 2002/03/10 11:45:02 carl
672
* InOutRes := 16 with rmdir()
673
* InOutRes := 5 more checking
675
Revision 1.1.2.17 2002/03/03 15:11:51 carl
676
* erase() bugfix (erasing a directory is done via rmdir() only!)
678
Revision 1.1.2.16 2002/02/15 18:13:35 carl
679
* bugfix for paramstr(0)