+#+win32
+;;; KLUDGE: This could be taken from GetSystemInfo
+(export (defun getpagesize () 4096))
+
+;;; passwd database
+#-win32
+(define-protocol-class passwd alien-passwd ()
+ ((name :initarg :name :accessor passwd-name)
+ (passwd :initarg :passwd :accessor passwd-passwd)
+ (uid :initarg :uid :accessor passwd-uid)
+ (gid :initarg :gid :accessor passwd-gid)
+ (gecos :initarg :gecos :accessor passwd-gecos)
+ (dir :initarg :dir :accessor passwd-dir)
+ (shell :initarg :shell :accessor passwd-shell)))
+
+(defmacro define-pw-call (name arg type)
+ #-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 r)
+ r
+ (alien-to-passwd r)))))))
+
+(define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
+(define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
+
+(define-protocol-class stat alien-stat ()
+ ((mode :initarg :mode :accessor stat-mode)
+ (ino :initarg :ino :accessor stat-ino)
+ (dev :initarg :dev :accessor stat-dev)
+ (nlink :initarg :nlink :accessor stat-nlink)
+ (uid :initarg :uid :accessor stat-uid)
+ (gid :initarg :gid :accessor stat-gid)
+ (size :initarg :size :accessor stat-size)
+ (atime :initarg :atime :accessor stat-atime)
+ (mtime :initarg :mtime :accessor stat-mtime)
+ (ctime :initarg :ctime :accessor stat-ctime)))
+
+(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)))
+ `(progn
+ (export ',lisp-name :sb-posix)
+ (declaim (inline ,lisp-name))
+ (defun ,lisp-name (,arg &optional stat)
+ (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
+ (with-alien-stat a-stat ()
+ (let ((r (alien-funcall
+ (extern-alien ,name ,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 (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)
+ (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
+ (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
+ (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
+ (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
+
+#-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)
+ (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))))
+
+;;; environment