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