8c2cac34da2f0d60e1703909a160e0b48858358e
[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       (defclass ,name ,superclasses
9         ,(loop for slotd in slots
10                collect (ldiff slotd (member :array-length slotd)))
11         ,@options)
12       (declaim (inline ,to-alien ,to-protocol))
13       (declaim (inline ,to-protocol ,to-alien))
14       (defun ,to-protocol (alien &optional instance)
15         (declare (type (sb-alien:alien (* ,alien-type)) alien)
16                  (type (or null ,name) instance))
17         (unless instance
18           (setf instance (make-instance ',name)))
19         ,@(loop for slotd in slots
20                 ;; FIXME: slotds in source are more complicated in general
21                 ;;
22                 ;; FIXME: baroque construction of intricate fragility
23                 for array-length = (getf (cdr slotd) :array-length)
24                 if array-length
25                   collect `(progn
26                              (let ((array (make-array ,array-length)))
27                                (setf (slot-value instance ',(car slotd))
28                                      array)
29                                (dotimes (i ,array-length)
30                                  (setf (aref array i)
31                                        (sb-alien:deref
32                                         (sb-alien:slot alien ',(car slotd))
33                                         i)))))
34                 else
35                   collect `(setf (slot-value instance ',(car slotd))
36                                  (sb-alien:slot alien ',(car slotd))))
37         instance)
38       (defun ,to-alien (instance &optional alien)
39         (declare (type (or null (sb-alien:alien (* ,alien-type))) alien)
40                  (type ,name instance))
41         (unless alien
42           (setf alien (sb-alien:make-alien ,alien-type)))
43         ,@(loop for slotd in slots
44                 for array-length = (getf (cdr slotd) :array-length)
45                 if array-length
46                   collect `(progn
47                              (let ((array (slot-value instance ',(car slotd))))
48                                (dotimes (i ,array-length)
49                                  (setf (sb-alien:deref
50                                         (sb-alien:slot alien ',(car slotd))
51                                         i)
52                                        (aref array i)))))
53                 else
54                   collect `(setf (sb-alien:slot alien ',(car slotd))
55                                  (slot-value instance ',(car slotd)))))
56       (find-class ',name))))
57
58 (define-condition sb-posix:syscall-error (error)
59   ((errno :initarg :errno :reader sb-posix:syscall-errno))
60   (:report (lambda (c s)
61              (let ((errno (sb-posix:syscall-errno c)))
62                (format s "System call error ~A (~A)"
63                        errno (sb-int:strerror errno))))))
64
65 (defun syscall-error ()
66   (error 'sb-posix:syscall-error :errno (get-errno)))
67
68 (declaim (inline never-fails))
69 (defun never-fails (&rest args)
70   (declare (ignore args))
71   nil)
72
73 ;;; Some systems may need C-level wrappers, which can live in the
74 ;;; runtime (so that save-lisp-and-die can produce standalone
75 ;;; executables).  See REAL-C-NAME in macros.lisp for the use of this
76 ;;; variable.
77 (eval-when (:compile-toplevel :load-toplevel)
78   (setf *c-functions-in-runtime*
79         '`(#+netbsd ,@("stat" "lstat" "fstat" "readdir" "opendir"))))
80
81
82 ;;; filesystem access
83 (defmacro define-call* (name &rest arguments)
84   #-win32 `(define-call ,name ,@arguments)
85   #+win32 `(define-call ,(if (consp name)
86                              `(,(concatenate 'string "_" (car name))
87                                 ,@(cdr name))
88                              (concatenate 'string "_" name))
89                ,@arguments))
90
91 (define-call* "access" int minusp (pathname filename) (mode int))
92 (define-call* "chdir" int minusp (pathname filename))
93 (define-call* "chmod" int minusp (pathname filename) (mode mode-t))
94 (define-call* "close" int minusp (fd file-descriptor))
95 (define-call* "creat" int minusp (pathname filename) (mode mode-t))
96 (define-call* "dup" int minusp (oldfd file-descriptor))
97 (define-call* "dup2" int minusp (oldfd file-descriptor)
98               (newfd file-descriptor))
99 (define-call* ("lseek" :options :largefile)
100     off-t minusp (fd file-descriptor) (offset off-t)
101     (whence int))
102 (define-call* "mkdir" int minusp (pathname filename) (mode mode-t))
103 (macrolet ((def (x)
104                `(progn
105                  (define-call-internally open-with-mode ,x int minusp
106                    (pathname filename) (flags int) (mode mode-t))
107                  (define-call-internally open-without-mode ,x int minusp
108                    (pathname filename) (flags int))
109                  (define-entry-point ,x
110                      (pathname flags &optional (mode nil mode-supplied))
111                    (if mode-supplied
112                        (open-with-mode pathname flags mode)
113                        (open-without-mode pathname flags))))))
114     (def #-win32 "open" #+win32 "_open"))
115 (define-call "rename" int minusp (oldpath filename) (newpath filename))
116 (define-call* "rmdir" int minusp (pathname filename))
117 (define-call* "unlink" int minusp (pathname filename))
118 (define-call #-netbsd "opendir" #+netbsd "_opendir"
119     (* t) null-alien (pathname filename))
120 (define-call (#-netbsd "readdir" #+netbsd "_readdir" :options :largefile)
121   (* dirent)
122   ;; readdir() has the worst error convention in the world.  It's just
123   ;; too painful to support.  (return is NULL _and_ errno "unchanged"
124   ;; is not an error, it's EOF).
125   not
126   (dir (* t)))
127 (define-call "closedir" int minusp (dir (* t)))
128 ;; need to do this here because we can't do it in the DEFPACKAGE
129 (define-call* "umask" mode-t never-fails (mode mode-t))
130 (define-call* "getpid" pid-t never-fails)
131
132 #-win32
133 (progn
134   (define-call "chown" int minusp (pathname filename)
135                (owner uid-t) (group gid-t))
136   (define-call "chroot" int minusp (pathname filename))
137   (define-call "fchdir" int minusp (fd file-descriptor))
138   (define-call "fchmod" int minusp (fd file-descriptor) (mode mode-t))
139   (define-call "fchown" int minusp (fd file-descriptor)
140                (owner uid-t)  (group gid-t))
141   (define-call "fdatasync" int minusp (fd file-descriptor))
142   (define-call ("ftruncate" :options :largefile)
143       int minusp (fd file-descriptor) (length off-t))
144   (define-call "fsync" int minusp (fd file-descriptor))
145   (define-call "lchown" int minusp (pathname filename)
146                (owner uid-t)  (group gid-t))
147   (define-call "link" int minusp (oldpath filename) (newpath filename))
148   (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
149   (define-call "symlink" int minusp (oldpath filename) (newpath filename))
150   (define-call "sync" void never-fails)
151   (define-call ("truncate" :options :largefile)
152       int minusp (pathname filename) (length off-t))
153   ;; FIXME: Windows does have _mktemp, which has a slightlty different
154   ;; interface
155   (defun mkstemp (template)
156     ;; we are emulating sb-alien's charset conversion for strings
157     ;; here, to accommodate for the call-by-reference nature of
158     ;; mkstemp's template strings.
159     (let ((arg (sb-ext:string-to-octets
160                 (filename template)
161                 :external-format sb-alien::*default-c-string-external-format*)))
162       (sb-sys:with-pinned-objects (arg)
163         (let ((result (alien-funcall (extern-alien "mkstemp"
164                                                    (function int c-string))
165                                      (sap-alien (sb-alien::vector-sap arg)
166                                                 (* char)))))
167           (when (minusp result)
168             (syscall-error))
169           (values result
170                   (sb-ext:octets-to-string
171                    arg
172                    :external-format sb-alien::*default-c-string-external-format*))))))
173   (define-call-internally ioctl-without-arg "ioctl" int minusp
174                           (fd file-descriptor) (cmd int))
175   (define-call-internally ioctl-with-int-arg "ioctl" int minusp
176                           (fd file-descriptor) (cmd int) (arg int))
177   (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
178                           (fd file-descriptor) (cmd int)
179                           (arg alien-pointer-to-anything-or-nil))
180   (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
181     (if argp
182         (etypecase arg
183           ((alien int) (ioctl-with-int-arg fd cmd arg))
184           ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
185         (ioctl-without-arg fd cmd)))
186   (define-call-internally fcntl-without-arg "fcntl" int minusp
187                           (fd file-descriptor) (cmd int))
188   (define-call-internally fcntl-with-int-arg "fcntl" int minusp
189                           (fd file-descriptor) (cmd int) (arg int))
190   (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
191                           (fd file-descriptor) (cmd int)
192                           (arg alien-pointer-to-anything-or-nil))
193   (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
194     (if argp
195         (etypecase arg
196           ((alien int) (fcntl-with-int-arg fd cmd arg))
197           ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
198         (fcntl-without-arg fd cmd)))
199
200   ;; uid, gid
201   (define-call "geteuid" uid-t never-fails) ; "always successful", it says
202   (define-call "getresuid" uid-t never-fails)
203   (define-call "getuid" uid-t never-fails)
204   (define-call "seteuid" int minusp (uid uid-t))
205   (define-call "setfsuid" int minusp (uid uid-t))
206   (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
207   (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
208   (define-call "setuid" int minusp (uid uid-t))
209   (define-call "getegid" gid-t never-fails)
210   (define-call "getgid" gid-t never-fails)
211   (define-call "getresgid" gid-t never-fails)
212   (define-call "setegid" int minusp (gid gid-t))
213   (define-call "setfsgid" int minusp (gid gid-t))
214   (define-call "setgid" int minusp (gid gid-t))
215   (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
216   (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
217
218   ;; processes, signals
219   (define-call "alarm" int never-fails (seconds unsigned))
220
221
222
223   #+mach-exception-handler
224   (progn
225     ;; FIXME this is a lie, of course this can fail, but there's no
226     ;; error handling here yet!
227     (define-call "setup_mach_exceptions" void never-fails)
228     (define-call ("posix_fork" :c-name "fork") pid-t minusp)
229     (defun fork ()
230       (let ((pid (posix-fork)))
231         (when (= pid 0)
232           (setup-mach-exceptions))
233         pid))
234     (export 'fork :sb-posix))
235
236   #-mach-exception-handler
237   (define-call "fork" pid-t minusp)
238
239   (define-call "getpgid" pid-t minusp (pid pid-t))
240   (define-call "getppid" pid-t never-fails)
241   (define-call "getpgrp" pid-t never-fails)
242   (define-call "getsid" pid-t minusp  (pid pid-t))
243   (define-call "kill" int minusp (pid pid-t) (signal int))
244   (define-call "killpg" int minusp (pgrp int) (signal int))
245   (define-call "pause" int minusp)
246   (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
247   (define-call "setpgrp" int minusp))
248
249 #-win32
250 (progn
251   (export 'readlink :sb-posix)
252   (defun readlink (pathspec)
253     (flet ((%readlink (path buf length)
254              (alien-funcall
255               (extern-alien "readlink" (function int c-string (* t) int))
256               path buf length)))
257       (loop for size = 128 then (* 2 size)
258             for buf = (make-alien c-string size)
259             do (unwind-protect
260                     (let ((count (%readlink (filename pathspec) buf size)))
261                       (cond ((minusp count)
262                              (syscall-error))
263                             ((< 0 count size)
264                              (setf (sb-sys:sap-ref-8 (sb-alien:alien-sap buf)
265                                                      count)
266                                    0)
267                              (return
268                                (sb-alien::c-string-to-string
269                                 (sb-alien:alien-sap buf)
270                                 (sb-impl::default-external-format)
271                                 'character)))))
272                  (free-alien buf))))))
273
274 #-win32
275 (progn
276  (export 'wait :sb-posix)
277  (declaim (inline wait))
278  (defun wait (&optional statusptr)
279    (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
280    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
281           (pid (alien-funcall
282                 (extern-alien "wait" (function pid-t (* int)))
283                 (sb-sys:vector-sap ptr))))
284      (if (minusp pid)
285          (syscall-error)
286          (values pid (aref ptr 0))))))
287
288 #-win32
289 (progn
290  (export 'waitpid :sb-posix)
291  (declaim (inline waitpid))
292  (defun waitpid (pid options &optional statusptr)
293    (declare (type (sb-alien:alien pid-t) pid)
294             (type (sb-alien:alien int) options)
295             (type (or null (simple-array (signed-byte 32) (1))) statusptr))
296    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
297           (pid (alien-funcall
298                 (extern-alien "waitpid" (function pid-t
299                                                   pid-t (* int) int))
300                 pid (sb-sys:vector-sap ptr) options)))
301      (if (minusp pid)
302          (syscall-error)
303          (values pid (aref ptr 0)))))
304  ;; waitpid macros
305  (define-call "wifexited" boolean never-fails (status int))
306  (define-call "wexitstatus" int never-fails (status int))
307  (define-call "wifsignaled" boolean never-fails (status int))
308  (define-call "wtermsig" int never-fails (status int))
309  (define-call "wifstopped" boolean never-fails (status int))
310  (define-call "wstopsig" int never-fails (status int))
311  #+nil ; see alien/waitpid-macros.c
312  (define-call "wifcontinued" boolean never-fails (status int)))
313
314 ;;; mmap, msync
315 #-win32
316 (progn
317  (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
318    (lambda (res)
319      (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
320    (addr sap-or-nil) (length unsigned) (prot unsigned)
321    (flags unsigned) (fd file-descriptor) (offset off-t))
322
323  (define-call "munmap" int minusp
324    (start sb-sys:system-area-pointer) (length unsigned))
325
326 (define-call "msync" int minusp
327   (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
328
329 #-win32
330 (define-call "getpagesize" int minusp)
331 #+win32
332 ;;; KLUDGE: This could be taken from GetSystemInfo
333 (export (defun getpagesize () 4096))
334
335 ;;; passwd database
336 #-win32
337 (define-protocol-class passwd alien-passwd ()
338   ((name :initarg :name :accessor passwd-name)
339    (passwd :initarg :passwd :accessor passwd-passwd)
340    (uid :initarg :uid :accessor passwd-uid)
341    (gid :initarg :gid :accessor passwd-gid)
342    (gecos :initarg :gecos :accessor passwd-gecos)
343    (dir :initarg :dir :accessor passwd-dir)
344    (shell :initarg :shell :accessor passwd-shell)))
345
346 (defmacro define-pw-call (name arg type)
347   #-win32
348   ;; FIXME: this isn't the documented way of doing this, surely?
349   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
350     `(progn
351       (export ',lisp-name :sb-posix)
352       (declaim (inline ,lisp-name))
353       (defun ,lisp-name (,arg)
354         (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
355           (if (null r)
356               r
357               (alien-to-passwd r)))))))
358
359 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
360 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
361
362 (define-protocol-class timeval alien-timeval ()
363   ((sec :initarg :tv-sec :accessor timeval-sec)
364    (usec :initarg :tv-usec :accessor timeval-usec)))
365
366 (define-protocol-class stat alien-stat ()
367   ((mode :initarg :mode :accessor stat-mode)
368    (ino :initarg :ino :accessor stat-ino)
369    (dev :initarg :dev :accessor stat-dev)
370    (nlink :initarg :nlink :accessor stat-nlink)
371    (uid :initarg :uid :accessor stat-uid)
372    (gid :initarg :gid :accessor stat-gid)
373    (size :initarg :size :accessor stat-size)
374    (atime :initarg :atime :accessor stat-atime)
375    (mtime :initarg :mtime :accessor stat-mtime)
376    (ctime :initarg :ctime :accessor stat-ctime)))
377
378 (defmacro define-stat-call (name arg designator-fun type)
379   ;; FIXME: this isn't the documented way of doing this, surely?
380   (let ((lisp-name (lisp-for-c-symbol name)))
381     `(progn
382       (export ',lisp-name :sb-posix)
383       (declaim (inline ,lisp-name))
384       (defun ,lisp-name (,arg &optional stat)
385         (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
386         (with-alien-stat a-stat ()
387           (let ((r (alien-funcall
388                     (extern-alien ,(real-c-name (list name :options :largefile)) ,type)
389                     (,designator-fun ,arg)
390                     a-stat)))
391             (when (minusp r)
392               (syscall-error))
393             (alien-to-stat a-stat stat)))))))
394
395 (define-stat-call #-win32 "stat" #+win32 "_stat"
396                   pathname filename
397                   (function int c-string (* alien-stat)))
398
399 #-win32
400 (define-stat-call "lstat"
401                   pathname filename
402                   (function int c-string (* alien-stat)))
403 ;;; No symbolic links on Windows, so use stat
404 #+win32
405 (progn
406   (declaim (inline lstat))
407   (export (defun lstat (filename &optional stat)
408             (if stat (stat filename stat) (stat filename)))))
409
410 (define-stat-call #-win32 "fstat" #+win32 "_fstat"
411                   fd file-descriptor
412                   (function int int (* alien-stat)))
413
414
415 ;;; mode flags
416 (define-call "s_isreg" boolean never-fails (mode mode-t))
417 (define-call "s_isdir" boolean never-fails (mode mode-t))
418 (define-call "s_ischr" boolean never-fails (mode mode-t))
419 (define-call "s_isblk" boolean never-fails (mode mode-t))
420 (define-call "s_isfifo" boolean never-fails (mode mode-t))
421 (define-call "s_islnk" boolean never-fails (mode mode-t))
422 (define-call "s_issock" boolean never-fails (mode mode-t))
423
424 #-win32
425 (progn
426  (export 'pipe :sb-posix)
427  (declaim (inline pipe))
428  (defun pipe (&optional filedes2)
429    (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
430    (unless filedes2
431      (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
432    (let ((r (alien-funcall
433              ;; FIXME: (* INT)?  (ARRAY INT 2) would be better
434              (extern-alien "pipe" (function int (* int)))
435              (sb-sys:vector-sap filedes2))))
436      (when (minusp r)
437        (syscall-error)))
438    (values (aref filedes2 0) (aref filedes2 1))))
439
440 #-win32
441 (define-protocol-class termios alien-termios ()
442   ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
443    (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
444    (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
445    (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
446    (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
447
448 #-win32
449 (progn
450  (export 'tcsetattr :sb-posix)
451  (declaim (inline tcsetattr))
452  (defun tcsetattr (fd actions termios)
453    (declare (type termios termios))
454    (with-alien-termios a-termios ()
455      (termios-to-alien termios a-termios)
456      (let ((fd (file-descriptor fd)))
457        (let* ((r (alien-funcall
458                   (extern-alien
459                    "tcsetattr"
460                    (function int int int (* alien-termios)))
461                   fd actions a-termios)))
462          (when (minusp r)
463            (syscall-error)))
464        (values))))
465  (export 'tcgetattr :sb-posix)
466  (declaim (inline tcgetattr))
467  (defun tcgetattr (fd &optional termios)
468    (declare (type (or null termios) termios))
469    (with-alien-termios a-termios ()
470      (let ((r (alien-funcall
471                (extern-alien "tcgetattr"
472                              (function int int (* alien-termios)))
473                (file-descriptor fd)
474                a-termios)))
475        (when (minusp r)
476          (syscall-error))
477        (setf termios (alien-to-termios a-termios termios))))
478    termios)
479  (export 'cfsetispeed :sb-posix)
480  (declaim (inline cfsetispeed))
481  (defun cfsetispeed (speed &optional termios)
482    (declare (type (or null termios) termios))
483    (with-alien-termios a-termios ()
484      (let ((r (alien-funcall
485                (extern-alien "cfsetispeed"
486                              (function int (* alien-termios) speed-t))
487                a-termios
488                speed)))
489        (when (minusp r)
490          (syscall-error))
491        (setf termios (alien-to-termios a-termios termios))))
492    termios)
493  (export 'cfsetospeed :sb-posix)
494  (declaim (inline cfsetospeed))
495  (defun cfsetospeed (speed &optional termios)
496    (declare (type (or null termios) termios))
497    (with-alien-termios a-termios ()
498      (let ((r (alien-funcall
499                (extern-alien "cfsetospeed"
500                              (function int (* alien-termios) speed-t))
501                a-termios
502                speed)))
503        (when (minusp r)
504          (syscall-error))
505        (setf termios (alien-to-termios a-termios termios))))
506    termios)
507  (export 'cfgetispeed :sb-posix)
508  (declaim (inline cfgetispeed))
509  (defun cfgetispeed (termios)
510    (declare (type termios termios))
511    (with-alien-termios a-termios ()
512      (termios-to-alien termios a-termios)
513      (alien-funcall (extern-alien "cfgetispeed"
514                                   (function speed-t (* alien-termios)))
515                     a-termios)))
516  (export 'cfgetospeed :sb-posix)
517  (declaim (inline cfgetospeed))
518  (defun cfgetospeed (termios)
519    (declare (type termios termios))
520    (with-alien-termios a-termios ()
521      (termios-to-alien termios a-termios)
522      (alien-funcall (extern-alien "cfgetospeed"
523                                  (function speed-t (* alien-termios)))
524                     a-termios))))
525
526
527 #-win32
528 (progn
529   (export 'time :sb-posix)
530   (defun time ()
531     (let ((result (alien-funcall (extern-alien "time"
532                                                (function time-t (* time-t)))
533                                  nil)))
534       (if (minusp result)
535           (syscall-error)
536           result)))
537   (export 'utime :sb-posix)
538   (defun utime (filename &optional access-time modification-time)
539     (let ((fun (extern-alien "utime" (function int c-string
540                                                (* alien-utimbuf))))
541           (name (filename filename)))
542       (if (not (and access-time modification-time))
543           (alien-funcall fun name nil)
544           (with-alien ((utimbuf (struct alien-utimbuf)))
545             (setf (slot utimbuf 'actime) (or access-time 0)
546                   (slot utimbuf 'modtime) (or modification-time 0))
547             (let ((result (alien-funcall fun name (alien-sap utimbuf))))
548               (if (minusp result)
549                   (syscall-error)
550                   result))))))
551   (export 'utimes :sb-posix)
552   (defun utimes (filename &optional access-time modification-time)
553     (flet ((seconds-and-useconds (time)
554              (multiple-value-bind (integer fractional)
555                  (cl:truncate time)
556                (values integer (cl:truncate (* fractional 1000000)))))
557            (maybe-syscall-error (value)
558              (if (minusp value)
559                  (syscall-error)
560                  value)))
561       (let ((fun (extern-alien "utimes" (function int c-string
562                                                   (* (array alien-timeval 2)))))
563             (name (filename filename)))
564         (if (not (and access-time modification-time))
565             (maybe-syscall-error (alien-funcall fun name nil))
566             (with-alien ((buf (array alien-timeval 2)))
567               (let ((actime (deref buf 0))
568                     (modtime (deref buf 1)))
569                 (setf (values (slot actime 'sec)
570                               (slot actime 'usec))
571                       (seconds-and-useconds (or access-time 0))
572                       (values (slot modtime 'sec)
573                               (slot modtime 'usec))
574                       (seconds-and-useconds (or modification-time 0)))
575                 (maybe-syscall-error (alien-funcall fun name
576                                                     (alien-sap buf))))))))))
577
578
579 ;;; environment
580
581 (export 'getenv :sb-posix)
582 (defun getenv (name)
583   (let ((r (alien-funcall
584             (extern-alien "getenv" (function (* char) c-string))
585             name)))
586     (declare (type (alien (* char)) r))
587     (unless (null-alien r)
588       (cast r c-string))))
589 (define-call "putenv" int minusp (string c-string))
590
591 ;;; syslog
592 #-win32
593 (progn
594   (export 'openlog :sb-posix)
595   (export 'syslog :sb-posix)
596   (export 'closelog :sb-posix)
597   (defun openlog (ident options &optional (facility log-user))
598     (alien-funcall (extern-alien
599                     "openlog" (function void c-string int int))
600                    ident options facility))
601   (defun syslog (priority format &rest args)
602     "Send a message to the syslog facility, with severity level
603 PRIORITY.  The message will be formatted as by CL:FORMAT (rather
604 than C's printf) with format string FORMAT and arguments ARGS."
605     (flet ((syslog1 (priority message)
606              (alien-funcall (extern-alien
607                              "syslog" (function void int c-string c-string))
608                             priority "%s" message)))
609       (syslog1 priority (apply #'format nil format args))))
610   (define-call "closelog" void never-fails))