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))))))
66 (defun syscall-error ()
67 (error 'sb-posix:syscall-error :errno (get-errno)))
69 (defun unsupported-error (lisp-name c-name)
70 (error "~S is unsupported by SBCL on this platform due to lack of ~A()."
73 (defun unsupported-warning (lisp-name c-name)
74 (warn "~S is unsupported by SBCL on this platform due to lack of ~A()."
77 (declaim (inline never-fails))
78 (defun never-fails (&rest args)
79 (declare (ignore args))
82 ;;; Some systems may need C-level wrappers, which can live in the
83 ;;; runtime (so that save-lisp-and-die can produce standalone
84 ;;; executables). See REAL-C-NAME in macros.lisp for the use of this
86 (eval-when (:compile-toplevel :load-toplevel)
87 (setf *c-functions-in-runtime*
88 '`(#+netbsd ,@("stat" "lstat" "fstat" "readdir" "opendir"))))
92 (defmacro define-call* (name &rest arguments)
93 #-win32 `(define-call ,name ,@arguments)
94 #+win32 `(define-call ,(if (consp name)
95 `(,(concatenate 'string "_" (car name))
97 (concatenate 'string "_" name))
100 (define-call* "access" int minusp (pathname filename) (mode int))
101 (define-call* "chdir" int minusp (pathname filename))
102 (define-call* "chmod" int minusp (pathname filename) (mode mode-t))
103 (define-call* "close" int minusp (fd file-descriptor))
104 (define-call* "creat" int minusp (pathname filename) (mode mode-t))
105 (define-call* "dup" int minusp (oldfd file-descriptor))
106 (define-call* "dup2" int minusp (oldfd file-descriptor)
107 (newfd file-descriptor))
108 (define-call* ("lseek" :options :largefile)
109 off-t minusp (fd file-descriptor) (offset off-t)
111 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
114 (define-call-internally open-with-mode ,x int minusp
115 (pathname filename) (flags int) (mode mode-t))
116 (define-call-internally open-without-mode ,x int minusp
117 (pathname filename) (flags int))
118 (define-entry-point ,x
119 (pathname flags &optional (mode nil mode-supplied))
121 (open-with-mode pathname flags mode)
122 (open-without-mode pathname flags))))))
123 (def #-win32 "open" #+win32 "_open"))
124 (define-call "rename" int minusp (oldpath filename) (newpath filename))
125 (define-call* "rmdir" int minusp (pathname filename))
126 (define-call* "unlink" int minusp (pathname filename))
127 (define-call #-netbsd "opendir" #+netbsd "_opendir"
128 (* t) null-alien (pathname filename))
130 (define-call ("readdir" :c-name "readdir$INODE64" :options :largefile)
132 ;; readdir() has the worst error convention in the world. It's just
133 ;; too painful to support. (return is NULL _and_ errno "unchanged"
134 ;; is not an error, it's EOF).
138 (define-call (#-netbsd "readdir" #+netbsd "_readdir" :options :largefile)
140 ;; readdir() has the worst error convention in the world. It's just
141 ;; too painful to support. (return is NULL _and_ errno "unchanged"
142 ;; is not an error, it's EOF).
145 (define-call "closedir" int minusp (dir (* t)))
146 ;; need to do this here because we can't do it in the DEFPACKAGE
147 (define-call* "umask" mode-t never-fails (mode mode-t))
148 (define-call* "getpid" pid-t never-fails)
152 (define-call "chown" int minusp (pathname filename)
153 (owner uid-t) (group gid-t))
154 (define-call "chroot" int minusp (pathname filename))
155 (define-call "fchdir" int minusp (fd file-descriptor))
156 (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
157 (define-call "fchown" int minusp (fd file-descriptor)
158 (owner uid-t) (group gid-t))
159 (define-call "fdatasync" int minusp (fd file-descriptor))
160 (define-call ("ftruncate" :options :largefile)
161 int minusp (fd file-descriptor) (length off-t))
162 (define-call "fsync" int minusp (fd file-descriptor))
163 (define-call "lchown" int minusp (pathname filename)
164 (owner uid-t) (group gid-t))
165 (define-call "link" int minusp (oldpath filename) (newpath filename))
166 (define-call "lockf" int minusp (fd file-descriptor) (cmd int) (len off-t))
167 (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
168 (define-call "symlink" int minusp (oldpath filename) (newpath filename))
169 (define-call "sync" void never-fails)
170 (define-call ("truncate" :options :largefile)
171 int minusp (pathname filename) (length off-t))
173 (macrolet ((def-mk*temp (lisp-name c-name result-type errorp dirp values)
174 (declare (ignore dirp))
175 (if (sb-sys:find-foreign-symbol-address c-name)
177 (defun ,lisp-name (template)
178 (let* ((external-format sb-alien::*default-c-string-external-format*)
179 (arg (sb-ext:string-to-octets
181 :external-format external-format
183 (sb-sys:with-pinned-objects (arg)
184 ;; accommodate for the call-by-reference
185 ;; nature of mks/dtemp's template strings.
186 (let ((result (alien-funcall (extern-alien ,c-name
187 (function ,result-type system-area-pointer))
188 (sb-alien::vector-sap arg))))
189 (when (,errorp result)
191 ;; FIXME: We'd rather return pathnames, but other
192 ;; SB-POSIX functions like this return strings...
193 (let ((pathname (sb-ext:octets-to-string
194 arg :external-format external-format
195 :end (1- (length arg)))))
197 '(values result pathname)
199 (export ',lisp-name))
201 (defun ,lisp-name (template)
202 (declare (ignore template))
203 (unsupported-error ',lisp-name ,c-name))
204 (define-compiler-macro ,lisp-name (&whole form template)
205 (declare (ignore template))
206 (unsupported-warning ',lisp-name ,c-name)
208 (export ',lisp-name)))))
209 (def-mk*temp mktemp "mktemp" (* char) null-alien nil nil)
210 ;; FIXME: Windows does have _mktemp, which has a slightly different
212 (def-mk*temp mkstemp "mkstemp" int minusp nil t)
213 ;; FIXME: What about Windows?
214 (def-mk*temp mkdtemp "mkdtemp" (* char) null-alien t nil))
215 (define-call-internally ioctl-without-arg "ioctl" int minusp
216 (fd file-descriptor) (cmd int))
217 (define-call-internally ioctl-with-int-arg "ioctl" int minusp
218 (fd file-descriptor) (cmd int) (arg int))
219 (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
220 (fd file-descriptor) (cmd int)
221 (arg alien-pointer-to-anything-or-nil))
222 (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
225 ((alien int) (ioctl-with-int-arg fd cmd arg))
226 ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
227 (ioctl-without-arg fd cmd)))
228 (define-call-internally fcntl-without-arg "fcntl" int minusp
229 (fd file-descriptor) (cmd int))
230 (define-call-internally fcntl-with-int-arg "fcntl" int minusp
231 (fd file-descriptor) (cmd int) (arg int))
232 (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
233 (fd file-descriptor) (cmd int)
234 (arg alien-pointer-to-anything-or-nil))
235 (define-protocol-class flock alien-flock ()
236 ((type :initarg :type :accessor flock-type
237 :documentation "Type of lock; F_RDLCK, F_WRLCK, F_UNLCK.")
238 (whence :initarg :whence :accessor flock-whence
239 :documentation "Flag for starting offset.")
240 (start :initarg :start :accessor flock-start
241 :documentation "Relative offset in bytes.")
242 (len :initarg :len :accessor flock-len
243 :documentation "Size; if 0 then until EOF.")
244 ;; Note: PID isn't initable, and is read-only. But other stuff in
245 ;; SB-POSIX right now loses when a protocol-class slot is unbound,
246 ;; so we initialize it to 0.
247 (pid :initform 0 :reader flock-pid
249 "Process ID of the process holding the lock; returned with F_GETLK."))
250 (:documentation "Class representing locks used in fcntl(2)."))
251 (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
254 ((alien int) (fcntl-with-int-arg fd cmd arg))
255 ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg))
256 (flock (with-alien-flock a-flock ()
257 (flock-to-alien arg a-flock)
258 (let ((r (fcntl-with-pointer-arg fd cmd a-flock)))
259 (alien-to-flock a-flock arg)
261 (fcntl-without-arg fd cmd)))
264 (define-call "geteuid" uid-t never-fails) ; "always successful", it says
265 #-sunos (define-call "getresuid" uid-t never-fails)
266 (define-call "getuid" uid-t never-fails)
267 (define-call "seteuid" int minusp (uid uid-t))
268 #-sunos (define-call "setfsuid" int minusp (uid uid-t))
269 (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
270 #-sunos (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
271 (define-call "setuid" int minusp (uid uid-t))
272 (define-call "getegid" gid-t never-fails)
273 (define-call "getgid" gid-t never-fails)
274 #-sunos (define-call "getresgid" gid-t never-fails)
275 (define-call "setegid" int minusp (gid gid-t))
276 #-sunos (define-call "setfsgid" int minusp (gid gid-t))
277 (define-call "setgid" int minusp (gid gid-t))
278 (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
279 #-sunos (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
281 ;; processes, signals
282 (define-call "alarm" int never-fails (seconds unsigned))
286 ;; FIXME this is a lie, of course this can fail, but there's no
287 ;; error handling here yet!
288 #+mach-exception-handler
289 (define-call "setup_mach_exceptions" void never-fails)
290 (define-call ("posix_fork" :c-name "fork") pid-t minusp)
292 "Forks the current process, returning 0 in the new process and the PID of
293 the child process in the parent. Forking while multiple threads are running is
296 (sb-thread::with-all-threads-lock
297 (when (cdr sb-thread::*all-threads*)
299 (let ((pid (posix-fork)))
300 #+mach-exception-handler
302 (setup-mach-exceptions))
303 (return-from fork pid)))
305 (error "Cannot fork with multiple threads running.")))
306 (export 'fork :sb-posix)
308 (define-call "getpgid" pid-t minusp (pid pid-t))
309 (define-call "getppid" pid-t never-fails)
310 (define-call "getpgrp" pid-t never-fails)
311 (define-call "getsid" pid-t minusp (pid pid-t))
312 (define-call "kill" int minusp (pid pid-t) (signal int))
313 (define-call "killpg" int minusp (pgrp int) (signal int))
314 (define-call "pause" int minusp)
315 (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
316 (define-call "setpgrp" int minusp)
317 (define-call "setsid" pid-t minusp))
319 (defmacro with-growing-c-string ((buffer size) &body body)
320 (sb-int:with-unique-names (c-string-block)
321 `(block ,c-string-block
323 (flet ((,buffer (&optional (size-incl-null))
325 (setf (sb-sys:sap-ref-8 (sb-alien:alien-sap ,buffer) size-incl-null)
327 (return-from ,c-string-block
328 (sb-alien::c-string-to-string
329 (sb-alien:alien-sap ,buffer)
330 (sb-impl::default-external-format)
332 (loop for ,size = 128 then (* 2 ,size)
335 (setf ,buffer (make-alien c-string ,size))
338 (free-alien ,buffer)))))))))
342 (export 'readlink :sb-posix)
343 (defun readlink (pathspec)
344 "Returns the resolved target of a symbolic link as a string."
345 (flet ((%readlink (path buf length)
347 (extern-alien "readlink" (function int c-string (* t) int))
349 (with-growing-c-string (buf size)
350 (let ((count (%readlink (filename pathspec) buf size)))
351 (cond ((minusp count)
357 (export 'getcwd :sb-posix)
359 "Returns the process's current working directory as a string."
360 (flet ((%getcwd (buffer size)
362 (extern-alien #-win32 "getcwd"
363 #+win32 "_getcwd" (function c-string (* t) int))
365 (with-growing-c-string (buf size)
366 (let ((result (%getcwd buf size)))
369 ((/= (get-errno) sb-posix:erange)
370 (syscall-error))))))))
374 (export 'wait :sb-posix)
375 (declaim (inline wait))
376 (defun wait (&optional statusptr)
377 (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
378 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
379 (pid (sb-sys:with-pinned-objects (ptr)
381 (extern-alien "wait" (function pid-t (* int)))
382 (sb-sys:vector-sap ptr)))))
385 (values pid (aref ptr 0))))))
389 (export 'waitpid :sb-posix)
390 (declaim (inline waitpid))
391 (defun waitpid (pid options &optional statusptr)
392 (declare (type (sb-alien:alien pid-t) pid)
393 (type (sb-alien:alien int) options)
394 (type (or null (simple-array (signed-byte 32) (1))) statusptr))
395 (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
396 (pid (sb-sys:with-pinned-objects (ptr)
398 (extern-alien "waitpid" (function pid-t
400 pid (sb-sys:vector-sap ptr) options))))
403 (values pid (aref ptr 0)))))
405 (define-call "wifexited" boolean never-fails (status int))
406 (define-call "wexitstatus" int never-fails (status int))
407 (define-call "wifsignaled" boolean never-fails (status int))
408 (define-call "wtermsig" int never-fails (status int))
409 (define-call "wifstopped" boolean never-fails (status int))
410 (define-call "wstopsig" int never-fails (status int))
411 #+nil ; see alien/waitpid-macros.c
412 (define-call "wifcontinued" boolean never-fails (status int)))
417 (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
419 (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
420 (addr sap-or-nil) (length unsigned) (prot unsigned)
421 (flags unsigned) (fd file-descriptor) (offset off-t))
423 (define-call "munmap" int minusp
424 (start sb-sys:system-area-pointer) (length unsigned))
426 (define-call "msync" int minusp
427 (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
429 ;;; mlockall, munlockall
430 (define-call "mlockall" int minusp (flags int))
431 (define-call "munlockall" int minusp)
434 (define-call "getpagesize" int minusp)
436 ;;; KLUDGE: This could be taken from GetSystemInfo
437 (export (defun getpagesize () 4096))
440 ;; The docstrings are copied from the descriptions in SUSv3,
443 (define-protocol-class passwd alien-passwd ()
444 ((name :initarg :name :accessor passwd-name
445 :documentation "User's login name.")
446 ;; Note: SUSv3 doesn't require this member.
447 (passwd :initarg :passwd :accessor passwd-passwd
448 :documentation "The account's encrypted password.")
449 (uid :initarg :uid :accessor passwd-uid
450 :documentation "Numerical user ID.")
451 (gid :initarg :gid :accessor passwd-gid
452 :documentation "Numerical group ID.")
453 ;; Note: SUSv3 doesn't require this member.
454 (gecos :initarg :gecos :accessor passwd-gecos
455 :documentation "User's name or comment field.")
456 (dir :initarg :dir :accessor passwd-dir
457 :documentation "Initial working directory.")
458 (shell :initarg :shell :accessor passwd-shell
459 :documentation "Program to use as shell."))
460 (:documentation "Instances of this class represent entries in
461 the system's user database."))
465 (define-protocol-class group alien-group ()
466 ((name :initarg :name :accessor group-name)
467 (passwd :initarg :passwd :accessor group-passwd)
468 (gid :initarg :gid :accessor group-gid)))
470 (defmacro define-obj-call (name arg type conv)
472 ;; FIXME: this isn't the documented way of doing this, surely?
473 (let ((lisp-name (intern (string-upcase name) :sb-posix)))
475 (export ',lisp-name :sb-posix)
476 (declaim (inline ,lisp-name))
477 (defun ,lisp-name (,arg)
478 (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
483 (define-obj-call "getpwnam" login-name (function (* alien-passwd) c-string) alien-to-passwd)
484 (define-obj-call "getpwuid" uid (function (* alien-passwd) uid-t) alien-to-passwd)
485 (define-obj-call "getgrnam" login-name (function (* alien-group) c-string) alien-to-group)
486 (define-obj-call "getgrgid" gid (function (* alien-group) gid-t) alien-to-group)
490 (define-protocol-class timeval alien-timeval ()
491 ((sec :initarg :tv-sec :accessor timeval-sec
492 :documentation "Seconds.")
493 (usec :initarg :tv-usec :accessor timeval-usec
494 :documentation "Microseconds."))
495 (:documentation "Instances of this class represent time values."))
497 (define-protocol-class stat alien-stat ()
498 ((mode :initarg :mode :reader stat-mode
499 :documentation "Mode of file.")
500 (ino :initarg :ino :reader stat-ino
501 :documentation "File serial number.")
502 (dev :initarg :dev :reader stat-dev
503 :documentation "Device ID of device containing file.")
504 (nlink :initarg :nlink :reader stat-nlink
505 :documentation "Number of hard links to the file.")
506 (uid :initarg :uid :reader stat-uid
507 :documentation "User ID of file.")
508 (gid :initarg :gid :reader stat-gid
509 :documentation "Group ID of file.")
510 (size :initarg :size :reader stat-size
511 :documentation "For regular files, the file size in
512 bytes. For symbolic links, the length
513 in bytes of the filename contained in
515 (atime :initarg :atime :reader stat-atime
516 :documentation "Time of last access.")
517 (mtime :initarg :mtime :reader stat-mtime
518 :documentation "Time of last data modification.")
519 (ctime :initarg :ctime :reader stat-ctime
520 :documentation "Time of last status change"))
521 (:documentation "Instances of this class represent Posix file
524 (defmacro define-stat-call (name arg designator-fun type)
525 ;; FIXME: this isn't the documented way of doing this, surely?
526 (let ((lisp-name (lisp-for-c-symbol name))
527 (real-name #+inode64 (format nil "~A$INODE64" name)
530 (export ',lisp-name :sb-posix)
531 (declaim (inline ,lisp-name))
532 (defun ,lisp-name (,arg &optional stat)
533 (declare (type (or null stat) stat))
534 (with-alien-stat a-stat ()
535 (let ((r (alien-funcall
536 (extern-alien ,(real-c-name (list real-name :options :largefile)) ,type)
537 (,designator-fun ,arg)
541 (alien-to-stat a-stat stat)))))))
543 (define-stat-call #-win32 "stat" #+win32 "_stat"
545 (function int c-string (* alien-stat)))
548 (define-stat-call "lstat"
550 (function int c-string (* alien-stat)))
551 ;;; No symbolic links on Windows, so use stat
554 (declaim (inline lstat))
555 (export (defun lstat (filename &optional stat)
556 (if stat (stat filename stat) (stat filename)))))
558 (define-stat-call #-win32 "fstat" #+win32 "_fstat"
560 (function int int (* alien-stat)))
564 (define-call "s_isreg" boolean never-fails (mode mode-t))
565 (define-call "s_isdir" boolean never-fails (mode mode-t))
566 (define-call "s_ischr" boolean never-fails (mode mode-t))
567 (define-call "s_isblk" boolean never-fails (mode mode-t))
568 (define-call "s_isfifo" boolean never-fails (mode mode-t))
569 (define-call "s_islnk" boolean never-fails (mode mode-t))
570 (define-call "s_issock" boolean never-fails (mode mode-t))
574 (export 'pipe :sb-posix)
575 (declaim (inline pipe))
576 (defun pipe (&optional filedes2)
577 (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
579 (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
580 (let ((r (sb-sys:with-pinned-objects (filedes2)
582 ;; FIXME: (* INT)? (ARRAY INT 2) would be better
583 (extern-alien "pipe" (function int (* int)))
584 (sb-sys:vector-sap filedes2)))))
587 (values (aref filedes2 0) (aref filedes2 1))))
590 (define-protocol-class termios alien-termios ()
591 ((iflag :initarg :iflag :accessor sb-posix:termios-iflag
592 :documentation "Input modes.")
593 (oflag :initarg :oflag :accessor sb-posix:termios-oflag
594 :documentation "Output modes.")
595 (cflag :initarg :cflag :accessor sb-posix:termios-cflag
596 :documentation "Control modes.")
597 (lflag :initarg :lflag :accessor sb-posix:termios-lflag
598 :documentation "Local modes.")
599 (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs
600 :documentation "Control characters"))
601 (:documentation "Instances of this class represent I/O
602 characteristics of the terminal."))
606 (export 'tcsetattr :sb-posix)
607 (declaim (inline tcsetattr))
608 (defun tcsetattr (fd actions termios)
609 (declare (type termios termios))
610 (with-alien-termios a-termios ()
611 (termios-to-alien termios a-termios)
612 (let ((fd (file-descriptor fd)))
613 (let* ((r (alien-funcall
616 (function int int int (* alien-termios)))
617 fd actions a-termios)))
621 (export 'tcgetattr :sb-posix)
622 (declaim (inline tcgetattr))
623 (defun tcgetattr (fd &optional termios)
624 (declare (type (or null termios) termios))
625 (with-alien-termios a-termios ()
626 (let ((r (alien-funcall
627 (extern-alien "tcgetattr"
628 (function int int (* alien-termios)))
633 (setf termios (alien-to-termios a-termios termios))))
635 (define-call "tcdrain" int minusp (fd file-descriptor))
636 (define-call "tcflow" int minusp (fd file-descriptor) (action int))
637 (define-call "tcflush" int minusp (fd file-descriptor) (queue-selector int))
638 (define-call "tcgetsid" pid-t minusp (fd file-descriptor))
639 (define-call "tcsendbreak" int minusp (fd file-descriptor) (duration int))
640 (export 'cfsetispeed :sb-posix)
641 (declaim (inline cfsetispeed))
642 (defun cfsetispeed (speed &optional termios)
643 (declare (type (or null termios) termios))
644 (with-alien-termios a-termios ()
645 (let ((r (alien-funcall
646 (extern-alien "cfsetispeed"
647 (function int (* alien-termios) speed-t))
652 (setf termios (alien-to-termios a-termios termios))))
654 (export 'cfsetospeed :sb-posix)
655 (declaim (inline cfsetospeed))
656 (defun cfsetospeed (speed &optional termios)
657 (declare (type (or null termios) termios))
658 (with-alien-termios a-termios ()
659 (let ((r (alien-funcall
660 (extern-alien "cfsetospeed"
661 (function int (* alien-termios) speed-t))
666 (setf termios (alien-to-termios a-termios termios))))
668 (export 'cfgetispeed :sb-posix)
669 (declaim (inline cfgetispeed))
670 (defun cfgetispeed (termios)
671 (declare (type termios termios))
672 (with-alien-termios a-termios ()
673 (termios-to-alien termios a-termios)
674 (alien-funcall (extern-alien "cfgetispeed"
675 (function speed-t (* alien-termios)))
677 (export 'cfgetospeed :sb-posix)
678 (declaim (inline cfgetospeed))
679 (defun cfgetospeed (termios)
680 (declare (type termios termios))
681 (with-alien-termios a-termios ()
682 (termios-to-alien termios a-termios)
683 (alien-funcall (extern-alien "cfgetospeed"
684 (function speed-t (* alien-termios)))
690 (export 'time :sb-posix)
692 (let ((result (alien-funcall (extern-alien "time"
693 (function time-t (* time-t)))
698 (export 'utime :sb-posix)
699 (defun utime (filename &optional access-time modification-time)
700 (let ((fun (extern-alien "utime" (function int c-string
702 (name (filename filename)))
703 (if (not (and access-time modification-time))
704 (alien-funcall fun name nil)
705 (with-alien ((utimbuf (struct alien-utimbuf)))
706 (setf (slot utimbuf 'actime) (or access-time 0)
707 (slot utimbuf 'modtime) (or modification-time 0))
708 (let ((result (alien-funcall fun name (alien-sap utimbuf))))
712 (export 'utimes :sb-posix)
713 (defun utimes (filename &optional access-time modification-time)
714 (flet ((seconds-and-useconds (time)
715 (multiple-value-bind (integer fractional)
717 (values integer (cl:truncate (* fractional 1000000)))))
718 (maybe-syscall-error (value)
722 (let ((fun (extern-alien "utimes" (function int c-string
723 (* (array alien-timeval 2)))))
724 (name (filename filename)))
725 (if (not (and access-time modification-time))
726 (maybe-syscall-error (alien-funcall fun name nil))
727 (with-alien ((buf (array alien-timeval 2)))
728 (let ((actime (deref buf 0))
729 (modtime (deref buf 1)))
730 (setf (values (slot actime 'sec)
732 (seconds-and-useconds (or access-time 0))
733 (values (slot modtime 'sec)
734 (slot modtime 'usec))
735 (seconds-and-useconds (or modification-time 0)))
736 (maybe-syscall-error (alien-funcall fun name
737 (alien-sap buf))))))))))
742 (eval-when (:compile-toplevel :load-toplevel)
743 ;; Do this at compile-time as Win32 code below refers to it as
745 (export 'getenv :sb-posix))
747 (let ((r (alien-funcall
748 (extern-alien "getenv" (function (* char) c-string))
750 (declare (type (alien (* char)) r))
751 (unless (null-alien r)
755 (define-call "setenv" int minusp (name c-string) (value c-string) (overwrite int))
756 (define-call "unsetenv" int minusp (name c-string))
757 (export 'putenv :sb-posix)
758 (defun putenv (string)
759 (declare (string string))
760 ;; We don't want to call actual putenv: the string passed to putenv ends
761 ;; up in environ, and we any string we allocate GC might move.
763 ;; This makes our wrapper nonconformant if you squit hard enough, but
764 ;; users who care about that should really be calling putenv() directly in
765 ;; order to be able to manage memory sanely.
766 (let ((p (position #\= string))
770 (unsetenv (subseq string 0 p))
771 (setenv (subseq string 0 p) (subseq string (1+ p)) 1))
772 (error "Invalid argument to putenv: ~S" string)))))
775 ;; Windows doesn't define a POSIX setenv, but happily their _putenv is sane.
776 (define-call* "putenv" int minusp (string c-string))
777 (export 'setenv :sb-posix)
778 (defun setenv (name value overwrite)
779 (declare (string name value))
780 (if (and (zerop overwrite) (sb-posix:getenv name))
782 (putenv (concatenate 'string name "=" value))))
783 (export 'unsetenv :sb-posix)
784 (defun unsetenv (name)
785 (declare (string name))
786 (putenv (concatenate 'string name "="))))
791 (export 'openlog :sb-posix)
792 (export 'syslog :sb-posix)
793 (export 'closelog :sb-posix)
794 (defun openlog (ident options &optional (facility log-user))
795 (alien-funcall (extern-alien
796 "openlog" (function void c-string int int))
797 ident options facility))
798 (defun syslog (priority format &rest args)
799 "Send a message to the syslog facility, with severity level
800 PRIORITY. The message will be formatted as by CL:FORMAT (rather
801 than C's printf) with format string FORMAT and arguments ARGS."
802 (flet ((syslog1 (priority message)
803 (alien-funcall (extern-alien
804 "syslog" (function void int c-string c-string))
805 priority "%s" message)))
806 (syslog1 priority (apply #'format nil format args))))
807 (define-call "closelog" void never-fails))