From 96a1929af257c585b90c02ab61dc87546fc1d9af Mon Sep 17 00:00:00 2001 From: marco Date: Tue, 12 Nov 2002 14:37:59 +0000 Subject: [PATCH] * Parts of new unix rtl --- rtl/linux/osposix.inc | 104 +++++++++++++++++ rtl/linux/osposixh.inc | 257 +++++++++++++++++++++++++++++++++++++++++ rtl/linux/ostypes.inc | 52 +++++++++ rtl/linux/posmacro.inc | 94 +++++++++++++++ 4 files changed, 507 insertions(+) create mode 100644 rtl/linux/osposix.inc create mode 100644 rtl/linux/osposixh.inc create mode 100644 rtl/linux/ostypes.inc create mode 100644 rtl/linux/posmacro.inc diff --git a/rtl/linux/osposix.inc b/rtl/linux/osposix.inc new file mode 100644 index 0000000000..3c12780bcb --- /dev/null +++ b/rtl/linux/osposix.inc @@ -0,0 +1,104 @@ +{ + $Id$ + Copyright (c) 2002 by Marco van de Voort. + + Implementation of the POSIX unit for *BSD^H^H^Hlinux. In practice only + includes other files, or specifies libc bindings. + + The conditional uselibc can be used to switch from libc to syscall + usage for basic primitives, but it is best to use unit POSIX if + possible. Note that the system unit must also be compiled using uselibc. + + 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. + + **************************************************************************** +} + +//Uses Sysctl; + +{ $I ostypes.inc} +{ $I posmacro.inc} + +{$ifdef uselibc} + {$Linklib c} + +{ var + Errno : cint; external name 'errno';} + + function sys_time(var tloc:time_t): time_t; cdecl; external name 'time'; + function sys_open(const path: pchar; flags : cint; mode: mode_t):cint; cdecl; external name 'open'; + function sys_close(fd : cint): cint; cdecl; external name 'close'; + function sys_lseek(fd : cint; offset : off_t; whence : cint): off_t; cdecl; external name 'lseek'; + function sys_read(fd: cint; buf: pchar; nbytes : size_t): ssize_t; cdecl; external name 'read'; + function sys_write(fd: cint;const buf:pchar; nbytes : size_t): ssize_t; cdecl; external name 'write'; + function sys_unlink(const path: pchar): cint; cdecl; external name 'unlink'; + function sys_rename(const old : pchar; const newpath: pchar): cint; cdecl;external name 'rename'; + function sys_stat(const path: pchar; var buf : stat): cint; cdecl; external name 'stat'; + function sys_chdir(const path : pchar): cint; cdecl; external name 'chdir'; + function sys_mkdir(const path : pchar; mode: mode_t):cint; cdecl; external name 'mkdir'; + function sys_rmdir(const path : pchar): cint; cdecl; external name 'rmdir'; + function sys_opendir(const dirname : pchar): pdir; cdecl; external name 'opendir'; + function sys_readdir(var dirp : dir) : pdirent;cdecl; external name 'readdir'; + function sys_closedir(var dirp : dir): cint; cdecl; external name 'closedir'; + procedure sys_exit(status : cint); cdecl; external name '_exit'; + function sys_sigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; cdecl; external name 'sigaction'; + function sys_ftruncate(fd : cint; flength : off_t): cint; cdecl; external name 'ftruncate'; + function sys_rename(const old : pchar; const newpath: pchar): cint; cdecl;external name 'rename'; + function sys_fstat(fd : cint; var sb : stat): cint; cdecl; external name 'fstat'; + function sys_fork : pid_t; cdecl; external name 'fork'; + function sys_execve(path : pchar; argv : ppchar; envp: ppchar): cint; cdecl; external name 'execve'; + function sys_waitpid(pid : pid_t; var stat_loc : cint; options: cint): pid_t; cdecl; external name 'waitpid'; + function sys_access(const pathname : pchar; amode : cint): cint; cdecl; external name 'access'; + function sys_uname(var name: utsname): cint; cdecl; external name 'uname'; + function sys_Dup(fildes:cint):cint; cdecl; external name 'dup'; + function sys_Dup2(fildes:cint;fildes2:cint):cint; cdecl; external name 'dup2'; + +{$else} + +// uses syscalls. + +function sys_time(var tloc:time_t): time_t; external name 'FPC_SYSC_TIME'; +function sys_open(const path: pchar; flags : cint; mode: mode_t):cint; external name 'FPC_SYSC_OPEN'; +function sys_close(fd : cint): cint; external name 'FPC_SYSC_CLOSE'; +function sys_lseek(fd : cint; offset : off_t; whence : cint): off_t; external name 'FPC_SYSC_LSEEK'; +function sys_read(fd: cint; buf: pchar; nbytes : size_t): ssize_t; external name 'FPC_SYSC_READ'; +function sys_write(fd: cint;const buf:pchar; nbytes : size_t): ssize_t; external name 'FPC_SYSC_WRITE'; +function sys_unlink(const path: pchar): cint; external name 'FPC_SYSC_UNLINK'; +function sys_rename(const old : pchar; const newpath: pchar): cint; external name 'FPC_SYSC_RENAME'; +function sys_stat(const path: pchar; var buf : stat):cint; external name 'FPC_SYSC_STAT'; +function sys_chdir(const path : pchar): cint; external name 'FPC_SYSC_CHDIR'; +function sys_mkdir(const path : pchar; mode: mode_t):cint; external name 'FPC_SYSC_MKDIR'; +function sys_rmdir(const path : pchar): cint; external name 'FPC_SYSC_RMDIR'; +function sys_opendir(const dirname : pchar): pdir; external name 'FPC_SYSC_OPENDIR'; +function sys_closedir(var dirp : dir): cint; external name 'FPC_SYSC_CLOSEDIR'; +function sys_readdir(var dirp : dir) : pdirent; external name 'FPC_SYSC_READDIR'; +procedure sys_exit(status : cint); external name 'FPC_SYSC_EXIT'; +function sys_sigaction(sig: cint; var act : sigactionrec; var oact : sigactionrec): cint; external name 'FPC_SYSC_SIGACTION'; +function sys_ftruncate(fd : cint; flength : off_t): cint; external name 'FPC_SYSC_FTRUNCATE'; +function sys_fstat(fd : cint; var sb : stat): cint; external name 'FPC_SYSC_FSTAT'; +function sys_fork : pid_t; external name 'FPC_SYSC_FORK'; +// function sys_execve(path : pchar; argv : ppchar;envp: ppchar): cint; external name 'FPC_SYSC_EXECVE'; +function sys_waitpid(pid : pid_t; var stat_loc : cint; options: cint): pid_t; external name 'FPC_SYSC_WAITPID'; +function sys_access(const pathname : pchar; amode : cint): cint;external name 'FPC_SYSC_ACCESS'; +function sys_Dup(fildes:cint):cint; external name 'FPC_SYSC_DUP'; +function sys_Dup2(fildes:cint;fildes2:cint):cint; external name 'FPC_SYSC_DUP2'; +function geterrno:cint; external name 'FPC_SYS_GETERRNO'; +procedure seterrno (i:cint); external name 'FPC_SYS_SETERRNO'; + +{$endif} + +{$I basefuncs.inc} + +{ + $Log$ + Revision 1.1 2002-11-12 14:37:59 marco + * Parts of new unix rtl + + + +} diff --git a/rtl/linux/osposixh.inc b/rtl/linux/osposixh.inc new file mode 100644 index 0000000000..2742a77d38 --- /dev/null +++ b/rtl/linux/osposixh.inc @@ -0,0 +1,257 @@ +{ + $Id$ + This file is part of the Free Pascal run time library. + Copyright (c) 2001 by Free Pascal development team + + Implements roughly POSIX 1003.1 conforming interface for Linux + header part. + + This file implements all the types/constants which must + be defined to port FPC to a new POSIX compliant OS. + + 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. + + **********************************************************************} + +{***********************************************************************} +{ POSIX STRUCTURES } +{***********************************************************************} + +{$define oldstat} +{$define oldreaddir} + +{$i ptypes.inc} +{$define oldreaddir} + +// Can't find these two in Posix and in FreeBSD +CONST + _UTSNAME_LENGTH = 65 ; + _UTSNAME_NODENAME_LENGTH = _UTSNAME_LENGTH; + {$ifdef usedomain} + _UTSNAME_DOMAIN_LENGTH = _UTSNAME_LENGTH; + {$endif} + +TYPE + blksize_t = cuint; + blkcnt_t = cuint; + ino64_t = cint64; + off64_t = cint64; + + { system information services } + utsname = record + sysname : Array[0.._UTSNAME_LENGTH -1] OF Char; // Name of this OS + nodename: Array[0.._UTSNAME_NODENAME_LENGTH-1] OF Char; // Name of this network node. + release : Array[0.._UTSNAME_LENGTH -1] OF Char; // Release level. + version : Array[0.._UTSNAME_LENGTH -1] OF Char; // Version level. + machine : Array[0.._UTSNAME_LENGTH -1] OF Char; // Hardware type. + {$ifdef usedomain} + domain : array[0.._UTSNAME_DOMAIN_LENGTH-1] of char; // Linux addition "Domain" + {$endif} + end; + +{$ifdef oldstat} + Stat = packed record + st_dev : word; + pad1 : word; + st_ino : longint; + st_mode, + nlink, + uid, + gid : word; + rdev : word; + pad2 : word; + st_size, + blksze, + blocks, + atime, + unused1, + mtime, + unused2, + ctime, + unused3, + unused4, + unused5 : longint; + end; + PStat=^Stat; + TStat=Stat; + +{$else} + + { file characteristics services } + stat = record + st_dev : dev_t; // inode's device + pad1 : cushort; + {$ifdef 64bitfs} // ?? + __st_ino : ino_t; + {$else} + st_ino : ino_t; // inode's number + {$endif} + st_mode : mode_t; // inode protection mode + st_nlink : nlink_t; // number of hard links + st_uid : uid_t; // user ID of the file's owner + st_gid : gid_t; // group ID of the file's group + st_rdev : dev_t; // device type + pad2 : cushort; + {$ifdef 64bitfs} + st_size : off64_t; // file size, in bytes + {$else} + st_size : off_t; // file size, in bytes + {$endif} + st_blksize : blksize_t; // optimal blocksize for I/O + {$ifdef 64bitfs} + st_blocks : blkcnt64_t; // blocks allocated for file + {$else} + st_blocks : blkcnt_t; // blocks allocated for file + {$endif} + st_atime : time_t; // time of last access + unused1 : culong; + st_mtime : time_t; // time of last data modification + unused2 : culong; + st_ctime : time_t; // time of last file status change + unused3 : culong; + {$ifdef 64bitfs} + st_ino : ino64_t + {$else} + unused4 : culong; + unused5 : culong; + {$endif} + end; +{$endif} + + { directory services } + pdirent = ^dirent; + dirent = packed record + {$ifndef 64bitfs} + d_fileno : ino_t; // file number of entry + d_off : off_t; + {$else} + d_fileno : ino64_t; // file number of entry + d_off : off64_t; + {$endif} + d_reclen : cushort; // length of string in d_name + {$ifdef Uselibc} // Libc different from kernel record! + d_type : cuchar; // file type, see below + {$endif} + d_name : array[0..(255 + 1)-1] of char; // name must be no longer than this + end; + + + pdir = ^dir; +{$ifdef oldreaddir} + // still old one, because we use the old READDIR + // defined somewhere in the old libc source. + dir = packed record + fd : integer; + loc : longint; + size : integer; + buf : pdirent; + {The following are used in libc, but NOT in the linux kernel sources ??} + nextoff: longint; + dd_max : integer; {size of buf. Irrelevant, as buf is of type dirent} + lock : pointer; + end; +{$else} + // new libc one. NOTE that off_t must be real, so 64-bit ifdef + // 64bitsfs + dir = record // packing doesn't matter. This is a userland struct. + fd : cint; + data : pchar; + allocation: size_t; + _size : size_t; + offset : size_t; + filepos : off_t; + end; +{$endif} + + putimbuf = ^utimbuf; + utimbuf = record + actime : time_t; + modtime : time_t; + end; + + flock = record + l_type : cshort; { lock type: read/write, etc. } + l_whence: cshort; { type of l_start } + {$ifdef 64bitfs} + l_start : off64_t; { starting offset } + l_len : off64_t; { len = 0 means until end of file } + {$else} + l_start : off_t; { starting offset } + l_len : off_t; { len = 0 means until end of file } + {$endif} + l_pid : pid_t; { lock owner } + end; + + tms = packed record + tms_utime : clock_t; { User CPU time } + tms_stime : clock_t; { System CPU time } + tms_cutime : clock_t; { User CPU time of terminated child procs } + tms_cstime : clock_t; { System CPU time of terminated child procs } + end; + + +{***********************************************************************} +{ POSIX CONSTANT ROUTINE DEFINITIONS } +{***********************************************************************} +CONST + { access routine - these maybe OR'ed together } + F_OK = 0; { test for existence of file } + R_OK = 4; { test for read permission on file } + W_OK = 2; { test for write permission on file } + X_OK = 1; { test for execute or search permission } + { seek routine } + SEEK_SET = 0; { seek from beginning of file } + SEEK_CUR = 1; { seek from current position } + SEEK_END = 2; { seek from end of file } + { open routine } + { File access modes for `open' and `fcntl'. } + O_RDONLY = 0; { Open read-only. } + O_WRONLY = 1; { Open write-only. } + O_RDWR = 2; { Open read/write. } + { Bits OR'd into the second argument to open. } + O_CREAT = $40; { Create file if it doesn't exist. } + O_EXCL = $80; { Fail if file already exists. } + O_TRUNC = $200; { Truncate file to zero length. } + O_NOCTTY = $100; { Don't assign a controlling terminal. } + { File status flags for `open' and `fcntl'. } + O_APPEND = $400; { Writes append to the file. } + O_NONBLOCK = $800; { Non-blocking I/O. } + + { mode_t possible values } + S_IRUSR = %0100000000; { Read permission for owner } + S_IWUSR = %0010000000; { Write permission for owner } + S_IXUSR = %0001000000; { Exec permission for owner } + S_IRGRP = %0000100000; { Read permission for group } + S_IWGRP = %0000010000; { Write permission for group } + S_IXGRP = %0000001000; { Exec permission for group } + S_IROTH = %0000000100; { Read permission for world } + S_IWOTH = %0000000010; { Write permission for world } + S_IXOTH = %0000000001; { Exec permission for world } + + { Used for waitpid } + WNOHANG = 1; { don't block waiting } + WUNTRACED = 2; { report status of stopped children } + + + {*************************************************************************} + { SIGNALS } + {*************************************************************************} + +{$define posixworkaround} +{$i signal.inc} + +// function geterrno:longint; +// procedure seterrno(i:longint); + +{ + $Log$ + Revision 1.1 2002-11-12 14:37:59 marco + * Parts of new unix rtl + + +} diff --git a/rtl/linux/ostypes.inc b/rtl/linux/ostypes.inc new file mode 100644 index 0000000000..b159886b7e --- /dev/null +++ b/rtl/linux/ostypes.inc @@ -0,0 +1,52 @@ +{ + $Id$ + This file is part of the Free Pascal run time library. + Copyright (c) 1999-2000 by Michael Van Canneyt, + member of the Free Pascal development team. + + OS dependant types internal to the Unix RTL. + + 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. + + **********************************************************************} + + +{ + Linux system calls take arguments as follows : + + i386 m68k ppc + %eax %d0 r0 System call number + %ebx %d1 r3 first argument + %ecx %d2 r4 second argument + %edx %d3 r5 third argumens + %esi %d3 r6 fourth argument + %edi %d4 r7 fifth argument + + That is why we define a special type, with only these arguments + To make it processor independent, we don't give any system dependent + names, but the rather abstract reg1,reg2 etc; +} + SysCallRegs=record + reg1,reg2,reg3,reg4,reg5,reg6 : longint; + end; + PSysCallRegs=^SysCallRegs; + TSysCallRegs=SysCallRegs; + + ptimespec= ^timespec; + timespec = packed record + tv_sec : time_t; + tv_nsec : clong; + end; + +{ + $Log$ + Revision 1.1 2002-11-12 14:37:59 marco + * Parts of new unix rtl + + +} diff --git a/rtl/linux/posmacro.inc b/rtl/linux/posmacro.inc new file mode 100644 index 0000000000..52fc88cb6a --- /dev/null +++ b/rtl/linux/posmacro.inc @@ -0,0 +1,94 @@ +{ + $Id$ + Copyright (c) 2000-2002 by Marco van de Voort + + The "linux" POSIX macro's that are used both in the POSIX unit as the + system unit. Not aliased via public names because I want these to + be inlined as much as possible in the future. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + 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. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + **************************************************************************** +} + +CONST + _S_IFDIR = &040000; + _S_IFCHR = &020000; + _S_IFBLK = &060000; + _S_IFREG = &100000; + _S_IFMT = &170000; + _S_IFIFO = &010000; + +function S_ISDIR(m : mode_t): boolean; + +begin + S_ISDIR:=((m and _S_IFMT) = _S_IFDIR); +end; + +function S_ISCHR(m : mode_t): boolean; +begin + S_ISCHR:=((m and _S_IFMT) = _S_IFCHR); +end; + +function S_ISBLK(m : mode_t): boolean; +begin + S_ISBLK:=((m and _S_IFMT) = _S_IFBLK); +end; + +function S_ISREG(m : mode_t): boolean; +begin + S_ISREG:=((m and _S_IFMT) = _S_IFREG); +end; + +function S_ISFIFO(m : mode_t): boolean; +begin + S_ISFIFO:=((m and _S_IFMT) = _S_IFIFO); +end; + +function wifexited(status : cint): cint; +begin + wifexited:=cint((status AND $7f) =0); +end; + +function wexitstatus(status : cint): cint; +begin + wexitstatus:=(status and $FF00) shr 8; +end; + +function wstopsig(status : cint): cint; +begin + wstopsig:=(status and $FF00) shr 8; +end; + +const wstopped=&177; + +function wifsignaled(status : cint): cint; +begin + wifsignaled:=cint(((status and $FF)<>wstopped) and ((status and &177)<>0)); +end; + +function wtermsig(status : cint):cint; + +begin + wtermsig:=cint(status and &177); +end; + +{ + $Log$ + Revision 1.1 2002-11-12 14:37:59 marco + * Parts of new unix rtl + + +}