+(defmacro define-call* (name &rest arguments)
+ #-win32 `(define-call ,name ,@arguments)
+ #+win32 `(define-call ,(if (consp name)
+ `(,(concatenate 'string "_" (car name))
+ ,@(cdr name))
+ (concatenate 'string "_" name))
+ ,@arguments))
+
+(define-call* "access" int minusp (pathname filename) (mode int))
+(define-call* "chdir" int minusp (pathname filename))
+(define-call* "chmod" int minusp (pathname filename) (mode mode-t))
+(define-call* "close" int minusp (fd file-descriptor))
+(define-call* "creat" int minusp (pathname filename) (mode mode-t))
+(define-call* "dup" int minusp (oldfd file-descriptor))
+(define-call* "dup2" int minusp (oldfd file-descriptor)
+ (newfd file-descriptor))
+(define-call* ("lseek" :options :largefile)
+ off-t minusp (fd file-descriptor) (offset off-t)
+ (whence int))
+(define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
+(macrolet ((def (x)
+ `(progn
+ (define-call-internally open-with-mode ,x int minusp
+ (pathname filename) (flags int) (mode mode-t))
+ (define-call-internally open-without-mode ,x int minusp
+ (pathname filename) (flags int))
+ (define-entry-point ,x
+ (pathname flags &optional (mode nil mode-supplied))
+ (if mode-supplied
+ (open-with-mode pathname flags mode)
+ (open-without-mode pathname flags))))))
+ (def #-win32 "open" #+win32 "_open"))
+(define-call "rename" int minusp (oldpath filename) (newpath filename))
+(define-call* "rmdir" int minusp (pathname filename))
+(define-call* "unlink" int minusp (pathname filename))
+(define-call #-netbsd "opendir" #+netbsd "_opendir"
+ (* t) null-alien (pathname filename))
+(define-call (#-netbsd "readdir" #+netbsd "_readdir" :options :largefile)
+ (* dirent)
+ ;; readdir() has the worst error convention in the world. It's just
+ ;; too painful to support. (return is NULL _and_ errno "unchanged"
+ ;; is not an error, it's EOF).
+ not
+ (dir (* t)))
+(define-call "closedir" int minusp (dir (* t)))
+;; need to do this here because we can't do it in the DEFPACKAGE
+(define-call* "umask" mode-t never-fails (mode mode-t))
+(define-call* "getpid" pid-t never-fails)
+
+#-win32
+(progn
+ (define-call "chown" int minusp (pathname filename)
+ (owner uid-t) (group gid-t))
+ (define-call "chroot" int minusp (pathname filename))
+ (define-call "fchdir" int minusp (fd file-descriptor))
+ (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
+ (define-call "fchown" int minusp (fd file-descriptor)
+ (owner uid-t) (group gid-t))
+ (define-call "fdatasync" int minusp (fd file-descriptor))
+ (define-call ("ftruncate" :options :largefile)
+ int minusp (fd file-descriptor) (length off-t))
+ (define-call "fsync" int minusp (fd file-descriptor))
+ (define-call "lchown" int minusp (pathname filename)
+ (owner uid-t) (group gid-t))
+ (define-call "link" int minusp (oldpath filename) (newpath filename))
+ (define-call "lockf" int minusp (fd file-descriptor) (cmd int) (len off-t))
+ (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
+ (define-call "symlink" int minusp (oldpath filename) (newpath filename))
+ (define-call "sync" void never-fails)
+ (define-call ("truncate" :options :largefile)
+ int minusp (pathname filename) (length off-t))
+ ;; FIXME: Windows does have _mktemp, which has a slightlty different
+ ;; interface
+ (defun mkstemp (template)
+ ;; we are emulating sb-alien's charset conversion for strings
+ ;; here, to accommodate for the call-by-reference nature of
+ ;; mkstemp's template strings.
+ (let ((arg (sb-ext:string-to-octets
+ (filename template)
+ :external-format sb-alien::*default-c-string-external-format*)))
+ (sb-sys:with-pinned-objects (arg)
+ (let ((result (alien-funcall (extern-alien "mkstemp"
+ (function int c-string))
+ (sap-alien (sb-alien::vector-sap arg)
+ (* char)))))
+ (when (minusp result)
+ (syscall-error))
+ (values result
+ (sb-ext:octets-to-string
+ arg
+ :external-format sb-alien::*default-c-string-external-format*))))))
+ (define-call-internally ioctl-without-arg "ioctl" int minusp
+ (fd file-descriptor) (cmd int))
+ (define-call-internally ioctl-with-int-arg "ioctl" int minusp
+ (fd file-descriptor) (cmd int) (arg int))
+ (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
+ (fd file-descriptor) (cmd int)
+ (arg alien-pointer-to-anything-or-nil))
+ (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
+ (if argp
+ (etypecase arg
+ ((alien int) (ioctl-with-int-arg fd cmd arg))
+ ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
+ (ioctl-without-arg fd cmd)))
+ (define-call-internally fcntl-without-arg "fcntl" int minusp
+ (fd file-descriptor) (cmd int))
+ (define-call-internally fcntl-with-int-arg "fcntl" int minusp
+ (fd file-descriptor) (cmd int) (arg int))
+ (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
+ (fd file-descriptor) (cmd int)
+ (arg alien-pointer-to-anything-or-nil))
+ (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
+ (if argp
+ (etypecase arg
+ ((alien int) (fcntl-with-int-arg fd cmd arg))
+ ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
+ (fcntl-without-arg fd cmd)))
+
+ ;; uid, gid
+ (define-call "geteuid" uid-t never-fails) ; "always successful", it says
+ (define-call "getresuid" uid-t never-fails)
+ (define-call "getuid" uid-t never-fails)
+ (define-call "seteuid" int minusp (uid uid-t))
+ (define-call "setfsuid" int minusp (uid uid-t))
+ (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
+ (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
+ (define-call "setuid" int minusp (uid uid-t))
+ (define-call "getegid" gid-t never-fails)
+ (define-call "getgid" gid-t never-fails)
+ (define-call "getresgid" gid-t never-fails)
+ (define-call "setegid" int minusp (gid gid-t))
+ (define-call "setfsgid" int minusp (gid gid-t))
+ (define-call "setgid" int minusp (gid gid-t))
+ (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
+ (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
+
+ ;; processes, signals
+ (define-call "alarm" int never-fails (seconds unsigned))
+
+
+
+ #+mach-exception-handler
+ (progn
+ ;; FIXME this is a lie, of course this can fail, but there's no
+ ;; error handling here yet!
+ (define-call "setup_mach_exceptions" void never-fails)
+ (define-call ("posix_fork" :c-name "fork") pid-t minusp)
+ (defun fork ()
+ (let ((pid (posix-fork)))
+ (when (= pid 0)
+ (setup-mach-exceptions))
+ pid))
+ (export 'fork :sb-posix))
+
+ #-mach-exception-handler
+ (define-call "fork" pid-t minusp)
+
+ (define-call "getpgid" pid-t minusp (pid pid-t))
+ (define-call "getppid" pid-t never-fails)
+ (define-call "getpgrp" pid-t never-fails)
+ (define-call "getsid" pid-t minusp (pid pid-t))
+ (define-call "kill" int minusp (pid pid-t) (signal int))
+ (define-call "killpg" int minusp (pgrp int) (signal int))
+ (define-call "pause" int minusp)
+ (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
+ (define-call "setpgrp" int minusp))
+
+(defmacro with-growing-c-string ((buffer size) &body body)
+ (sb-int:with-unique-names (c-string-block)
+ `(block ,c-string-block
+ (let (,buffer)
+ (flet ((,buffer (&optional (size-incl-null))
+ (when size-incl-null
+ (setf (sb-sys:sap-ref-8 (sb-alien:alien-sap ,buffer) size-incl-null)
+ 0))
+ (return-from ,c-string-block
+ (sb-alien::c-string-to-string
+ (sb-alien:alien-sap ,buffer)
+ (sb-impl::default-external-format)
+ 'character))))
+ (loop for ,size = 128 then (* 2 ,size)
+ do (unwind-protect
+ (progn
+ (setf ,buffer (make-alien c-string ,size))
+ ,@body)
+ (when ,buffer
+ (free-alien ,buffer)))))))))
+
+#-win32
+(progn
+ (export 'readlink :sb-posix)
+ (defun readlink (pathspec)
+ (flet ((%readlink (path buf length)
+ (alien-funcall
+ (extern-alien "readlink" (function int c-string (* t) int))
+ path buf length)))
+ (with-growing-c-string (buf size)
+ (let ((count (%readlink (filename pathspec) buf size)))
+ (cond ((minusp count)
+ (syscall-error))
+ ((< 0 count size)
+ (buf count))))))))
+
+(progn
+ (export 'getcwd :sb-posix)
+ (defun getcwd ()
+ (flet ((%getcwd (buffer size)
+ (alien-funcall
+ (extern-alien #-win32 "getcwd"
+ #+win32 "_getcwd" (function c-string (* t) int))
+ buffer size)))
+ (with-growing-c-string (buf size)
+ (let ((result (%getcwd buf size)))
+ (cond (result
+ (buf))
+ ((/= (get-errno) sb-posix:erange)
+ (syscall-error))))))))
+
+#-win32
+(progn
+ (export 'wait :sb-posix)
+ (declaim (inline wait))
+ (defun wait (&optional statusptr)
+ (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
+ (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
+ (pid (sb-sys:with-pinned-objects (ptr)
+ (alien-funcall
+ (extern-alien "wait" (function pid-t (* int)))
+ (sb-sys:vector-sap ptr)))))
+ (if (minusp pid)
+ (syscall-error)
+ (values pid (aref ptr 0))))))
+
+#-win32
+(progn
+ (export 'waitpid :sb-posix)
+ (declaim (inline waitpid))
+ (defun waitpid (pid options &optional statusptr)
+ (declare (type (sb-alien:alien pid-t) pid)
+ (type (sb-alien:alien int) options)
+ (type (or null (simple-array (signed-byte 32) (1))) statusptr))
+ (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
+ (pid (sb-sys:with-pinned-objects (ptr)
+ (alien-funcall
+ (extern-alien "waitpid" (function pid-t
+ pid-t (* int) int))
+ pid (sb-sys:vector-sap ptr) options))))
+ (if (minusp pid)
+ (syscall-error)
+ (values pid (aref ptr 0)))))
+ ;; waitpid macros
+ (define-call "wifexited" boolean never-fails (status int))
+ (define-call "wexitstatus" int never-fails (status int))
+ (define-call "wifsignaled" boolean never-fails (status int))
+ (define-call "wtermsig" int never-fails (status int))
+ (define-call "wifstopped" boolean never-fails (status int))
+ (define-call "wstopsig" int never-fails (status int))
+ #+nil ; see alien/waitpid-macros.c
+ (define-call "wifcontinued" boolean never-fails (status int)))
+
+;;; mmap, msync
+#-win32
+(progn
+ (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
+ (lambda (res)
+ (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
+ (addr sap-or-nil) (length unsigned) (prot unsigned)
+ (flags unsigned) (fd file-descriptor) (offset off-t))
+
+ (define-call "munmap" int minusp
+ (start sb-sys:system-area-pointer) (length unsigned))