+#+win32
+;;; KLUDGE: This could be taken from GetSystemInfo
+(export (defun getpagesize () 4096))
+
+;;; passwd database
+;; The docstrings are copied from the descriptions in SUSv3,
+;; where present.
+#-win32
+(define-protocol-class passwd alien-passwd ()
+ ((name :initarg :name :accessor passwd-name
+ :documentation "User's login name.")
+ ;; Note: SUSv3 doesn't require this member.
+ (passwd :initarg :passwd :accessor passwd-passwd
+ :documentation "The account's encrypted password.")
+ (uid :initarg :uid :accessor passwd-uid
+ :documentation "Numerical user ID.")
+ (gid :initarg :gid :accessor passwd-gid
+ :documentation "Numerical group ID.")
+ ;; Note: SUSv3 doesn't require this member.
+ (gecos :initarg :gecos :accessor passwd-gecos
+ :documentation "User's name or comment field.")
+ (dir :initarg :dir :accessor passwd-dir
+ :documentation "Initial working directory.")
+ (shell :initarg :shell :accessor passwd-shell
+ :documentation "Program to use as shell."))
+ (:documentation "Instances of this class represent entries in
+ the system's user database."))
+
+;;; group database
+#-win32
+(define-protocol-class group alien-group ()
+ ((name :initarg :name :accessor group-name)
+ (passwd :initarg :passwd :accessor group-passwd)
+ (gid :initarg :gid :accessor group-gid)))
+
+(defmacro define-obj-call (name arg type conv)
+ #-win32
+ ;; FIXME: this isn't the documented way of doing this, surely?
+ (let ((lisp-name (intern (string-upcase name) :sb-posix)))
+ `(progn
+ (export ',lisp-name :sb-posix)
+ (declaim (inline ,lisp-name))
+ (defun ,lisp-name (,arg)
+ (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
+ (if (null-alien r)
+ nil
+ (,conv r)))))))
+
+(define-obj-call "getpwnam" login-name (function (* alien-passwd) c-string) alien-to-passwd)
+(define-obj-call "getpwuid" uid (function (* alien-passwd) uid-t) alien-to-passwd)
+(define-obj-call "getgrnam" login-name (function (* alien-group) c-string) alien-to-group)
+(define-obj-call "getgrgid" gid (function (* alien-group) gid-t) alien-to-group)
+
+
+#-win32
+(define-protocol-class timeval alien-timeval ()
+ ((sec :initarg :tv-sec :accessor timeval-sec
+ :documentation "Seconds.")
+ (usec :initarg :tv-usec :accessor timeval-usec
+ :documentation "Microseconds."))
+ (:documentation "Instances of this class represent time values."))
+
+(define-protocol-class stat alien-stat ()
+ ((mode :initarg :mode :reader stat-mode
+ :documentation "Mode of file.")
+ (ino :initarg :ino :reader stat-ino
+ :documentation "File serial number.")
+ (dev :initarg :dev :reader stat-dev
+ :documentation "Device ID of device containing file.")
+ (nlink :initarg :nlink :reader stat-nlink
+ :documentation "Number of hard links to the file.")
+ (uid :initarg :uid :reader stat-uid
+ :documentation "User ID of file.")
+ (gid :initarg :gid :reader stat-gid
+ :documentation "Group ID of file.")
+ (size :initarg :size :reader stat-size
+ :documentation "For regular files, the file size in
+ bytes. For symbolic links, the length
+ in bytes of the filename contained in
+ the symbolic link.")
+ (atime :initarg :atime :reader stat-atime
+ :documentation "Time of last access.")
+ (mtime :initarg :mtime :reader stat-mtime
+ :documentation "Time of last data modification.")
+ (ctime :initarg :ctime :reader stat-ctime
+ :documentation "Time of last status change"))
+ (:documentation "Instances of this class represent Posix file
+ metadata."))
+
+(defmacro define-stat-call (name arg designator-fun type)
+ ;; FIXME: this isn't the documented way of doing this, surely?
+ (let ((lisp-name (lisp-for-c-symbol name))
+ (real-name #+inode64 (format nil "~A$INODE64" name)
+ #-inode64 name))
+ `(progn
+ (export ',lisp-name :sb-posix)
+ (declaim (inline ,lisp-name))
+ (defun ,lisp-name (,arg &optional stat)
+ (declare (type (or null stat) stat))
+ (with-alien-stat a-stat ()
+ (let ((r (alien-funcall
+ (extern-alien ,(real-c-name (list real-name :options :largefile)) ,type)
+ (,designator-fun ,arg)
+ a-stat)))
+ (when (minusp r)
+ (syscall-error))
+ (alien-to-stat a-stat stat)))))))
+
+(define-stat-call #-win32 "stat" #+win32 "_stat"
+ pathname filename
+ (function int c-string (* alien-stat)))
+
+#-win32
+(define-stat-call "lstat"
+ pathname filename
+ (function int c-string (* alien-stat)))
+;;; No symbolic links on Windows, so use stat
+#+win32
+(progn
+ (declaim (inline lstat))
+ (export (defun lstat (filename &optional stat)
+ (if stat (stat filename stat) (stat filename)))))
+
+(define-stat-call #-win32 "fstat" #+win32 "_fstat"
+ fd file-descriptor
+ (function int int (* alien-stat)))
+
+
+;;; mode flags
+(define-call "s_isreg" boolean never-fails (mode mode-t))
+(define-call "s_isdir" boolean never-fails (mode mode-t))
+(define-call "s_ischr" boolean never-fails (mode mode-t))
+(define-call "s_isblk" boolean never-fails (mode mode-t))
+(define-call "s_isfifo" boolean never-fails (mode mode-t))
+(define-call "s_islnk" boolean never-fails (mode mode-t))
+(define-call "s_issock" boolean never-fails (mode mode-t))
+
+#-win32
+(progn
+ (export 'pipe :sb-posix)
+ (declaim (inline pipe))
+ (defun pipe (&optional filedes2)
+ (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
+ (unless filedes2
+ (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
+ (let ((r (sb-sys:with-pinned-objects (filedes2)
+ (alien-funcall
+ ;; FIXME: (* INT)? (ARRAY INT 2) would be better
+ (extern-alien "pipe" (function int (* int)))
+ (sb-sys:vector-sap filedes2)))))
+ (when (minusp r)
+ (syscall-error)))
+ (values (aref filedes2 0) (aref filedes2 1))))
+
+#-win32
+(define-protocol-class termios alien-termios ()
+ ((iflag :initarg :iflag :accessor sb-posix:termios-iflag
+ :documentation "Input modes.")
+ (oflag :initarg :oflag :accessor sb-posix:termios-oflag
+ :documentation "Output modes.")
+ (cflag :initarg :cflag :accessor sb-posix:termios-cflag
+ :documentation "Control modes.")
+ (lflag :initarg :lflag :accessor sb-posix:termios-lflag
+ :documentation "Local modes.")
+ (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs
+ :documentation "Control characters"))
+ (:documentation "Instances of this class represent I/O
+ characteristics of the terminal."))
+
+#-win32
+(progn
+ (export 'tcsetattr :sb-posix)
+ (declaim (inline tcsetattr))
+ (defun tcsetattr (fd actions termios)
+ (declare (type termios termios))
+ (with-alien-termios a-termios ()
+ (termios-to-alien termios a-termios)
+ (let ((fd (file-descriptor fd)))
+ (let* ((r (alien-funcall
+ (extern-alien
+ "tcsetattr"
+ (function int int int (* alien-termios)))
+ fd actions a-termios)))
+ (when (minusp r)
+ (syscall-error)))
+ (values))))
+ (export 'tcgetattr :sb-posix)
+ (declaim (inline tcgetattr))
+ (defun tcgetattr (fd &optional termios)
+ (declare (type (or null termios) termios))
+ (with-alien-termios a-termios ()
+ (let ((r (alien-funcall
+ (extern-alien "tcgetattr"
+ (function int int (* alien-termios)))
+ (file-descriptor fd)
+ a-termios)))
+ (when (minusp r)
+ (syscall-error))
+ (setf termios (alien-to-termios a-termios termios))))
+ termios)
+ (define-call "tcdrain" int minusp (fd file-descriptor))
+ (define-call "tcflow" int minusp (fd file-descriptor) (action int))
+ (define-call "tcflush" int minusp (fd file-descriptor) (queue-selector int))
+ (define-call "tcgetsid" pid-t minusp (fd file-descriptor))
+ (define-call "tcsendbreak" int minusp (fd file-descriptor) (duration int))
+ (export 'cfsetispeed :sb-posix)
+ (declaim (inline cfsetispeed))
+ (defun cfsetispeed (speed &optional termios)
+ (declare (type (or null termios) termios))
+ (with-alien-termios a-termios ()
+ (let ((r (alien-funcall
+ (extern-alien "cfsetispeed"
+ (function int (* alien-termios) speed-t))
+ a-termios
+ speed)))
+ (when (minusp r)
+ (syscall-error))
+ (setf termios (alien-to-termios a-termios termios))))
+ termios)
+ (export 'cfsetospeed :sb-posix)
+ (declaim (inline cfsetospeed))
+ (defun cfsetospeed (speed &optional termios)
+ (declare (type (or null termios) termios))
+ (with-alien-termios a-termios ()
+ (let ((r (alien-funcall
+ (extern-alien "cfsetospeed"
+ (function int (* alien-termios) speed-t))
+ a-termios
+ speed)))
+ (when (minusp r)
+ (syscall-error))
+ (setf termios (alien-to-termios a-termios termios))))
+ termios)
+ (export 'cfgetispeed :sb-posix)
+ (declaim (inline cfgetispeed))
+ (defun cfgetispeed (termios)
+ (declare (type termios termios))
+ (with-alien-termios a-termios ()
+ (termios-to-alien termios a-termios)
+ (alien-funcall (extern-alien "cfgetispeed"
+ (function speed-t (* alien-termios)))
+ a-termios)))
+ (export 'cfgetospeed :sb-posix)
+ (declaim (inline cfgetospeed))
+ (defun cfgetospeed (termios)
+ (declare (type termios termios))
+ (with-alien-termios a-termios ()
+ (termios-to-alien termios a-termios)
+ (alien-funcall (extern-alien "cfgetospeed"
+ (function speed-t (* alien-termios)))
+ a-termios))))
+
+
+#-win32
+(progn
+ (export 'time :sb-posix)
+ (defun time ()
+ (let ((result (alien-funcall (extern-alien "time"
+ (function time-t (* time-t)))
+ nil)))
+ (if (minusp result)
+ (syscall-error)
+ result)))
+ (export 'utime :sb-posix)
+ (defun utime (filename &optional access-time modification-time)
+ (let ((fun (extern-alien "utime" (function int c-string
+ (* alien-utimbuf))))
+ (name (filename filename)))
+ (if (not (and access-time modification-time))
+ (alien-funcall fun name nil)
+ (with-alien ((utimbuf (struct alien-utimbuf)))
+ (setf (slot utimbuf 'actime) (or access-time 0)
+ (slot utimbuf 'modtime) (or modification-time 0))
+ (let ((result (alien-funcall fun name (alien-sap utimbuf))))
+ (if (minusp result)
+ (syscall-error)
+ result))))))
+ (export 'utimes :sb-posix)
+ (defun utimes (filename &optional access-time modification-time)
+ (flet ((seconds-and-useconds (time)
+ (multiple-value-bind (integer fractional)
+ (cl:truncate time)
+ (values integer (cl:truncate (* fractional 1000000)))))
+ (maybe-syscall-error (value)
+ (if (minusp value)
+ (syscall-error)
+ value)))
+ (let ((fun (extern-alien "utimes" (function int c-string
+ (* (array alien-timeval 2)))))
+ (name (filename filename)))
+ (if (not (and access-time modification-time))
+ (maybe-syscall-error (alien-funcall fun name nil))
+ (with-alien ((buf (array alien-timeval 2)))
+ (let ((actime (deref buf 0))
+ (modtime (deref buf 1)))
+ (setf (values (slot actime 'sec)
+ (slot actime 'usec))
+ (seconds-and-useconds (or access-time 0))
+ (values (slot modtime 'sec)
+ (slot modtime 'usec))
+ (seconds-and-useconds (or modification-time 0)))
+ (maybe-syscall-error (alien-funcall fun name
+ (alien-sap buf))))))))))
+
+
+;;; environment
+
+(eval-when (:compile-toplevel :load-toplevel)
+ ;; Do this at compile-time as Win32 code below refers to it as
+ ;; sb-posix:getenv.
+ (export 'getenv :sb-posix))
+(defun getenv (name)
+ (let ((r (alien-funcall
+ (extern-alien "getenv" (function (* char) c-string))
+ name)))
+ (declare (type (alien (* char)) r))
+ (unless (null-alien r)
+ (cast r c-string))))
+#-win32
+(progn
+ (define-call "setenv" int minusp (name c-string) (value c-string) (overwrite int))
+ (define-call "unsetenv" int minusp (name c-string))
+ (export 'putenv :sb-posix)
+ (defun putenv (string)
+ (declare (string string))
+ ;; We don't want to call actual putenv: the string passed to putenv ends
+ ;; up in environ, and we any string we allocate GC might move.
+ ;;
+ ;; This makes our wrapper nonconformant if you squit hard enough, but
+ ;; users who care about that should really be calling putenv() directly in
+ ;; order to be able to manage memory sanely.
+ (let ((p (position #\= string))
+ (n (length string)))
+ (if p
+ (if (= p n)
+ (unsetenv (subseq string 0 p))
+ (setenv (subseq string 0 p) (subseq string (1+ p)) 1))
+ (error "Invalid argument to putenv: ~S" string)))))
+#+win32
+(progn
+ ;; Windows doesn't define a POSIX setenv, but happily their _putenv is sane.
+ (define-call* "putenv" int minusp (string c-string))
+ (export 'setenv :sb-posix)
+ (defun setenv (name value overwrite)
+ (declare (string name value))
+ (if (and (zerop overwrite) (sb-posix:getenv name))
+ 0
+ (putenv (concatenate 'string name "=" value))))
+ (export 'unsetenv :sb-posix)
+ (defun unsetenv (name)
+ (declare (string name))
+ (putenv (concatenate 'string name "="))))