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 ;; KLUDGE: Splice out :ARRAY-LENGTH options (they're
12 ;; for the conversion functions, not for DEFCLASS).
13 for array-length-option = (member :array-length slotd)
14 collect (append (ldiff slotd array-length-option)
15 (cddr array-length-option)))
17 (declaim (inline ,to-alien ,to-protocol))
18 (declaim (inline ,to-protocol ,to-alien))
19 (defun ,to-protocol (alien &optional instance)
20 (declare (type (sb-alien:alien (* ,alien-type)) alien)
21 (type (or null ,name) instance))
23 (setf instance (make-instance ',name)))
24 ,@(loop for slotd in slots
25 ;; FIXME: slotds in source are more complicated in general
27 ;; FIXME: baroque construction of intricate fragility
28 for array-length = (getf (cdr slotd) :array-length)
31 (let ((array (make-array ,array-length)))
32 (setf (slot-value instance ',(car slotd))
34 (dotimes (i ,array-length)
37 (sb-alien:slot alien ',(car slotd))
40 collect `(setf (slot-value instance ',(car slotd))
41 (sb-alien:slot alien ',(car slotd))))
43 (defun ,to-alien (instance &optional alien)
44 (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
45 (type ,name instance))
47 (setf alien (sb-alien:make-alien ,alien-type)))
48 ,@(loop for slotd in slots
49 for array-length = (getf (cdr slotd) :array-length)
52 (let ((array (slot-value instance ',(car slotd))))
53 (dotimes (i ,array-length)
55 (sb-alien:slot alien ',(car slotd))
59 collect `(setf (sb-alien:slot alien ',(car slotd))
60 (slot-value instance ',(car slotd)))))
61 (find-class ',name))))
63 (define-condition sb-posix:syscall-error (error)
64 ((errno :initarg :errno :reader sb-posix:syscall-errno)
65 (name :initarg :name :initform nil :reader sb-posix:syscall-name))
66 (:report (lambda (c s)
67 (let ((errno (sb-posix:syscall-errno c))
68 (name (sb-posix:syscall-name c)))
70 (format s "Error in ~S: ~A (~A)"
72 (sb-int:strerror errno)
74 (format s "Error in syscall: ~A (~A)"
75 (sb-int:strerror errno)
78 (declaim (ftype (function (&optional symbol) nil) syscall-error))
79 (defun syscall-error (&optional name)
80 (error 'sb-posix:syscall-error
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 (declaim (inline never-fails))
93 (defun never-fails (&rest args)
94 (declare (ignore args))
97 ;;; Some systems may need C-level wrappers, which can live in the
98 ;;; runtime (so that save-lisp-and-die can produce standalone
99 ;;; executables). See REAL-C-NAME in macros.lisp for the use of this
101 (eval-when (:compile-toplevel :load-toplevel)
102 (setf *c-functions-in-runtime*
103 '`(#+netbsd ,@("stat" "lstat" "fstat" "readdir" "opendir"))))
106 ;;; filesystem access
107 (defmacro define-call* (name &rest arguments)
108 #-win32 `(define-call ,name ,@arguments)
109 #+win32 `(define-call ,(if (consp name)
110 `(,(concatenate 'string "_" (car name))
112 (concatenate 'string "_" name))
115 (define-call* "access" int minusp (pathname filename) (mode int))
116 (define-call* "chdir" int minusp (pathname filename))
117 (define-call* "chmod" int minusp (pathname filename) (mode mode-t))
118 (define-call* "close" int minusp (fd file-descriptor))
119 (define-call* "creat" int minusp (pathname filename) (mode mode-t))
120 (define-call* "dup" int minusp (oldfd file-descriptor))
121 (define-call* "dup2" int minusp (oldfd file-descriptor)
122 (newfd file-descriptor))
123 (define-call* ("lseek" :options :largefile)
124 off-t minusp (fd file-descriptor) (offset off-t)
126 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
129 (define-call-internally open-with-mode ,x int minusp
130 (pathname filename) (flags int) (mode mode-t))
131 (define-call-internally open-without-mode ,x int minusp
132 (pathname filename) (flags int))
133 (define-entry-point ,x
134 (pathname flags &optional (mode nil mode-supplied))
136 (open-with-mode pathname flags mode)
137 (open-without-mode pathname flags))))))
138 (def #-win32 "open" #+win32 "_open"))
139 (define-call* "read" int minusp
140 (fd file-descriptor) (buf (* t)) (count int))
141 (define-call "rename" int minusp (oldpath filename) (newpath filename))
142 (define-call* "rmdir" int minusp (pathname filename))
143 (define-call* "unlink" int minusp (pathname filename))
144 (define-call #-netbsd "opendir" #+netbsd "_opendir"
145 (* t) null-alien (pathname filename))
146 (define-call* "write" int minusp
147 (fd file-descriptor) (buf (* t)) (count int))
149 (define-call ("readdir" :c-name "readdir$INODE64" :options :largefile)
151 ;; readdir() has the worst error convention in the world. It's just
152 ;; too painful to support. (return is NULL _and_ errno "unchanged"
153 ;; is not an error, it's EOF).
157 (define-call (#-netbsd "readdir" #+netbsd "_readdir" :options :largefile)
159 ;; readdir() has the worst error convention in the world. It's just
160 ;; too painful to support. (return is NULL _and_ errno "unchanged"
161 ;; is not an error, it's EOF).
164 (define-call "closedir" int minusp (dir (* t)))
165 ;; need to do this here because we can't do it in the DEFPACKAGE
166 (define-call* "umask" mode-t never-fails (mode mode-t))
167 (define-call* "getpid" pid-t never-fails)
171 (define-call "chown" int minusp (pathname filename)
172 (owner uid-t) (group gid-t))
173 (define-call "chroot" int minusp (pathname filename))
174 (define-call "fchdir" int minusp (fd file-descriptor))
175 (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
176 (define-call "fchown" int minusp (fd file-descriptor)
177 (owner uid-t) (group gid-t))
178 (define-call "fdatasync" int minusp (fd file-descriptor))
179 (define-call ("ftruncate" :options :largefile)
180 int minusp (fd file-descriptor) (length off-t))
181 (define-call "fsync" int minusp (fd file-descriptor))
182 (define-call "lchown" int minusp (pathname filename)
183 (owner uid-t) (group gid-t))
184 (define-call "link" int minusp (oldpath filename) (newpath filename))
185 (define-call "lockf" int minusp (fd file-descriptor) (cmd int) (len off-t))
186 (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
187 (define-call "symlink" int minusp (oldpath filename) (newpath filename))
188 (define-call "sync" void never-fails)
189 (define-call ("truncate" :options :largefile)
190 int minusp (pathname filename) (length off-t))
192 (macrolet ((def-mk*temp (lisp-name c-name result-type errorp dirp values)
193 (declare (ignore dirp))
194 (if (sb-sys:find-foreign-symbol-address c-name)
196 (defun ,lisp-name (template)
197 (let* ((external-format sb-alien::*default-c-string-external-format*)
198 (arg (sb-ext:string-to-octets
200 :external-format external-format
202 (sb-sys:with-pinned-objects (arg)
203 ;; accommodate for the call-by-reference
204 ;; nature of mks/dtemp's template strings.
205 (let ((result (alien-funcall (extern-alien ,c-name
206 (function ,result-type system-area-pointer))
207 (sb-alien::vector-sap arg))))
208 (when (,errorp result)
209 (syscall-error ',lisp-name))
210 ;; FIXME: We'd rather return pathnames, but other
211 ;; SB-POSIX functions like this return strings...
212 (let ((pathname (sb-ext:octets-to-string
213 arg :external-format external-format
214 :end (1- (length arg)))))
216 '(values result pathname)
218 (export ',lisp-name))
220 (defun ,lisp-name (template)
221 (declare (ignore template))
222 (unsupported-error ',lisp-name ,c-name))
223 (define-compiler-macro ,lisp-name (&whole form template)
224 (declare (ignore template))
225 (unsupported-warning ',lisp-name ,c-name)
227 (export ',lisp-name)))))
228 (def-mk*temp mktemp "mktemp" (* char) null-alien nil nil)
229 ;; FIXME: Windows does have _mktemp, which has a slightly different
231 (def-mk*temp mkstemp "mkstemp" int minusp nil t)
232 ;; FIXME: What about Windows?
233 (def-mk*temp mkdtemp "mkdtemp" (* char) null-alien t nil))
234 (define-call-internally ioctl-without-arg "ioctl" int minusp
235 (fd file-descriptor) (cmd int))
236 (define-call-internally ioctl-with-int-arg "ioctl" int minusp
237 (fd file-descriptor) (cmd int) (arg int))
238 (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
239 (fd file-descriptor) (cmd int)
240 (arg alien-pointer-to-anything-or-nil))
241 (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
244 ((alien int) (ioctl-with-int-arg fd cmd arg))
245 ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
246 (ioctl-without-arg fd cmd)))
247 (define-call-internally fcntl-without-arg "fcntl" int minusp
248 (fd file-descriptor) (cmd int))
249 (define-call-internally fcntl-with-int-arg "fcntl" int minusp
250 (fd file-descriptor) (cmd int) (arg int))
251 (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
252 (fd file-descriptor) (cmd int)
253 (arg alien-pointer-to-anything-or-nil))
254 (define-protocol-class flock alien-flock ()
255 ((type :initarg :type :accessor flock-type
256 :documentation "Type of lock; F_RDLCK, F_WRLCK, F_UNLCK.")
257 (whence :initarg :whence :accessor flock-whence
258 :documentation "Flag for starting offset.")
259 (start :initarg :start :accessor flock-start
260 :documentation "Relative offset in bytes.")
261 (len :initarg :len :accessor flock-len
262 :documentation "Size; if 0 then until EOF.")
263 ;; Note: PID isn't initable, and is read-only. But other stuff in
264 ;; SB-POSIX right now loses when a protocol-class slot is unbound,
265 ;; so we initialize it to 0.
266 (pid :initform 0 :reader flock-pid
268 "Process ID of the process holding the lock; returned with F_GETLK."))
269 (:documentation "Class representing locks used in fcntl(2)."))
270 (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
273 ((alien int) (fcntl-with-int-arg fd cmd arg))
274 ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg))
275 (flock (with-alien-flock a-flock ()
276 (flock-to-alien arg a-flock)
277 (let ((r (fcntl-with-pointer-arg fd cmd a-flock)))
278 (alien-to-flock a-flock arg)
280 (fcntl-without-arg fd cmd)))
283 (define-call "geteuid" uid-t never-fails) ; "always successful", it says
285 (define-call "getresuid" uid-t never-fails)
286 (define-call "getuid" uid-t never-fails)
287 (define-call "seteuid" int minusp (uid uid-t))
289 (define-call "setfsuid" int minusp (uid uid-t))
290 (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
292 (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
293 (define-call "setuid" int minusp (uid uid-t))
294 (define-call "getegid" gid-t never-fails)
295 (define-call "getgid" gid-t never-fails)
297 (define-call "getresgid" gid-t never-fails)
298 (define-call "setegid" int minusp (gid gid-t))
300 (define-call "setfsgid" int minusp (gid gid-t))
301 (define-call "setgid" int minusp (gid gid-t))
302 (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
304 (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
306 ;; processes, signals
307 (define-call "alarm" int never-fails (seconds unsigned))
309 ;; exit and abort, not much point inlining these
310 (define-simple-call abort void)
311 (define-simple-call exit void (status int))
312 (define-simple-call _exit void (status int))
314 ;; FIXME this is a lie, of course this can fail, but there's no
315 ;; error handling here yet!
316 #+mach-exception-handler
317 (define-call "setup_mach_exceptions" void never-fails)
318 (define-call ("posix_fork" :c-name "fork") pid-t minusp)
320 "Forks the current process, returning 0 in the new process and the PID of
321 the child process in the parent. Forking while multiple threads are running is
324 (sb-thread::with-all-threads-lock
325 (when (cdr sb-thread::*all-threads*)
327 (let ((pid (posix-fork)))
328 #+mach-exception-handler
330 (setup-mach-exceptions))
331 (return-from fork pid)))
333 (error "Cannot fork with multiple threads running.")))
334 (export 'fork :sb-posix)
336 (define-call "getpgid" pid-t minusp (pid pid-t))
337 (define-call "getppid" pid-t never-fails)
338 (define-call "getpgrp" pid-t never-fails)
339 (define-call "getsid" pid-t minusp (pid pid-t))
340 (define-call "kill" int minusp (pid pid-t) (signal int))
341 (define-call "killpg" int minusp (pgrp int) (signal int))
342 (define-call "pause" int minusp)
343 (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
344 (define-call "setpgrp" int minusp)
345 (define-call "setsid" pid-t minusp))
347 (defmacro with-growing-c-string ((buffer size) &body body)
348 (sb-int:with-unique-names (c-string-block)
349 `(block ,c-string-block
351 (flet ((,buffer (&optional (size-incl-null))
353 (setf (sb-sys:sap-ref-8 (sb-alien:alien-sap ,buffer) size-incl-null)
355 (return-from ,c-string-block
356 (sb-alien::c-string-to-string
357 (sb-alien:alien-sap ,buffer)
358 (sb-impl::default-external-format)
360 (loop for ,size = 128 then (* 2 ,size)
363 (setf ,buffer (make-alien c-string ,size))
366 (free-alien ,buffer)))))))))
370 (export 'readlink :sb-posix)
371 (defun readlink (pathspec)
372 "Returns the resolved target of a symbolic link as a string."
373 (flet ((%readlink (path buf length)
375 (extern-alien "readlink" (function int (c-string :not-null t) (* t) int))
377 (with-growing-c-string (buf size)
378 (let ((count (%readlink (filename pathspec) buf size)))
379 (cond ((minusp count)
380 (syscall-error 'readlink))
385 (export 'getcwd :sb-posix)
387 "Returns the process's current working directory as a string."
388 (flet ((%getcwd (buffer size)
390 (extern-alien #-win32 "getcwd"
391 #+win32 "_getcwd" (function c-string (* t) int))
393 (with-growing-c-string (buf size)
394 (let ((result (%getcwd buf size)))
397 ((/= (get-errno) sb-posix:erange)
398 (syscall-error 'getcwd))))))))
402 (export 'wait :sb-posix)
403 (declaim (inline wait))
404 (defun wait (&optional statusptr)
405 (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
406 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
407 (pid (sb-sys:with-pinned-objects (ptr)
409 (extern-alien "wait" (function pid-t (* int)))
410 (sb-sys:vector-sap ptr)))))
412 (syscall-error 'wait)
413 (values pid (aref ptr 0))))))
417 (export 'waitpid :sb-posix)
418 (declaim (inline waitpid))
419 (defun waitpid (pid options &optional statusptr)
420 (declare (type (sb-alien:alien pid-t) pid)
421 (type (sb-alien:alien int) options)
422 (type (or null (simple-array (signed-byte 32) (1))) statusptr))
423 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
424 (pid (sb-sys:with-pinned-objects (ptr)
426 (extern-alien "waitpid" (function pid-t
428 pid (sb-sys:vector-sap ptr) options))))
430 (syscall-error 'waitpid)
431 (values pid (aref ptr 0)))))
433 (define-call "wifexited" boolean never-fails (status int))
434 (define-call "wexitstatus" int never-fails (status int))
435 (define-call "wifsignaled" boolean never-fails (status int))
436 (define-call "wtermsig" int never-fails (status int))
437 (define-call "wifstopped" boolean never-fails (status int))
438 (define-call "wstopsig" int never-fails (status int))
439 #+nil ; see alien/waitpid-macros.c
440 (define-call "wifcontinued" boolean never-fails (status int)))
445 (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
447 (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
448 (addr sap-or-nil) (length unsigned) (prot unsigned)
449 (flags unsigned) (fd file-descriptor) (offset off-t))
451 (define-call "munmap" int minusp
452 (start sb-sys:system-area-pointer) (length unsigned))
455 (define-call "msync" int minusp
456 (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
459 ;; No attempt is made to offer a full mmap-like interface on Windows.
460 ;; It would be possible to do so (and has been done by AK on his
461 ;; branch), but the use case is unclear to me. However, the following
462 ;; definitions are needed to keep existing code in sb-simple-streams
464 (defconstant PROT-READ #x02)
465 (defconstant PROT-WRITE #x04)
466 (defconstant PROT-EXEC #x10)
467 (defconstant PROT-NONE 0)
468 (defconstant MAP-SHARED 0)
469 (defconstant MAP-PRIVATE 1)
470 (defconstant MS-ASYNC nil)
471 (defconstant MS-SYNC nil)
472 (export ;export on the fly like define-call
473 (defun msync (address length flags)
474 (declare (ignore flags))
475 (when (zerop (sb-win32:flush-view-of-file address length))
476 (sb-win32::win32-error "FlushViewOfFile")))))
478 ;;; mlockall, munlockall
479 (define-call "mlockall" int minusp (flags int))
480 (define-call "munlockall" int minusp)
483 (define-call "getpagesize" int minusp)
485 ;;; KLUDGE: This could be taken from GetSystemInfo
486 (export (defun getpagesize () 4096))
489 ;; The docstrings are copied from the descriptions in SUSv3,
492 (define-protocol-class passwd alien-passwd ()
493 ((name :initarg :name :accessor passwd-name
494 :documentation "User's login name.")
495 ;; Note: SUSv3 doesn't require this member.
496 (passwd :initarg :passwd :accessor passwd-passwd
497 :documentation "The account's encrypted password.")
498 (uid :initarg :uid :accessor passwd-uid
499 :documentation "Numerical user ID.")
500 (gid :initarg :gid :accessor passwd-gid
501 :documentation "Numerical group ID.")
502 ;; Note: SUSv3 doesn't require this member.
503 (gecos :initarg :gecos :accessor passwd-gecos
504 :documentation "User's name or comment field.")
505 (dir :initarg :dir :accessor passwd-dir
506 :documentation "Initial working directory.")
507 (shell :initarg :shell :accessor passwd-shell
508 :documentation "Program to use as shell."))
509 (:documentation "Instances of this class represent entries in
510 the system's user database."))
514 (define-protocol-class group alien-group ()
515 ((name :initarg :name :accessor group-name)
516 (passwd :initarg :passwd :accessor group-passwd)
517 (gid :initarg :gid :accessor group-gid)))
519 (defmacro define-obj-call (name arg type conv)
521 ;; FIXME: this isn't the documented way of doing this, surely?
522 (let ((lisp-name (intern (string-upcase name) :sb-posix)))
524 (export ',lisp-name :sb-posix)
525 (declaim (inline ,lisp-name))
526 (defun ,lisp-name (,arg)
527 (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
532 (define-obj-call "getpwnam" login-name (function (* alien-passwd) (c-string :not-null t))
534 (define-obj-call "getpwuid" uid (function (* alien-passwd) uid-t)
536 (define-obj-call "getgrnam" login-name (function (* alien-group) (c-string :not-null t))
538 (define-obj-call "getgrgid" gid (function (* alien-group) gid-t)
543 (define-protocol-class timeval alien-timeval ()
544 ((sec :initarg :tv-sec :accessor timeval-sec
545 :documentation "Seconds.")
546 (usec :initarg :tv-usec :accessor timeval-usec
547 :documentation "Microseconds."))
548 (:documentation "Instances of this class represent time values."))
550 (define-protocol-class stat alien-stat ()
551 ((mode :initarg :mode :reader stat-mode
552 :documentation "Mode of file.")
553 (ino :initarg :ino :reader stat-ino
554 :documentation "File serial number.")
555 (dev :initarg :dev :reader stat-dev
556 :documentation "Device ID of device containing file.")
557 (nlink :initarg :nlink :reader stat-nlink
558 :documentation "Number of hard links to the file.")
559 (uid :initarg :uid :reader stat-uid
560 :documentation "User ID of file.")
561 (gid :initarg :gid :reader stat-gid
562 :documentation "Group ID of file.")
563 (size :initarg :size :reader stat-size
564 :documentation "For regular files, the file size in
565 bytes. For symbolic links, the length
566 in bytes of the filename contained in
568 (rdev :initarg :rdev :reader stat-rdev
569 :documentation "For devices the device number.")
570 (atime :initarg :atime :reader stat-atime
571 :documentation "Time of last access.")
572 (mtime :initarg :mtime :reader stat-mtime
573 :documentation "Time of last data modification.")
574 (ctime :initarg :ctime :reader stat-ctime
575 :documentation "Time of last status change"))
576 (:documentation "Instances of this class represent Posix file
579 (defmacro define-stat-call (name arg designator-fun type)
580 ;; FIXME: this isn't the documented way of doing this, surely?
581 (let ((lisp-name (lisp-for-c-symbol name))
582 (real-name #+inode64 (format nil "~A$INODE64" name)
585 (export ',lisp-name :sb-posix)
586 (declaim (inline ,lisp-name))
587 (defun ,lisp-name (,arg &optional stat)
588 (declare (type (or null stat) stat))
589 (with-alien-stat a-stat ()
590 (let ((r (alien-funcall
591 (extern-alien ,(real-c-name (list real-name :options :largefile)) ,type)
592 (,designator-fun ,arg)
595 (syscall-error ',lisp-name))
596 (alien-to-stat a-stat stat)))))))
598 (define-stat-call #-win32 "stat" #+win32 "_stat"
600 (function int (c-string :not-null t) (* alien-stat)))
603 (define-stat-call "lstat"
605 (function int (c-string :not-null t) (* alien-stat)))
606 ;;; No symbolic links on Windows, so use stat
609 (declaim (inline lstat))
610 (export (defun lstat (filename &optional stat)
611 (if stat (stat filename stat) (stat filename)))))
613 (define-stat-call #-win32 "fstat" #+win32 "_fstat"
615 (function int int (* alien-stat)))
619 (define-call "s_isreg" boolean never-fails (mode mode-t))
620 (define-call "s_isdir" boolean never-fails (mode mode-t))
621 (define-call "s_ischr" boolean never-fails (mode mode-t))
622 (define-call "s_isblk" boolean never-fails (mode mode-t))
623 (define-call "s_isfifo" boolean never-fails (mode mode-t))
624 (define-call "s_islnk" boolean never-fails (mode mode-t))
625 (define-call "s_issock" boolean never-fails (mode mode-t))
629 (export 'pipe :sb-posix)
630 (declaim (inline pipe))
631 (defun pipe (&optional filedes2)
632 (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
634 (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
635 (let ((r (sb-sys:with-pinned-objects (filedes2)
637 ;; FIXME: (* INT)? (ARRAY INT 2) would be better
638 (extern-alien "pipe" (function int (* int)))
639 (sb-sys:vector-sap filedes2)))))
641 (syscall-error 'pipe)))
642 (values (aref filedes2 0) (aref filedes2 1))))
645 (define-protocol-class termios alien-termios ()
646 ((iflag :initarg :iflag :accessor sb-posix:termios-iflag
647 :documentation "Input modes.")
648 (oflag :initarg :oflag :accessor sb-posix:termios-oflag
649 :documentation "Output modes.")
650 (cflag :initarg :cflag :accessor sb-posix:termios-cflag
651 :documentation "Control modes.")
652 (lflag :initarg :lflag :accessor sb-posix:termios-lflag
653 :documentation "Local modes.")
654 (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs
655 :documentation "Control characters."))
656 (:documentation "Instances of this class represent I/O
657 characteristics of the terminal."))
661 (export 'tcsetattr :sb-posix)
662 (declaim (inline tcsetattr))
663 (defun tcsetattr (fd actions termios)
664 (declare (type termios termios))
665 (with-alien-termios a-termios ()
666 (termios-to-alien termios a-termios)
667 (let ((fd (file-descriptor fd)))
668 (let* ((r (alien-funcall
671 (function int int int (* alien-termios)))
672 fd actions a-termios)))
674 (syscall-error 'tcsetattr)))
676 (export 'tcgetattr :sb-posix)
677 (declaim (inline tcgetattr))
678 (defun tcgetattr (fd &optional termios)
679 (declare (type (or null termios) termios))
680 (with-alien-termios a-termios ()
681 (let ((r (alien-funcall
682 (extern-alien "tcgetattr"
683 (function int int (* alien-termios)))
687 (syscall-error 'tcgetattr))
688 (setf termios (alien-to-termios a-termios termios))))
690 (define-call "tcdrain" int minusp (fd file-descriptor))
691 (define-call "tcflow" int minusp (fd file-descriptor) (action int))
692 (define-call "tcflush" int minusp (fd file-descriptor) (queue-selector int))
693 (define-call "tcgetsid" pid-t minusp (fd file-descriptor))
694 (define-call "tcsendbreak" int minusp (fd file-descriptor) (duration int))
695 (export 'cfsetispeed :sb-posix)
696 (declaim (inline cfsetispeed))
697 (defun cfsetispeed (speed &optional termios)
698 (declare (type (or null termios) termios))
699 (with-alien-termios a-termios ()
700 (let ((r (alien-funcall
701 (extern-alien "cfsetispeed"
702 (function int (* alien-termios) speed-t))
706 (syscall-error 'cfsetispeed))
707 (setf termios (alien-to-termios a-termios termios))))
709 (export 'cfsetospeed :sb-posix)
710 (declaim (inline cfsetospeed))
711 (defun cfsetospeed (speed &optional termios)
712 (declare (type (or null termios) termios))
713 (with-alien-termios a-termios ()
714 (let ((r (alien-funcall
715 (extern-alien "cfsetospeed"
716 (function int (* alien-termios) speed-t))
720 (syscall-error 'cfsetospeed))
721 (setf termios (alien-to-termios a-termios termios))))
723 (export 'cfgetispeed :sb-posix)
724 (declaim (inline cfgetispeed))
725 (defun cfgetispeed (termios)
726 (declare (type termios termios))
727 (with-alien-termios a-termios ()
728 (termios-to-alien termios a-termios)
729 (alien-funcall (extern-alien "cfgetispeed"
730 (function speed-t (* alien-termios)))
732 (export 'cfgetospeed :sb-posix)
733 (declaim (inline cfgetospeed))
734 (defun cfgetospeed (termios)
735 (declare (type termios termios))
736 (with-alien-termios a-termios ()
737 (termios-to-alien termios a-termios)
738 (alien-funcall (extern-alien "cfgetospeed"
739 (function speed-t (* alien-termios)))
745 (export 'time :sb-posix)
747 (let ((result (alien-funcall (extern-alien "time"
748 (function time-t (* time-t)))
751 (syscall-error 'time)
753 (export 'utime :sb-posix)
754 (defun utime (filename &optional access-time modification-time)
755 (let ((fun (extern-alien #-netbsd "utime" #+netbsd "_utime"
756 (function int (c-string :not-null t)
758 (name (filename filename)))
759 (if (not (and access-time modification-time))
760 (alien-funcall fun name nil)
761 (with-alien ((utimbuf (struct alien-utimbuf)))
762 (setf (slot utimbuf 'actime) (or access-time 0)
763 (slot utimbuf 'modtime) (or modification-time 0))
764 (let ((result (alien-funcall fun name (alien-sap utimbuf))))
766 (syscall-error 'utime)
768 (export 'utimes :sb-posix)
769 (defun utimes (filename &optional access-time modification-time)
770 (flet ((seconds-and-useconds (time)
771 (multiple-value-bind (integer fractional)
773 (values integer (cl:truncate (* fractional 1000000)))))
774 (maybe-syscall-error (value)
776 (syscall-error 'utimes)
778 (let ((fun (extern-alien "utimes" (function int (c-string :not-null t)
779 (* (array alien-timeval 2)))))
780 (name (filename filename)))
781 (if (not (and access-time modification-time))
782 (maybe-syscall-error (alien-funcall fun name nil))
783 (with-alien ((buf (array alien-timeval 2)))
784 (let ((actime (deref buf 0))
785 (modtime (deref buf 1)))
786 (setf (values (slot actime 'sec)
788 (seconds-and-useconds (or access-time 0))
789 (values (slot modtime 'sec)
790 (slot modtime 'usec))
791 (seconds-and-useconds (or modification-time 0)))
792 (maybe-syscall-error (alien-funcall fun name
793 (alien-sap buf))))))))))
798 (eval-when (:compile-toplevel :load-toplevel)
799 ;; Do this at compile-time as Win32 code below refers to it as
801 (export 'getenv :sb-posix))
803 (let ((r (alien-funcall
804 (extern-alien "getenv" (function (* char) (c-string :not-null t)))
806 (declare (type (alien (* char)) r))
807 (unless (null-alien r)
811 (define-call "setenv" int minusp
812 (name (c-string :not-null t))
813 (value (c-string :not-null t))
815 (define-call "unsetenv" int minusp (name (c-string :not-null t)))
816 (export 'putenv :sb-posix)
817 (defun putenv (string)
818 (declare (string string))
819 ;; We don't want to call actual putenv: the string passed to putenv ends
820 ;; up in environ, and we any string we allocate GC might move.
822 ;; This makes our wrapper nonconformant if you squit hard enough, but
823 ;; users who care about that should really be calling putenv() directly in
824 ;; order to be able to manage memory sanely.
825 (let ((p (position #\= string))
829 (unsetenv (subseq string 0 p))
830 (setenv (subseq string 0 p) (subseq string (1+ p)) 1))
831 (error "Invalid argument to putenv: ~S" string)))))
834 ;; Windows doesn't define a POSIX setenv, but happily their _putenv is sane.
835 (define-call* "putenv" int minusp (string (c-string :not-null t)))
836 (export 'setenv :sb-posix)
837 (defun setenv (name value overwrite)
838 (declare (string name value))
839 (if (and (zerop overwrite) (sb-posix:getenv name))
841 (putenv (concatenate 'string name "=" value))))
842 (export 'unsetenv :sb-posix)
843 (defun unsetenv (name)
844 (declare (string name))
845 (putenv (concatenate 'string name "="))))
850 (export 'openlog :sb-posix)
851 (export 'syslog :sb-posix)
852 (export 'closelog :sb-posix)
853 (defun openlog (ident options &optional (facility log-user))
854 (alien-funcall (extern-alien
855 "openlog" (function void (c-string :not-null t) int int))
856 ident options facility))
857 (defun syslog (priority format &rest args)
858 "Send a message to the syslog facility, with severity level
859 PRIORITY. The message will be formatted as by CL:FORMAT (rather
860 than C's printf) with format string FORMAT and arguments ARGS."
861 (flet ((syslog1 (priority message)
862 (alien-funcall (extern-alien
863 "syslog" (function void int
864 (c-string :not-null t)
865 (c-string :not-null t)))
866 priority "%s" message)))
867 (syslog1 priority (apply #'format nil format args))))
868 (define-call "closelog" void never-fails))