1da9d8fa09a18b8bc8389e76357b4a1f17c8ea6d
[sbcl.git] / contrib / sb-posix / interface.lisp
1 (cl:in-package :sb-posix)
2
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))))
7     `(progn
8       (export ',name :sb-posix)
9       (defclass ,name ,superclasses
10         ,(loop for slotd in slots
11                collect (ldiff slotd (member :array-length slotd)))
12         ,@options)
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))
18         (unless instance
19           (setf instance (make-instance ',name)))
20         ,@(loop for slotd in slots
21                 ;; FIXME: slotds in source are more complicated in general
22                 ;;
23                 ;; FIXME: baroque construction of intricate fragility
24                 for array-length = (getf (cdr slotd) :array-length)
25                 if array-length
26                   collect `(progn
27                              (let ((array (make-array ,array-length)))
28                                (setf (slot-value instance ',(car slotd))
29                                      array)
30                                (dotimes (i ,array-length)
31                                  (setf (aref array i)
32                                        (sb-alien:deref
33                                         (sb-alien:slot alien ',(car slotd))
34                                         i)))))
35                 else
36                   collect `(setf (slot-value instance ',(car slotd))
37                                  (sb-alien:slot alien ',(car slotd))))
38         instance)
39       (defun ,to-alien (instance &optional alien)
40         (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
41                  (type ,name instance))
42         (unless alien
43           (setf alien (sb-alien:make-alien ,alien-type)))
44         ,@(loop for slotd in slots
45                 for array-length = (getf (cdr slotd) :array-length)
46                 if array-length
47                   collect `(progn
48                              (let ((array (slot-value instance ',(car slotd))))
49                                (dotimes (i ,array-length)
50                                  (setf (sb-alien:deref
51                                         (sb-alien:slot alien ',(car slotd))
52                                         i)
53                                        (aref array i)))))
54                 else
55                   collect `(setf (sb-alien:slot alien ',(car slotd))
56                                  (slot-value instance ',(car slotd)))))
57       (find-class ',name))))
58
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))))))
65
66 (defun syscall-error ()
67   (error 'sb-posix:syscall-error :errno (get-errno)))
68
69 (defun unsupported-error (lisp-name c-name)
70   (error "~S is unsupported by SBCL on this platform due to lack of ~A()."
71          lisp-name c-name))
72
73 (defun unsupported-warning (lisp-name c-name)
74   (warn "~S is unsupported by SBCL on this platform due to lack of ~A()."
75         lisp-name c-name))
76
77 (declaim (inline never-fails))
78 (defun never-fails (&rest args)
79   (declare (ignore args))
80   nil)
81
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
85 ;;; variable.
86 (eval-when (:compile-toplevel :load-toplevel)
87   (setf *c-functions-in-runtime*
88         '`(#+netbsd ,@("stat" "lstat" "fstat" "readdir" "opendir"))))
89
90
91 ;;; filesystem access
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))
96                                 ,@(cdr name))
97                              (concatenate 'string "_" name))
98                ,@arguments))
99
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)
110     (whence int))
111 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
112 (macrolet ((def (x)
113                `(progn
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))
120                    (if 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))
129 (define-call (#-netbsd "readdir" #+netbsd "_readdir" :options :largefile)
130   (* dirent)
131   ;; readdir() has the worst error convention in the world.  It's just
132   ;; too painful to support.  (return is NULL _and_ errno "unchanged"
133   ;; is not an error, it's EOF).
134   not
135   (dir (* t)))
136 (define-call "closedir" int minusp (dir (* t)))
137 ;; need to do this here because we can't do it in the DEFPACKAGE
138 (define-call* "umask" mode-t never-fails (mode mode-t))
139 (define-call* "getpid" pid-t never-fails)
140
141 #-win32
142 (progn
143   (define-call "chown" int minusp (pathname filename)
144                (owner uid-t) (group gid-t))
145   (define-call "chroot" int minusp (pathname filename))
146   (define-call "fchdir" int minusp (fd file-descriptor))
147   (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
148   (define-call "fchown" int minusp (fd file-descriptor)
149                (owner uid-t)  (group gid-t))
150   (define-call "fdatasync" int minusp (fd file-descriptor))
151   (define-call ("ftruncate" :options :largefile)
152       int minusp (fd file-descriptor) (length off-t))
153   (define-call "fsync" int minusp (fd file-descriptor))
154   (define-call "lchown" int minusp (pathname filename)
155                (owner uid-t)  (group gid-t))
156   (define-call "link" int minusp (oldpath filename) (newpath filename))
157   (define-call "lockf" int minusp (fd file-descriptor) (cmd int) (len off-t))
158   (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
159   (define-call "symlink" int minusp (oldpath filename) (newpath filename))
160   (define-call "sync" void never-fails)
161   (define-call ("truncate" :options :largefile)
162       int minusp (pathname filename) (length off-t))
163   #-win32
164   (macrolet ((def-mk*temp (lisp-name c-name result-type errorp dirp values)
165                (declare (ignore dirp))
166                (if (sb-sys:find-foreign-symbol-address c-name)
167                    `(progn
168                       (defun ,lisp-name (template)
169                         (let* ((external-format sb-alien::*default-c-string-external-format*)
170                                (arg (sb-ext:string-to-octets
171                                      (filename template)
172                                      :external-format external-format
173                                      :null-terminate t)))
174                           (sb-sys:with-pinned-objects (arg)
175                             ;; accommodate for the call-by-reference
176                             ;; nature of mks/dtemp's template strings.
177                             (let ((result (alien-funcall (extern-alien ,c-name
178                                                                        (function ,result-type system-area-pointer))
179                                                          (sb-alien::vector-sap arg))))
180                               (when (,errorp result)
181                                 (syscall-error))
182                               ;; FIXME: We'd rather return pathnames, but other
183                               ;; SB-POSIX functions like this return strings...
184                               (let ((pathname (sb-ext:octets-to-string
185                                                arg :external-format external-format
186                                                :end (1- (length arg)))))
187                                 ,(if values
188                                      '(values result pathname)
189                                      'pathname))))))
190                       (export ',lisp-name))
191                    `(progn
192                       (defun ,lisp-name (template)
193                         (declare (ignore template))
194                         (unsupported-error ',lisp-name ,c-name))
195                       (define-compiler-macro ,lisp-name (&whole form template)
196                         (declare (ignore template))
197                         (unsupported-warning ',lisp-name ,c-name)
198                         form)
199                       (export ',lisp-name)))))
200     (def-mk*temp mktemp "mktemp" (* char) null-alien nil nil)
201     ;; FIXME: Windows does have _mktemp, which has a slightly different
202     ;; interface
203     (def-mk*temp mkstemp "mkstemp" int minusp nil t)
204     ;; FIXME: What about Windows?
205     (def-mk*temp mkdtemp "mkdtemp" (* char) null-alien t nil))
206   (define-call-internally ioctl-without-arg "ioctl" int minusp
207                           (fd file-descriptor) (cmd int))
208   (define-call-internally ioctl-with-int-arg "ioctl" int minusp
209                           (fd file-descriptor) (cmd int) (arg int))
210   (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
211                           (fd file-descriptor) (cmd int)
212                           (arg alien-pointer-to-anything-or-nil))
213   (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
214     (if argp
215         (etypecase arg
216           ((alien int) (ioctl-with-int-arg fd cmd arg))
217           ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
218         (ioctl-without-arg fd cmd)))
219   (define-call-internally fcntl-without-arg "fcntl" int minusp
220                           (fd file-descriptor) (cmd int))
221   (define-call-internally fcntl-with-int-arg "fcntl" int minusp
222                           (fd file-descriptor) (cmd int) (arg int))
223   (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
224                           (fd file-descriptor) (cmd int)
225                           (arg alien-pointer-to-anything-or-nil))
226   (define-protocol-class flock alien-flock ()
227    ((type :initarg :type :accessor flock-type
228           :documentation "Type of lock; F_RDLCK, F_WRLCK, F_UNLCK.")
229     (whence :initarg :whence :accessor flock-whence
230             :documentation "Flag for starting offset.")
231     (start :initarg :start :accessor flock-start
232            :documentation "Relative offset in bytes.")
233     (len :initarg :len :accessor flock-len
234          :documentation "Size; if 0 then until EOF.")
235     ;; Note: PID isn't initable, and is read-only.  But other stuff in
236     ;; SB-POSIX right now loses when a protocol-class slot is unbound,
237     ;; so we initialize it to 0.
238     (pid :initform 0 :reader flock-pid
239          :documentation
240          "Process ID of the process holding the lock; returned with F_GETLK."))
241    (:documentation "Class representing locks used in fcntl(2)."))
242   (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
243     (if argp
244         (etypecase arg
245           ((alien int) (fcntl-with-int-arg fd cmd arg))
246           ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg))
247           (flock (with-alien-flock a-flock ()
248                    (flock-to-alien arg a-flock)
249                    (let ((r (fcntl-with-pointer-arg fd cmd a-flock)))
250                      (alien-to-flock a-flock arg)
251                      r))))
252         (fcntl-without-arg fd cmd)))
253
254   ;; uid, gid
255   (define-call "geteuid" uid-t never-fails) ; "always successful", it says
256   (define-call "getresuid" uid-t never-fails)
257   (define-call "getuid" uid-t never-fails)
258   (define-call "seteuid" int minusp (uid uid-t))
259   (define-call "setfsuid" int minusp (uid uid-t))
260   (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
261   (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
262   (define-call "setuid" int minusp (uid uid-t))
263   (define-call "getegid" gid-t never-fails)
264   (define-call "getgid" gid-t never-fails)
265   (define-call "getresgid" gid-t never-fails)
266   (define-call "setegid" int minusp (gid gid-t))
267   (define-call "setfsgid" int minusp (gid gid-t))
268   (define-call "setgid" int minusp (gid gid-t))
269   (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
270   (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
271
272   ;; processes, signals
273   (define-call "alarm" int never-fails (seconds unsigned))
274
275
276
277   #+mach-exception-handler
278   (progn
279     ;; FIXME this is a lie, of course this can fail, but there's no
280     ;; error handling here yet!
281     (define-call "setup_mach_exceptions" void never-fails)
282     (define-call ("posix_fork" :c-name "fork") pid-t minusp)
283     (defun fork ()
284       (let ((pid (posix-fork)))
285         (when (= pid 0)
286           (setup-mach-exceptions))
287         pid))
288     (export 'fork :sb-posix))
289
290   #-mach-exception-handler
291   (define-call "fork" pid-t minusp)
292
293   (define-call "getpgid" pid-t minusp (pid pid-t))
294   (define-call "getppid" pid-t never-fails)
295   (define-call "getpgrp" pid-t never-fails)
296   (define-call "getsid" pid-t minusp  (pid pid-t))
297   (define-call "kill" int minusp (pid pid-t) (signal int))
298   (define-call "killpg" int minusp (pgrp int) (signal int))
299   (define-call "pause" int minusp)
300   (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
301   (define-call "setpgrp" int minusp)
302   (define-call "setsid" pid-t minusp))
303
304 (defmacro with-growing-c-string ((buffer size) &body body)
305   (sb-int:with-unique-names (c-string-block)
306     `(block ,c-string-block
307        (let (,buffer)
308          (flet ((,buffer (&optional (size-incl-null))
309                   (when size-incl-null
310                     (setf (sb-sys:sap-ref-8 (sb-alien:alien-sap ,buffer) size-incl-null)
311                           0))
312                   (return-from ,c-string-block
313                     (sb-alien::c-string-to-string
314                      (sb-alien:alien-sap ,buffer)
315                      (sb-impl::default-external-format)
316                      'character))))
317            (loop for ,size = 128 then (* 2 ,size)
318                  do (unwind-protect
319                          (progn
320                            (setf ,buffer (make-alien c-string ,size))
321                            ,@body)
322                       (when ,buffer
323                         (free-alien ,buffer)))))))))
324
325 #-win32
326 (progn
327   (export 'readlink :sb-posix)
328   (defun readlink (pathspec)
329     "Returns the resolved target of a symbolic link as a string."
330     (flet ((%readlink (path buf length)
331              (alien-funcall
332               (extern-alien "readlink" (function int c-string (* t) int))
333               path buf length)))
334       (with-growing-c-string (buf size)
335         (let ((count (%readlink (filename pathspec) buf size)))
336           (cond ((minusp count)
337                  (syscall-error))
338                 ((< 0 count size)
339                  (buf count))))))))
340
341 (progn
342   (export 'getcwd :sb-posix)
343   (defun getcwd ()
344     "Returns the process's current working directory as a string."
345     (flet ((%getcwd (buffer size)
346              (alien-funcall
347               (extern-alien #-win32 "getcwd"
348                             #+win32 "_getcwd" (function c-string (* t) int))
349               buffer size)))
350       (with-growing-c-string (buf size)
351         (let ((result (%getcwd buf size)))
352           (cond (result
353                  (buf))
354                 ((/= (get-errno) sb-posix:erange)
355                  (syscall-error))))))))
356
357 #-win32
358 (progn
359  (export 'wait :sb-posix)
360  (declaim (inline wait))
361  (defun wait (&optional statusptr)
362    (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
363    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
364           (pid (sb-sys:with-pinned-objects (ptr)
365                  (alien-funcall
366                   (extern-alien "wait" (function pid-t (* int)))
367                   (sb-sys:vector-sap ptr)))))
368      (if (minusp pid)
369          (syscall-error)
370          (values pid (aref ptr 0))))))
371
372 #-win32
373 (progn
374  (export 'waitpid :sb-posix)
375  (declaim (inline waitpid))
376  (defun waitpid (pid options &optional statusptr)
377    (declare (type (sb-alien:alien pid-t) pid)
378             (type (sb-alien:alien int) options)
379             (type (or null (simple-array (signed-byte 32) (1))) statusptr))
380    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
381           (pid (sb-sys:with-pinned-objects (ptr)
382                  (alien-funcall
383                   (extern-alien "waitpid" (function pid-t
384                                                     pid-t (* int) int))
385                   pid (sb-sys:vector-sap ptr) options))))
386      (if (minusp pid)
387          (syscall-error)
388          (values pid (aref ptr 0)))))
389  ;; waitpid macros
390  (define-call "wifexited" boolean never-fails (status int))
391  (define-call "wexitstatus" int never-fails (status int))
392  (define-call "wifsignaled" boolean never-fails (status int))
393  (define-call "wtermsig" int never-fails (status int))
394  (define-call "wifstopped" boolean never-fails (status int))
395  (define-call "wstopsig" int never-fails (status int))
396  #+nil ; see alien/waitpid-macros.c
397  (define-call "wifcontinued" boolean never-fails (status int)))
398
399 ;;; mmap, msync
400 #-win32
401 (progn
402  (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
403    (lambda (res)
404      (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
405    (addr sap-or-nil) (length unsigned) (prot unsigned)
406    (flags unsigned) (fd file-descriptor) (offset off-t))
407
408  (define-call "munmap" int minusp
409    (start sb-sys:system-area-pointer) (length unsigned))
410
411 (define-call "msync" int minusp
412   (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
413
414 ;;; mlockall, munlockall
415 (define-call "mlockall" int minusp (flags int))
416 (define-call "munlockall" int minusp)
417
418 #-win32
419 (define-call "getpagesize" int minusp)
420 #+win32
421 ;;; KLUDGE: This could be taken from GetSystemInfo
422 (export (defun getpagesize () 4096))
423
424 ;;; passwd database
425 ;; The docstrings are copied from the descriptions in SUSv3,
426 ;; where present.
427 #-win32
428 (define-protocol-class passwd alien-passwd ()
429   ((name :initarg :name :accessor passwd-name
430          :documentation "User's login name.")
431    ;; Note: SUSv3 doesn't require this member.
432    (passwd :initarg :passwd :accessor passwd-passwd
433            :documentation "The account's encrypted password.")
434    (uid :initarg :uid :accessor passwd-uid
435         :documentation "Numerical user ID.")
436    (gid :initarg :gid :accessor passwd-gid
437         :documentation "Numerical group ID.")
438    ;; Note: SUSv3 doesn't require this member.
439    (gecos :initarg :gecos :accessor passwd-gecos
440           :documentation "User's name or comment field.")
441    (dir :initarg :dir :accessor passwd-dir
442         :documentation "Initial working directory.")
443    (shell :initarg :shell :accessor passwd-shell
444           :documentation "Program to use as shell."))
445   (:documentation "Instances of this class represent entries in
446                    the system's user database."))
447
448 ;;; group database
449 #-win32
450 (define-protocol-class group alien-group ()
451   ((name :initarg :name :accessor group-name)
452    (passwd :initarg :passwd :accessor group-passwd)
453    (gid :initarg :gid :accessor group-gid)))
454
455 (defmacro define-obj-call (name arg type conv)
456   #-win32
457   ;; FIXME: this isn't the documented way of doing this, surely?
458   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
459     `(progn
460       (export ',lisp-name :sb-posix)
461       (declaim (inline ,lisp-name))
462       (defun ,lisp-name (,arg)
463         (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
464           (if (null-alien r)
465               nil
466               (,conv r)))))))
467
468 (define-obj-call "getpwnam" login-name (function (* alien-passwd) c-string) alien-to-passwd)
469 (define-obj-call "getpwuid" uid (function (* alien-passwd) uid-t) alien-to-passwd)
470 (define-obj-call "getgrnam" login-name (function (* alien-group) c-string) alien-to-group)
471 (define-obj-call "getgrgid" gid (function (* alien-group) gid-t) alien-to-group)
472
473
474 #-win32
475 (define-protocol-class timeval alien-timeval ()
476   ((sec :initarg :tv-sec :accessor timeval-sec
477         :documentation "Seconds.")
478    (usec :initarg :tv-usec :accessor timeval-usec
479          :documentation "Microseconds."))
480   (:documentation "Instances of this class represent time values."))
481
482 (define-protocol-class stat alien-stat ()
483   ((mode :initarg :mode :reader stat-mode
484          :documentation "Mode of file.")
485    (ino :initarg :ino :reader stat-ino
486         :documentation "File serial number.")
487    (dev :initarg :dev :reader stat-dev
488         :documentation "Device ID of device containing file.")
489    (nlink :initarg :nlink :reader stat-nlink
490           :documentation "Number of hard links to the file.")
491    (uid :initarg :uid :reader stat-uid
492         :documentation "User ID of file.")
493    (gid :initarg :gid :reader stat-gid
494         :documentation "Group ID of file.")
495    (size :initarg :size :reader stat-size
496          :documentation "For regular files, the file size in
497                          bytes.  For symbolic links, the length
498                          in bytes of the filename contained in
499                          the symbolic link.")
500    (atime :initarg :atime :reader stat-atime
501           :documentation "Time of last access.")
502    (mtime :initarg :mtime :reader stat-mtime
503           :documentation "Time of last data modification.")
504    (ctime :initarg :ctime :reader stat-ctime
505           :documentation "Time of last status change"))
506   (:documentation "Instances of this class represent Posix file
507                    metadata."))
508
509 (defmacro define-stat-call (name arg designator-fun type)
510   ;; FIXME: this isn't the documented way of doing this, surely?
511   (let ((lisp-name (lisp-for-c-symbol name)))
512     `(progn
513       (export ',lisp-name :sb-posix)
514       (declaim (inline ,lisp-name))
515       (defun ,lisp-name (,arg &optional stat)
516         (declare (type (or null stat) stat))
517         (with-alien-stat a-stat ()
518           (let ((r (alien-funcall
519                     (extern-alien ,(real-c-name (list name :options :largefile)) ,type)
520                     (,designator-fun ,arg)
521                     a-stat)))
522             (when (minusp r)
523               (syscall-error))
524             (alien-to-stat a-stat stat)))))))
525
526 (define-stat-call #-win32 "stat" #+win32 "_stat"
527                   pathname filename
528                   (function int c-string (* alien-stat)))
529
530 #-win32
531 (define-stat-call "lstat"
532                   pathname filename
533                   (function int c-string (* alien-stat)))
534 ;;; No symbolic links on Windows, so use stat
535 #+win32
536 (progn
537   (declaim (inline lstat))
538   (export (defun lstat (filename &optional stat)
539             (if stat (stat filename stat) (stat filename)))))
540
541 (define-stat-call #-win32 "fstat" #+win32 "_fstat"
542                   fd file-descriptor
543                   (function int int (* alien-stat)))
544
545
546 ;;; mode flags
547 (define-call "s_isreg" boolean never-fails (mode mode-t))
548 (define-call "s_isdir" boolean never-fails (mode mode-t))
549 (define-call "s_ischr" boolean never-fails (mode mode-t))
550 (define-call "s_isblk" boolean never-fails (mode mode-t))
551 (define-call "s_isfifo" boolean never-fails (mode mode-t))
552 (define-call "s_islnk" boolean never-fails (mode mode-t))
553 (define-call "s_issock" boolean never-fails (mode mode-t))
554
555 #-win32
556 (progn
557  (export 'pipe :sb-posix)
558  (declaim (inline pipe))
559  (defun pipe (&optional filedes2)
560    (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
561    (unless filedes2
562      (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
563    (let ((r (sb-sys:with-pinned-objects (filedes2)
564               (alien-funcall
565                ;; FIXME: (* INT)?  (ARRAY INT 2) would be better
566                (extern-alien "pipe" (function int (* int)))
567                (sb-sys:vector-sap filedes2)))))
568      (when (minusp r)
569        (syscall-error)))
570    (values (aref filedes2 0) (aref filedes2 1))))
571
572 #-win32
573 (define-protocol-class termios alien-termios ()
574   ((iflag :initarg :iflag :accessor sb-posix:termios-iflag
575           :documentation "Input modes.")
576    (oflag :initarg :oflag :accessor sb-posix:termios-oflag
577           :documentation "Output modes.")
578    (cflag :initarg :cflag :accessor sb-posix:termios-cflag
579           :documentation "Control modes.")
580    (lflag :initarg :lflag :accessor sb-posix:termios-lflag
581           :documentation "Local modes.")
582    (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs
583        :documentation "Control characters"))
584   (:documentation "Instances of this class represent I/O
585                    characteristics of the terminal."))
586
587 #-win32
588 (progn
589  (export 'tcsetattr :sb-posix)
590  (declaim (inline tcsetattr))
591  (defun tcsetattr (fd actions termios)
592    (declare (type termios termios))
593    (with-alien-termios a-termios ()
594      (termios-to-alien termios a-termios)
595      (let ((fd (file-descriptor fd)))
596        (let* ((r (alien-funcall
597                   (extern-alien
598                    "tcsetattr"
599                    (function int int int (* alien-termios)))
600                   fd actions a-termios)))
601          (when (minusp r)
602            (syscall-error)))
603        (values))))
604  (export 'tcgetattr :sb-posix)
605  (declaim (inline tcgetattr))
606  (defun tcgetattr (fd &optional termios)
607    (declare (type (or null termios) termios))
608    (with-alien-termios a-termios ()
609      (let ((r (alien-funcall
610                (extern-alien "tcgetattr"
611                              (function int int (* alien-termios)))
612                (file-descriptor fd)
613                a-termios)))
614        (when (minusp r)
615          (syscall-error))
616        (setf termios (alien-to-termios a-termios termios))))
617    termios)
618  (export 'cfsetispeed :sb-posix)
619  (declaim (inline cfsetispeed))
620  (defun cfsetispeed (speed &optional termios)
621    (declare (type (or null termios) termios))
622    (with-alien-termios a-termios ()
623      (let ((r (alien-funcall
624                (extern-alien "cfsetispeed"
625                              (function int (* alien-termios) speed-t))
626                a-termios
627                speed)))
628        (when (minusp r)
629          (syscall-error))
630        (setf termios (alien-to-termios a-termios termios))))
631    termios)
632  (export 'cfsetospeed :sb-posix)
633  (declaim (inline cfsetospeed))
634  (defun cfsetospeed (speed &optional termios)
635    (declare (type (or null termios) termios))
636    (with-alien-termios a-termios ()
637      (let ((r (alien-funcall
638                (extern-alien "cfsetospeed"
639                              (function int (* alien-termios) speed-t))
640                a-termios
641                speed)))
642        (when (minusp r)
643          (syscall-error))
644        (setf termios (alien-to-termios a-termios termios))))
645    termios)
646  (export 'cfgetispeed :sb-posix)
647  (declaim (inline cfgetispeed))
648  (defun cfgetispeed (termios)
649    (declare (type termios termios))
650    (with-alien-termios a-termios ()
651      (termios-to-alien termios a-termios)
652      (alien-funcall (extern-alien "cfgetispeed"
653                                   (function speed-t (* alien-termios)))
654                     a-termios)))
655  (export 'cfgetospeed :sb-posix)
656  (declaim (inline cfgetospeed))
657  (defun cfgetospeed (termios)
658    (declare (type termios termios))
659    (with-alien-termios a-termios ()
660      (termios-to-alien termios a-termios)
661      (alien-funcall (extern-alien "cfgetospeed"
662                                  (function speed-t (* alien-termios)))
663                     a-termios))))
664
665
666 #-win32
667 (progn
668   (export 'time :sb-posix)
669   (defun time ()
670     (let ((result (alien-funcall (extern-alien "time"
671                                                (function time-t (* time-t)))
672                                  nil)))
673       (if (minusp result)
674           (syscall-error)
675           result)))
676   (export 'utime :sb-posix)
677   (defun utime (filename &optional access-time modification-time)
678     (let ((fun (extern-alien "utime" (function int c-string
679                                                (* alien-utimbuf))))
680           (name (filename filename)))
681       (if (not (and access-time modification-time))
682           (alien-funcall fun name nil)
683           (with-alien ((utimbuf (struct alien-utimbuf)))
684             (setf (slot utimbuf 'actime) (or access-time 0)
685                   (slot utimbuf 'modtime) (or modification-time 0))
686             (let ((result (alien-funcall fun name (alien-sap utimbuf))))
687               (if (minusp result)
688                   (syscall-error)
689                   result))))))
690   (export 'utimes :sb-posix)
691   (defun utimes (filename &optional access-time modification-time)
692     (flet ((seconds-and-useconds (time)
693              (multiple-value-bind (integer fractional)
694                  (cl:truncate time)
695                (values integer (cl:truncate (* fractional 1000000)))))
696            (maybe-syscall-error (value)
697              (if (minusp value)
698                  (syscall-error)
699                  value)))
700       (let ((fun (extern-alien "utimes" (function int c-string
701                                                   (* (array alien-timeval 2)))))
702             (name (filename filename)))
703         (if (not (and access-time modification-time))
704             (maybe-syscall-error (alien-funcall fun name nil))
705             (with-alien ((buf (array alien-timeval 2)))
706               (let ((actime (deref buf 0))
707                     (modtime (deref buf 1)))
708                 (setf (values (slot actime 'sec)
709                               (slot actime 'usec))
710                       (seconds-and-useconds (or access-time 0))
711                       (values (slot modtime 'sec)
712                               (slot modtime 'usec))
713                       (seconds-and-useconds (or modification-time 0)))
714                 (maybe-syscall-error (alien-funcall fun name
715                                                     (alien-sap buf))))))))))
716
717
718 ;;; environment
719
720 (export 'getenv :sb-posix)
721 (defun getenv (name)
722   (let ((r (alien-funcall
723             (extern-alien "getenv" (function (* char) c-string))
724             name)))
725     (declare (type (alien (* char)) r))
726     (unless (null-alien r)
727       (cast r c-string))))
728 (define-call "putenv" int minusp (string c-string))
729
730 ;;; syslog
731 #-win32
732 (progn
733   (export 'openlog :sb-posix)
734   (export 'syslog :sb-posix)
735   (export 'closelog :sb-posix)
736   (defun openlog (ident options &optional (facility log-user))
737     (alien-funcall (extern-alien
738                     "openlog" (function void c-string int int))
739                    ident options facility))
740   (defun syslog (priority format &rest args)
741     "Send a message to the syslog facility, with severity level
742 PRIORITY.  The message will be formatted as by CL:FORMAT (rather
743 than C's printf) with format string FORMAT and arguments ARGS."
744     (flet ((syslog1 (priority message)
745              (alien-funcall (extern-alien
746                              "syslog" (function void int c-string c-string))
747                             priority "%s" message)))
748       (syslog1 priority (apply #'format nil format args))))
749   (define-call "closelog" void never-fails))