+ (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 ',lisp-name))
+ (alien-to-stat a-stat stat)))))))
+
+(define-stat-call #-win32 "stat" #+win32 "_stat"
+ pathname filename
+ (function int (c-string :not-null t) (* alien-stat)))
+
+#-win32
+(define-stat-call "lstat"
+ pathname filename
+ (function int (c-string :not-null t) (* 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 'pipe)))
+ (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 'tcsetattr)))
+ (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 'tcgetattr))
+ (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 'cfsetispeed))
+ (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 'cfsetospeed))
+ (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 'time)
+ result)))
+ (export 'utime :sb-posix)
+ (defun utime (filename &optional access-time modification-time)
+ (let ((fun (extern-alien "utime" (function int (c-string :not-null t)
+ (* 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 'utime)
+ 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 'utimes)
+ value)))
+ (let ((fun (extern-alien "utimes" (function int (c-string :not-null t)
+ (* (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)