+;; 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)
+ "Returns the resolved target of a symbolic link as a string."
+ (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))))))))