+(define-protocol-class sb-posix::stat sb-posix::alien-stat ()
+ ((sb-posix::mode :initarg :mode :accessor sb-posix:stat-mode)
+ (sb-posix::ino :initarg :ino :accessor sb-posix:stat-ino)
+ (sb-posix::dev :initarg :dev :accessor sb-posix:stat-dev)
+ (sb-posix::nlink :initarg :nlink :accessor sb-posix:stat-nlink)
+ (sb-posix::uid :initarg :uid :accessor sb-posix:stat-uid)
+ (sb-posix::gid :initarg :gid :accessor sb-posix:stat-gid)
+ (sb-posix::size :initarg :size :accessor sb-posix:stat-size)
+ (sb-posix::atime :initarg :atime :accessor sb-posix:stat-atime)
+ (sb-posix::mtime :initarg :mtime :accessor sb-posix:stat-mtime)
+ (sb-posix::ctime :initarg :ctime :accessor sb-posix: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 (intern (string-upcase name) :sb-posix)))
+ `(progn
+ (export ',lisp-name :sb-posix)
+ (declaim (inline ,lisp-name))
+ (defun ,lisp-name (,arg &optional stat)
+ (declare (type (or null (sb-alien:alien (* sb-posix::alien-stat))) stat))
+ (sb-posix::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 "stat" pathname sb-posix::filename
+ (function int c-string (* sb-posix::alien-stat)))
+(define-stat-call "lstat" pathname sb-posix::filename
+ (function int c-string (* sb-posix::alien-stat)))
+(define-stat-call "fstat" fd sb-posix::file-descriptor
+ (function int int (* sb-posix::alien-stat)))
+
+
+;;; mode flags
+(define-call "s_isreg" boolean never-fails (mode sb-posix::mode-t))
+(define-call "s_isdir" boolean never-fails (mode sb-posix::mode-t))
+(define-call "s_ischr" boolean never-fails (mode sb-posix::mode-t))
+(define-call "s_isblk" boolean never-fails (mode sb-posix::mode-t))
+(define-call "s_isfifo" boolean never-fails (mode sb-posix::mode-t))
+(define-call "s_islnk" boolean never-fails (mode sb-posix::mode-t))
+(define-call "s_issock" boolean never-fails (mode sb-posix::mode-t))
+
+(export 'sb-posix::pipe :sb-posix)
+(declaim (inline sb-posix::pipe))
+(defun sb-posix::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)))
+
+(define-protocol-class sb-posix::termios sb-posix::alien-termios ()
+ ((sb-posix::iflag :initarg :iflag :accessor sb-posix:termios-iflag)
+ (sb-posix::oflag :initarg :oflag :accessor sb-posix:termios-oflag)
+ (sb-posix::cflag :initarg :cflag :accessor sb-posix:termios-cflag)
+ (sb-posix::lflag :initarg :lflag :accessor sb-posix:termios-lflag)
+ (sb-posix::cc :initarg :cc :accessor sb-posix:termios-cc :array-length sb-posix::nccs)))
+
+(export 'sb-posix::tcsetattr :sb-posix)
+(declaim (inline sb-posix::tcsetattr))
+(defun sb-posix::tcsetattr (fd actions termios)
+ (sb-posix::with-alien-termios a-termios ()
+ (termios-to-alien termios a-termios)
+ (let ((fd (sb-posix::file-descriptor fd)))
+ (let* ((r (alien-funcall
+ (extern-alien
+ "tcsetattr"
+ (function int int int (* sb-posix::alien-termios)))
+ fd actions a-termios)))
+ (when (minusp r)
+ (syscall-error)))
+ (values))))
+(export 'sb-posix::tcgetattr :sb-posix)
+(declaim (inline sb-posix::tcgetattr))
+(defun sb-posix::tcgetattr (fd &optional termios)
+ (sb-posix::with-alien-termios a-termios ()
+ (let ((r (alien-funcall
+ (extern-alien "tcgetattr"
+ (function int int (* sb-posix::alien-termios)))
+ (sb-posix::file-descriptor fd)
+ a-termios)))
+ (when (minusp r)
+ (syscall-error))
+ (setf termios (alien-to-termios a-termios termios))))
+ termios)
+
+;;; environment
+
+(export 'sb-posix::getenv :sb-posix)
+(defun sb-posix::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))))
+(define-call "putenv" int minusp (string c-string))