1 (cl:in-package :sb-posix)
3 (defmacro define-protocol-class
4 (name alien-type superclasses slots &rest options)
5 (let ((to-protocol (intern (format nil "ALIEN-TO-~A" name)))
6 (to-alien (intern (format nil "~A-TO-ALIEN" name))))
8 (export ',name :sb-posix)
9 (defclass ,name ,superclasses
10 ,(loop for slotd in slots
11 collect (ldiff slotd (member :array-length slotd)))
13 (declaim (inline ,to-alien ,to-protocol))
14 (declaim (inline ,to-protocol ,to-alien))
15 (defun ,to-protocol (alien &optional instance)
16 (declare (type (sb-alien:alien (* ,alien-type)) alien)
17 (type (or null ,name) instance))
19 (setf instance (make-instance ',name)))
20 ,@(loop for slotd in slots
21 ;; FIXME: slotds in source are more complicated in general
23 ;; FIXME: baroque construction of intricate fragility
24 for array-length = (getf (cdr slotd) :array-length)
27 (let ((array (make-array ,array-length)))
28 (setf (slot-value instance ',(car slotd))
30 (dotimes (i ,array-length)
33 (sb-alien:slot alien ',(car slotd))
36 collect `(setf (slot-value instance ',(car slotd))
37 (sb-alien:slot alien ',(car slotd))))
39 (defun ,to-alien (instance &optional alien)
40 (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
41 (type ,name instance))
43 (setf alien (sb-alien:make-alien ,alien-type)))
44 ,@(loop for slotd in slots
45 for array-length = (getf (cdr slotd) :array-length)
48 (let ((array (slot-value instance ',(car slotd))))
49 (dotimes (i ,array-length)
51 (sb-alien:slot alien ',(car slotd))
55 collect `(setf (sb-alien:slot alien ',(car slotd))
56 (slot-value instance ',(car slotd)))))
57 (find-class ',name))))
59 (define-condition sb-posix:syscall-error (error)
60 ((errno :initarg :errno :reader sb-posix:syscall-errno))
61 (:report (lambda (c s)
62 (let ((errno (sb-posix:syscall-errno c)))
63 (format s "System call error ~A (~A)"
64 errno (sb-int:strerror errno))))))
69 (do-symbols (symbol (find-package "SB-POSIX"))
70 (when (get symbol 'errno)
71 (let ((errno (symbol-value symbol)))
72 (setf errno-max (max errno errno-max))
74 (eval `(define-condition ,symbol (syscall-error) ())))
76 (let ((table (make-array (1+ errno-max))))
77 (mapc #'(lambda (cons) (setf (elt table (car cons)) (cdr cons))) list)
80 (defun syscall-error ()
81 (let ((errno (get-errno)))
82 (error (elt *errno-table* errno) :errno errno)))
84 (defun unsupported-error (lisp-name c-name)
85 (error "~S is unsupported by SBCL on this platform due to lack of ~A()."
88 (defun unsupported-warning (lisp-name c-name)
89 (warn "~S is unsupported by SBCL on this platform due to lack of ~A()."
92 ;; Note that we inherit from SIMPLE-FILE-ERROR first, to get its
93 ;; error reporting, rather than SYSCALL-ERROR's.
94 (define-condition file-syscall-error
95 (sb-impl::simple-file-error syscall-error)
98 (defvar *file-errno-table*
99 (let ((array (copy-seq *errno-table*)))
101 (lambda (condition-class-name)
102 (if (symbolp condition-class-name)
103 (let ((file-condition-name
105 (format nil "FILE-~A" condition-class-name))))
106 ;; Should condition class names like FILE-ENOENT
107 ;; and FILE-ENOTDIR be exported? I want to say
108 ;; "no", since we already export ENOENT, ENOTDIR
109 ;; et al, and so the user can write handlers
112 ;; (handler-bind ((sb-posix:enoent ...)
113 ;; (sb-posix:enotdir ...)
117 ;; which will do the right thing for all our
118 ;; FILE-SYSCALL-ERRORs, without exposing this
119 ;; implementation detail. (Recall that some
120 ;; FILE-ERRORs don't strictly have to do with
121 ;; the file system, e.g., supplying a wild
122 ;; pathname to some functions.) But if the
123 ;; prevailing opinion is otherwise, uncomment
125 #| (export file-condition-name) |#
126 (eval `(define-condition ,file-condition-name
127 (,condition-class-name file-syscall-error)
129 condition-class-name))
133 ;; Note: do we have to declare SIMPLE-FILE-PERROR notinline in
135 (sb-ext:without-package-locks
136 (defun sb-impl::simple-file-perror (note-format pathname errno)
137 (error (elt *file-errno-table* errno)
140 :format-control "~@<~?: ~2I~_~A~:>"
142 (list note-format (list pathname) (sb-int:strerror errno)))))
144 ;; Note: it might prove convenient to develop a parallel set of
145 ;; condition classes for STREAM-ERRORs, too.
146 (declaim (inline never-fails))
147 (defun never-fails (&rest args)
148 (declare (ignore args))
151 ;;; Some systems may need C-level wrappers, which can live in the
152 ;;; runtime (so that save-lisp-and-die can produce standalone
153 ;;; executables). See REAL-C-NAME in macros.lisp for the use of this
155 (eval-when (:compile-toplevel :load-toplevel)
156 (setf *c-functions-in-runtime*
157 '`(#+netbsd ,@("stat" "lstat" "fstat" "readdir" "opendir"))))
160 ;;; filesystem access
161 (defmacro define-call* (name &rest arguments)
162 #-win32 `(define-call ,name ,@arguments)
163 #+win32 `(define-call ,(if (consp name)
164 `(,(concatenate 'string "_" (car name))
166 (concatenate 'string "_" name))
169 (define-call* "access" int minusp (pathname filename) (mode int))
170 (define-call* "chdir" int minusp (pathname filename))
171 (define-call* "chmod" int minusp (pathname filename) (mode mode-t))
172 (define-call* "close" int minusp (fd file-descriptor))
173 (define-call* "creat" int minusp (pathname filename) (mode mode-t))
174 (define-call* "dup" int minusp (oldfd file-descriptor))
175 (define-call* "dup2" int minusp (oldfd file-descriptor)
176 (newfd file-descriptor))
177 (define-call* ("lseek" :options :largefile)
178 off-t minusp (fd file-descriptor) (offset off-t)
180 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
183 (define-call-internally open-with-mode ,x int minusp
184 (pathname filename) (flags int) (mode mode-t))
185 (define-call-internally open-without-mode ,x int minusp
186 (pathname filename) (flags int))
187 (define-entry-point ,x
188 (pathname flags &optional (mode nil mode-supplied))
190 (open-with-mode pathname flags mode)
191 (open-without-mode pathname flags))))))
192 (def #-win32 "open" #+win32 "_open"))
193 (define-call "rename" int minusp (oldpath filename) (newpath filename))
194 (define-call* "rmdir" int minusp (pathname filename))
195 (define-call* "unlink" int minusp (pathname filename))
196 (define-call #-netbsd "opendir" #+netbsd "_opendir"
197 (* t) null-alien (pathname filename))
198 (define-call (#-netbsd "readdir" #+netbsd "_readdir" :options :largefile)
200 ;; readdir() has the worst error convention in the world. It's just
201 ;; too painful to support. (return is NULL _and_ errno "unchanged"
202 ;; is not an error, it's EOF).
205 (define-call "closedir" int minusp (dir (* t)))
206 ;; need to do this here because we can't do it in the DEFPACKAGE
207 (define-call* "umask" mode-t never-fails (mode mode-t))
208 (define-call* "getpid" pid-t never-fails)
212 (define-call "chown" int minusp (pathname filename)
213 (owner uid-t) (group gid-t))
214 (define-call "chroot" int minusp (pathname filename))
215 (define-call "fchdir" int minusp (fd file-descriptor))
216 (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
217 (define-call "fchown" int minusp (fd file-descriptor)
218 (owner uid-t) (group gid-t))
219 (define-call "fdatasync" int minusp (fd file-descriptor))
220 (define-call ("ftruncate" :options :largefile)
221 int minusp (fd file-descriptor) (length off-t))
222 (define-call "fsync" int minusp (fd file-descriptor))
223 (define-call "lchown" int minusp (pathname filename)
224 (owner uid-t) (group gid-t))
225 (define-call "link" int minusp (oldpath filename) (newpath filename))
226 (define-call "lockf" int minusp (fd file-descriptor) (cmd int) (len off-t))
227 (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
228 (define-call "symlink" int minusp (oldpath filename) (newpath filename))
229 (define-call "sync" void never-fails)
230 (define-call ("truncate" :options :largefile)
231 int minusp (pathname filename) (length off-t))
233 (macrolet ((def-mk*temp (lisp-name c-name result-type errorp dirp values)
234 (declare (ignore dirp))
235 (if (sb-sys:find-foreign-symbol-address c-name)
237 (defun ,lisp-name (template)
238 (let* ((external-format sb-alien::*default-c-string-external-format*)
239 (arg (sb-ext:string-to-octets
241 :external-format external-format)))
242 (sb-sys:with-pinned-objects (arg)
243 ;; accommodate for the call-by-reference
244 ;; nature of mks/dtemp's template strings.
245 (let ((result (alien-funcall (extern-alien ,c-name
246 (function ,result-type system-area-pointer))
247 (sb-alien::vector-sap arg))))
248 (when (,errorp result)
250 ;; FIXME: We'd rather return pathnames, but other
251 ;; SB-POSIX functions like this return strings...
252 (let ((pathname (sb-ext:octets-to-string
253 arg :external-format external-format)))
255 '(values result pathname)
257 (export ',lisp-name))
259 (defun ,lisp-name (template)
260 (declare (ignore template))
261 (unsupported-error ',lisp-name ,c-name))
262 (define-compiler-macro ,lisp-name (&whole form template)
263 (declare (ignore template))
264 (unsupported-warning ',lisp-name ,c-name)
266 (export ',lisp-name)))))
267 (def-mk*temp mktemp "mktemp" (* char) null-alien nil nil)
268 ;; FIXME: Windows does have _mktemp, which has a slightly different
270 (def-mk*temp mkstemp "mkstemp" int minusp nil t)
271 ;; FIXME: What about Windows?
272 (def-mk*temp mkdtemp "mkdtemp" (* char) null-alien t nil))
273 (define-call-internally ioctl-without-arg "ioctl" int minusp
274 (fd file-descriptor) (cmd int))
275 (define-call-internally ioctl-with-int-arg "ioctl" int minusp
276 (fd file-descriptor) (cmd int) (arg int))
277 (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
278 (fd file-descriptor) (cmd int)
279 (arg alien-pointer-to-anything-or-nil))
280 (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
283 ((alien int) (ioctl-with-int-arg fd cmd arg))
284 ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
285 (ioctl-without-arg fd cmd)))
286 (define-call-internally fcntl-without-arg "fcntl" int minusp
287 (fd file-descriptor) (cmd int))
288 (define-call-internally fcntl-with-int-arg "fcntl" int minusp
289 (fd file-descriptor) (cmd int) (arg int))
290 (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
291 (fd file-descriptor) (cmd int)
292 (arg alien-pointer-to-anything-or-nil))
293 (define-protocol-class flock alien-flock ()
294 ((type :initarg :type :accessor flock-type
295 :documentation "Type of lock; F_RDLCK, F_WRLCK, F_UNLCK.")
296 (whence :initarg :whence :accessor flock-whence
297 :documentation "Flag for starting offset.")
298 (start :initarg :start :accessor flock-start
299 :documentation "Relative offset in bytes.")
300 (len :initarg :len :accessor flock-len
301 :documentation "Size; if 0 then until EOF.")
302 ;; Note: PID isn't initable, and is read-only. But other stuff in
303 ;; SB-POSIX right now loses when a protocol-class slot is unbound,
304 ;; so we initialize it to 0.
305 (pid :initform 0 :reader flock-pid
307 "Process ID of the process holding the lock; returned with F_GETLK."))
308 (:documentation "Class representing locks used in fcntl(2)."))
309 (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
312 ((alien int) (fcntl-with-int-arg fd cmd arg))
313 ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg))
314 (flock (with-alien-flock a-flock ()
315 (flock-to-alien arg a-flock)
316 (let ((r (fcntl-with-pointer-arg fd cmd a-flock)))
317 (alien-to-flock a-flock arg)
319 (fcntl-without-arg fd cmd)))
322 (define-call "geteuid" uid-t never-fails) ; "always successful", it says
323 (define-call "getresuid" uid-t never-fails)
324 (define-call "getuid" uid-t never-fails)
325 (define-call "seteuid" int minusp (uid uid-t))
326 (define-call "setfsuid" int minusp (uid uid-t))
327 (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
328 (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
329 (define-call "setuid" int minusp (uid uid-t))
330 (define-call "getegid" gid-t never-fails)
331 (define-call "getgid" gid-t never-fails)
332 (define-call "getresgid" gid-t never-fails)
333 (define-call "setegid" int minusp (gid gid-t))
334 (define-call "setfsgid" int minusp (gid gid-t))
335 (define-call "setgid" int minusp (gid gid-t))
336 (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
337 (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
339 ;; processes, signals
340 (define-call "alarm" int never-fails (seconds unsigned))
344 #+mach-exception-handler
346 ;; FIXME this is a lie, of course this can fail, but there's no
347 ;; error handling here yet!
348 (define-call "setup_mach_exceptions" void never-fails)
349 (define-call ("posix_fork" :c-name "fork") pid-t minusp)
351 (let ((pid (posix-fork)))
353 (setup-mach-exceptions))
355 (export 'fork :sb-posix))
357 #-mach-exception-handler
358 (define-call "fork" pid-t minusp)
360 (define-call "getpgid" pid-t minusp (pid pid-t))
361 (define-call "getppid" pid-t never-fails)
362 (define-call "getpgrp" pid-t never-fails)
363 (define-call "getsid" pid-t minusp (pid pid-t))
364 (define-call "kill" int minusp (pid pid-t) (signal int))
365 (define-call "killpg" int minusp (pgrp int) (signal int))
366 (define-call "pause" int minusp)
367 (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
368 (define-call "setpgrp" int minusp))
370 (defmacro with-growing-c-string ((buffer size) &body body)
371 (sb-int:with-unique-names (c-string-block)
372 `(block ,c-string-block
374 (flet ((,buffer (&optional (size-incl-null))
376 (setf (sb-sys:sap-ref-8 (sb-alien:alien-sap ,buffer) size-incl-null)
378 (return-from ,c-string-block
379 (sb-alien::c-string-to-string
380 (sb-alien:alien-sap ,buffer)
381 (sb-impl::default-external-format)
383 (loop for ,size = 128 then (* 2 ,size)
386 (setf ,buffer (make-alien c-string ,size))
389 (free-alien ,buffer)))))))))
393 (export 'readlink :sb-posix)
394 (defun readlink (pathspec)
395 "Returns the resolved target of a symbolic link as a string."
396 (flet ((%readlink (path buf length)
398 (extern-alien "readlink" (function int c-string (* t) int))
400 (with-growing-c-string (buf size)
401 (let ((count (%readlink (filename pathspec) buf size)))
402 (cond ((minusp count)
408 (export 'getcwd :sb-posix)
410 "Returns the process's current working directory as a string."
411 (flet ((%getcwd (buffer size)
413 (extern-alien #-win32 "getcwd"
414 #+win32 "_getcwd" (function c-string (* t) int))
416 (with-growing-c-string (buf size)
417 (let ((result (%getcwd buf size)))
420 ((/= (get-errno) sb-posix:erange)
421 (syscall-error))))))))
425 (export 'wait :sb-posix)
426 (declaim (inline wait))
427 (defun wait (&optional statusptr)
428 (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
429 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
430 (pid (sb-sys:with-pinned-objects (ptr)
432 (extern-alien "wait" (function pid-t (* int)))
433 (sb-sys:vector-sap ptr)))))
436 (values pid (aref ptr 0))))))
440 (export 'waitpid :sb-posix)
441 (declaim (inline waitpid))
442 (defun waitpid (pid options &optional statusptr)
443 (declare (type (sb-alien:alien pid-t) pid)
444 (type (sb-alien:alien int) options)
445 (type (or null (simple-array (signed-byte 32) (1))) statusptr))
446 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
447 (pid (sb-sys:with-pinned-objects (ptr)
449 (extern-alien "waitpid" (function pid-t
451 pid (sb-sys:vector-sap ptr) options))))
454 (values pid (aref ptr 0)))))
456 (define-call "wifexited" boolean never-fails (status int))
457 (define-call "wexitstatus" int never-fails (status int))
458 (define-call "wifsignaled" boolean never-fails (status int))
459 (define-call "wtermsig" int never-fails (status int))
460 (define-call "wifstopped" boolean never-fails (status int))
461 (define-call "wstopsig" int never-fails (status int))
462 #+nil ; see alien/waitpid-macros.c
463 (define-call "wifcontinued" boolean never-fails (status int)))
468 (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
470 (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
471 (addr sap-or-nil) (length unsigned) (prot unsigned)
472 (flags unsigned) (fd file-descriptor) (offset off-t))
474 (define-call "munmap" int minusp
475 (start sb-sys:system-area-pointer) (length unsigned))
477 (define-call "msync" int minusp
478 (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
481 (define-call "getpagesize" int minusp)
483 ;;; KLUDGE: This could be taken from GetSystemInfo
484 (export (defun getpagesize () 4096))
487 ;; The docstrings are copied from the descriptions in SUSv3,
490 (define-protocol-class passwd alien-passwd ()
491 ((name :initarg :name :accessor passwd-name
492 :documentation "User's login name.")
493 ;; Note: SUSv3 doesn't require this member.
494 (passwd :initarg :passwd :accessor passwd-passwd
495 :documentation "The account's encrypted password.")
496 (uid :initarg :uid :accessor passwd-uid
497 :documentation "Numerical user ID.")
498 (gid :initarg :gid :accessor passwd-gid
499 :documentation "Numerical group ID.")
500 ;; Note: SUSv3 doesn't require this member.
501 (gecos :initarg :gecos :accessor passwd-gecos
502 :documentation "User's name or comment field.")
503 (dir :initarg :dir :accessor passwd-dir
504 :documentation "Initial working directory.")
505 (shell :initarg :shell :accessor passwd-shell
506 :documentation "Program to use as shell."))
507 (:documentation "Instances of this class represent entries in
508 the system's user database."))
510 (defmacro define-pw-call (name arg type)
512 ;; FIXME: this isn't the documented way of doing this, surely?
513 (let ((lisp-name (intern (string-upcase name) :sb-posix)))
515 (export ',lisp-name :sb-posix)
516 (declaim (inline ,lisp-name))
517 (defun ,lisp-name (,arg)
518 (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
521 (alien-to-passwd r)))))))
523 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
524 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
528 (define-protocol-class group alien-group ()
529 ((name :initarg :name :accessor group-name)
530 (passwd :initarg :passwd :accessor group-passwd)
531 (gid :initarg :gid :accessor group-gid)))
533 (defmacro define-gr-call (name arg type)
535 ;; FIXME: this isn't the documented way of doing this, surely?
536 (let ((lisp-name (intern (string-upcase name) :sb-posix)))
538 (export ',lisp-name :sb-posix)
539 (declaim (inline ,lisp-name))
540 (defun ,lisp-name (,arg)
541 (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
544 (alien-to-group r)))))))
546 (define-gr-call "getgrnam" login-name (function (* alien-group) c-string))
547 (define-gr-call "getgrgid" gid (function (* alien-group) gid-t))
551 (define-protocol-class timeval alien-timeval ()
552 ((sec :initarg :tv-sec :accessor timeval-sec
553 :documentation "Seconds.")
554 (usec :initarg :tv-usec :accessor timeval-usec
555 :documentation "Microseconds."))
556 (:documentation "Instances of this class represent time values."))
558 (define-protocol-class stat alien-stat ()
559 ((mode :initarg :mode :reader stat-mode
560 :documentation "Mode of file.")
561 (ino :initarg :ino :reader stat-ino
562 :documentation "File serial number.")
563 (dev :initarg :dev :reader stat-dev
564 :documentation "Device ID of device containing file.")
565 (nlink :initarg :nlink :reader stat-nlink
566 :documentation "Number of hard links to the file.")
567 (uid :initarg :uid :reader stat-uid
568 :documentation "User ID of file.")
569 (gid :initarg :gid :reader stat-gid
570 :documentation "Group ID of file.")
571 (size :initarg :size :reader stat-size
572 :documentation "For regular files, the file size in
573 bytes. For symbolic links, the length
574 in bytes of the filename contained in
576 (atime :initarg :atime :reader stat-atime
577 :documentation "Time of last access.")
578 (mtime :initarg :mtime :reader stat-mtime
579 :documentation "Time of last data modification.")
580 (ctime :initarg :ctime :reader stat-ctime
581 :documentation "Time of last status change"))
582 (:documentation "Instances of this class represent Posix file
585 (defmacro define-stat-call (name arg designator-fun type)
586 ;; FIXME: this isn't the documented way of doing this, surely?
587 (let ((lisp-name (lisp-for-c-symbol name)))
589 (export ',lisp-name :sb-posix)
590 (declaim (inline ,lisp-name))
591 (defun ,lisp-name (,arg &optional stat)
592 (declare (type (or null stat) stat))
593 (with-alien-stat a-stat ()
594 (let ((r (alien-funcall
595 (extern-alien ,(real-c-name (list name :options :largefile)) ,type)
596 (,designator-fun ,arg)
600 (alien-to-stat a-stat stat)))))))
602 (define-stat-call #-win32 "stat" #+win32 "_stat"
604 (function int c-string (* alien-stat)))
607 (define-stat-call "lstat"
609 (function int c-string (* alien-stat)))
610 ;;; No symbolic links on Windows, so use stat
613 (declaim (inline lstat))
614 (export (defun lstat (filename &optional stat)
615 (if stat (stat filename stat) (stat filename)))))
617 (define-stat-call #-win32 "fstat" #+win32 "_fstat"
619 (function int int (* alien-stat)))
623 (define-call "s_isreg" boolean never-fails (mode mode-t))
624 (define-call "s_isdir" boolean never-fails (mode mode-t))
625 (define-call "s_ischr" boolean never-fails (mode mode-t))
626 (define-call "s_isblk" boolean never-fails (mode mode-t))
627 (define-call "s_isfifo" boolean never-fails (mode mode-t))
628 (define-call "s_islnk" boolean never-fails (mode mode-t))
629 (define-call "s_issock" boolean never-fails (mode mode-t))
633 (export 'pipe :sb-posix)
634 (declaim (inline pipe))
635 (defun pipe (&optional filedes2)
636 (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
638 (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
639 (let ((r (sb-sys:with-pinned-objects (filedes2)
641 ;; FIXME: (* INT)? (ARRAY INT 2) would be better
642 (extern-alien "pipe" (function int (* int)))
643 (sb-sys:vector-sap filedes2)))))
646 (values (aref filedes2 0) (aref filedes2 1))))
649 (define-protocol-class termios alien-termios ()
650 ((iflag :initarg :iflag :accessor sb-posix:termios-iflag
651 :documentation "Input modes.")
652 (oflag :initarg :oflag :accessor sb-posix:termios-oflag
653 :documentation "Output modes.")
654 (cflag :initarg :cflag :accessor sb-posix:termios-cflag
655 :documentation "Control modes.")
656 (lflag :initarg :lflag :accessor sb-posix:termios-lflag
657 :documentation "Local modes.")
658 (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs
659 :documentation "Control characters"))
660 (:documentation "Instances of this class represent I/O
661 characteristics of the terminal."))
665 (export 'tcsetattr :sb-posix)
666 (declaim (inline tcsetattr))
667 (defun tcsetattr (fd actions termios)
668 (declare (type termios termios))
669 (with-alien-termios a-termios ()
670 (termios-to-alien termios a-termios)
671 (let ((fd (file-descriptor fd)))
672 (let* ((r (alien-funcall
675 (function int int int (* alien-termios)))
676 fd actions a-termios)))
680 (export 'tcgetattr :sb-posix)
681 (declaim (inline tcgetattr))
682 (defun tcgetattr (fd &optional termios)
683 (declare (type (or null termios) termios))
684 (with-alien-termios a-termios ()
685 (let ((r (alien-funcall
686 (extern-alien "tcgetattr"
687 (function int int (* alien-termios)))
692 (setf termios (alien-to-termios a-termios termios))))
694 (export 'cfsetispeed :sb-posix)
695 (declaim (inline cfsetispeed))
696 (defun cfsetispeed (speed &optional termios)
697 (declare (type (or null termios) termios))
698 (with-alien-termios a-termios ()
699 (let ((r (alien-funcall
700 (extern-alien "cfsetispeed"
701 (function int (* alien-termios) speed-t))
706 (setf termios (alien-to-termios a-termios termios))))
708 (export 'cfsetospeed :sb-posix)
709 (declaim (inline cfsetospeed))
710 (defun cfsetospeed (speed &optional termios)
711 (declare (type (or null termios) termios))
712 (with-alien-termios a-termios ()
713 (let ((r (alien-funcall
714 (extern-alien "cfsetospeed"
715 (function int (* alien-termios) speed-t))
720 (setf termios (alien-to-termios a-termios termios))))
722 (export 'cfgetispeed :sb-posix)
723 (declaim (inline cfgetispeed))
724 (defun cfgetispeed (termios)
725 (declare (type termios termios))
726 (with-alien-termios a-termios ()
727 (termios-to-alien termios a-termios)
728 (alien-funcall (extern-alien "cfgetispeed"
729 (function speed-t (* alien-termios)))
731 (export 'cfgetospeed :sb-posix)
732 (declaim (inline cfgetospeed))
733 (defun cfgetospeed (termios)
734 (declare (type termios termios))
735 (with-alien-termios a-termios ()
736 (termios-to-alien termios a-termios)
737 (alien-funcall (extern-alien "cfgetospeed"
738 (function speed-t (* alien-termios)))
744 (export 'time :sb-posix)
746 (let ((result (alien-funcall (extern-alien "time"
747 (function time-t (* time-t)))
752 (export 'utime :sb-posix)
753 (defun utime (filename &optional access-time modification-time)
754 (let ((fun (extern-alien "utime" (function int c-string
756 (name (filename filename)))
757 (if (not (and access-time modification-time))
758 (alien-funcall fun name nil)
759 (with-alien ((utimbuf (struct alien-utimbuf)))
760 (setf (slot utimbuf 'actime) (or access-time 0)
761 (slot utimbuf 'modtime) (or modification-time 0))
762 (let ((result (alien-funcall fun name (alien-sap utimbuf))))
766 (export 'utimes :sb-posix)
767 (defun utimes (filename &optional access-time modification-time)
768 (flet ((seconds-and-useconds (time)
769 (multiple-value-bind (integer fractional)
771 (values integer (cl:truncate (* fractional 1000000)))))
772 (maybe-syscall-error (value)
776 (let ((fun (extern-alien "utimes" (function int c-string
777 (* (array alien-timeval 2)))))
778 (name (filename filename)))
779 (if (not (and access-time modification-time))
780 (maybe-syscall-error (alien-funcall fun name nil))
781 (with-alien ((buf (array alien-timeval 2)))
782 (let ((actime (deref buf 0))
783 (modtime (deref buf 1)))
784 (setf (values (slot actime 'sec)
786 (seconds-and-useconds (or access-time 0))
787 (values (slot modtime 'sec)
788 (slot modtime 'usec))
789 (seconds-and-useconds (or modification-time 0)))
790 (maybe-syscall-error (alien-funcall fun name
791 (alien-sap buf))))))))))
796 (export 'getenv :sb-posix)
798 (let ((r (alien-funcall
799 (extern-alien "getenv" (function (* char) c-string))
801 (declare (type (alien (* char)) r))
802 (unless (null-alien r)
804 (define-call "putenv" int minusp (string c-string))
809 (export 'openlog :sb-posix)
810 (export 'syslog :sb-posix)
811 (export 'closelog :sb-posix)
812 (defun openlog (ident options &optional (facility log-user))
813 (alien-funcall (extern-alien
814 "openlog" (function void c-string int int))
815 ident options facility))
816 (defun syslog (priority format &rest args)
817 "Send a message to the syslog facility, with severity level
818 PRIORITY. The message will be formatted as by CL:FORMAT (rather
819 than C's printf) with format string FORMAT and arguments ARGS."
820 (flet ((syslog1 (priority message)
821 (alien-funcall (extern-alien
822 "syslog" (function void int c-string c-string))
823 priority "%s" message)))
824 (syslog1 priority (apply #'format nil format args))))
825 (define-call "closelog" void never-fails))