1.0.6.16: add SB-POSIX:GETCWD
[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 "lockf" int minusp (fd file-descriptor) (cmd int) (len off-t))
149   (define-call "mkfifo" int minusp (pathname filename) (mode mode-t))
150   (define-call "symlink" int minusp (oldpath filename) (newpath filename))
151   (define-call "sync" void never-fails)
152   (define-call ("truncate" :options :largefile)
153       int minusp (pathname filename) (length off-t))
154   ;; FIXME: Windows does have _mktemp, which has a slightlty different
155   ;; interface
156   (defun mkstemp (template)
157     ;; we are emulating sb-alien's charset conversion for strings
158     ;; here, to accommodate for the call-by-reference nature of
159     ;; mkstemp's template strings.
160     (let ((arg (sb-ext:string-to-octets
161                 (filename template)
162                 :external-format sb-alien::*default-c-string-external-format*)))
163       (sb-sys:with-pinned-objects (arg)
164         (let ((result (alien-funcall (extern-alien "mkstemp"
165                                                    (function int c-string))
166                                      (sap-alien (sb-alien::vector-sap arg)
167                                                 (* char)))))
168           (when (minusp result)
169             (syscall-error))
170           (values result
171                   (sb-ext:octets-to-string
172                    arg
173                    :external-format sb-alien::*default-c-string-external-format*))))))
174   (define-call-internally ioctl-without-arg "ioctl" int minusp
175                           (fd file-descriptor) (cmd int))
176   (define-call-internally ioctl-with-int-arg "ioctl" int minusp
177                           (fd file-descriptor) (cmd int) (arg int))
178   (define-call-internally ioctl-with-pointer-arg "ioctl" int minusp
179                           (fd file-descriptor) (cmd int)
180                           (arg alien-pointer-to-anything-or-nil))
181   (define-entry-point "ioctl" (fd cmd &optional (arg nil argp))
182     (if argp
183         (etypecase arg
184           ((alien int) (ioctl-with-int-arg fd cmd arg))
185           ((or (alien (* t)) null) (ioctl-with-pointer-arg fd cmd arg)))
186         (ioctl-without-arg fd cmd)))
187   (define-call-internally fcntl-without-arg "fcntl" int minusp
188                           (fd file-descriptor) (cmd int))
189   (define-call-internally fcntl-with-int-arg "fcntl" int minusp
190                           (fd file-descriptor) (cmd int) (arg int))
191   (define-call-internally fcntl-with-pointer-arg "fcntl" int minusp
192                           (fd file-descriptor) (cmd int)
193                           (arg alien-pointer-to-anything-or-nil))
194   (define-entry-point "fcntl" (fd cmd &optional (arg nil argp))
195     (if argp
196         (etypecase arg
197           ((alien int) (fcntl-with-int-arg fd cmd arg))
198           ((or (alien (* t)) null) (fcntl-with-pointer-arg fd cmd arg)))
199         (fcntl-without-arg fd cmd)))
200
201   ;; uid, gid
202   (define-call "geteuid" uid-t never-fails) ; "always successful", it says
203   (define-call "getresuid" uid-t never-fails)
204   (define-call "getuid" uid-t never-fails)
205   (define-call "seteuid" int minusp (uid uid-t))
206   (define-call "setfsuid" int minusp (uid uid-t))
207   (define-call "setreuid" int minusp (ruid uid-t) (euid uid-t))
208   (define-call "setresuid" int minusp (ruid uid-t) (euid uid-t) (suid uid-t))
209   (define-call "setuid" int minusp (uid uid-t))
210   (define-call "getegid" gid-t never-fails)
211   (define-call "getgid" gid-t never-fails)
212   (define-call "getresgid" gid-t never-fails)
213   (define-call "setegid" int minusp (gid gid-t))
214   (define-call "setfsgid" int minusp (gid gid-t))
215   (define-call "setgid" int minusp (gid gid-t))
216   (define-call "setregid" int minusp (rgid gid-t) (egid gid-t))
217   (define-call "setresgid" int minusp (rgid gid-t) (egid gid-t) (sgid gid-t))
218
219   ;; processes, signals
220   (define-call "alarm" int never-fails (seconds unsigned))
221
222
223
224   #+mach-exception-handler
225   (progn
226     ;; FIXME this is a lie, of course this can fail, but there's no
227     ;; error handling here yet!
228     (define-call "setup_mach_exceptions" void never-fails)
229     (define-call ("posix_fork" :c-name "fork") pid-t minusp)
230     (defun fork ()
231       (let ((pid (posix-fork)))
232         (when (= pid 0)
233           (setup-mach-exceptions))
234         pid))
235     (export 'fork :sb-posix))
236
237   #-mach-exception-handler
238   (define-call "fork" pid-t minusp)
239
240   (define-call "getpgid" pid-t minusp (pid pid-t))
241   (define-call "getppid" pid-t never-fails)
242   (define-call "getpgrp" pid-t never-fails)
243   (define-call "getsid" pid-t minusp  (pid pid-t))
244   (define-call "kill" int minusp (pid pid-t) (signal int))
245   (define-call "killpg" int minusp (pgrp int) (signal int))
246   (define-call "pause" int minusp)
247   (define-call "setpgid" int minusp (pid pid-t) (pgid pid-t))
248   (define-call "setpgrp" int minusp))
249
250 (defmacro with-growing-c-string ((buffer size) &body body)
251   (sb-int:with-unique-names (c-string-block)
252     `(block ,c-string-block
253        (let (,buffer)
254          (flet ((,buffer (&optional (size-incl-null))
255                   (when size-incl-null
256                     (setf (sb-sys:sap-ref-8 (sb-alien:alien-sap ,buffer) size-incl-null)
257                           0))
258                   (return-from ,c-string-block
259                     (sb-alien::c-string-to-string
260                      (sb-alien:alien-sap ,buffer)
261                      (sb-impl::default-external-format)
262                      'character))))
263            (loop for ,size = 128 then (* 2 ,size)
264                  do (unwind-protect
265                          (progn
266                            (setf ,buffer (make-alien c-string ,size))
267                            ,@body)
268                       (when ,buffer
269                         (free-alien ,buffer)))))))))
270
271 #-win32
272 (progn
273   (export 'readlink :sb-posix)
274   (defun readlink (pathspec)
275     (flet ((%readlink (path buf length)
276              (alien-funcall
277               (extern-alien "readlink" (function int c-string (* t) int))
278               path buf length)))
279       (with-growing-c-string (buf size)
280         (let ((count (%readlink (filename pathspec) buf size)))
281           (cond ((minusp count)
282                  (syscall-error))
283                 ((< 0 count size)
284                  (buf count))))))))
285
286 (progn
287   (export 'getcwd :sb-posix)
288   (defun getcwd ()
289     (flet ((%getcwd (buffer size)
290              (alien-funcall
291               (extern-alien "getcwd" (function c-string (* t) int))
292               buffer size)))
293       (with-growing-c-string (buf size)
294         (let ((result (%getcwd buf size)))
295           (cond (result
296                  (buf))
297                 ((/= (get-errno) sb-posix:erange)
298                  (syscall-error))))))))
299
300 #-win32
301 (progn
302  (export 'wait :sb-posix)
303  (declaim (inline wait))
304  (defun wait (&optional statusptr)
305    (declare (type (or null (simple-array (signed-byte 32) (1))) statusptr))
306    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
307           (pid (alien-funcall
308                 (extern-alien "wait" (function pid-t (* int)))
309                 (sb-sys:vector-sap ptr))))
310      (if (minusp pid)
311          (syscall-error)
312          (values pid (aref ptr 0))))))
313
314 #-win32
315 (progn
316  (export 'waitpid :sb-posix)
317  (declaim (inline waitpid))
318  (defun waitpid (pid options &optional statusptr)
319    (declare (type (sb-alien:alien pid-t) pid)
320             (type (sb-alien:alien int) options)
321             (type (or null (simple-array (signed-byte 32) (1))) statusptr))
322    (let* ((ptr (or statusptr (make-array 1 :element-type '(signed-byte 32))))
323           (pid (alien-funcall
324                 (extern-alien "waitpid" (function pid-t
325                                                   pid-t (* int) int))
326                 pid (sb-sys:vector-sap ptr) options)))
327      (if (minusp pid)
328          (syscall-error)
329          (values pid (aref ptr 0)))))
330  ;; waitpid macros
331  (define-call "wifexited" boolean never-fails (status int))
332  (define-call "wexitstatus" int never-fails (status int))
333  (define-call "wifsignaled" boolean never-fails (status int))
334  (define-call "wtermsig" int never-fails (status int))
335  (define-call "wifstopped" boolean never-fails (status int))
336  (define-call "wstopsig" int never-fails (status int))
337  #+nil ; see alien/waitpid-macros.c
338  (define-call "wifcontinued" boolean never-fails (status int)))
339
340 ;;; mmap, msync
341 #-win32
342 (progn
343  (define-call ("mmap" :options :largefile) sb-sys:system-area-pointer
344    (lambda (res)
345      (= (sb-sys:sap-int res) #.(1- (expt 2 sb-vm::n-machine-word-bits))))
346    (addr sap-or-nil) (length unsigned) (prot unsigned)
347    (flags unsigned) (fd file-descriptor) (offset off-t))
348
349  (define-call "munmap" int minusp
350    (start sb-sys:system-area-pointer) (length unsigned))
351
352 (define-call "msync" int minusp
353   (addr sb-sys:system-area-pointer) (length unsigned) (flags int)))
354
355 #-win32
356 (define-call "getpagesize" int minusp)
357 #+win32
358 ;;; KLUDGE: This could be taken from GetSystemInfo
359 (export (defun getpagesize () 4096))
360
361 ;;; passwd database
362 #-win32
363 (define-protocol-class passwd alien-passwd ()
364   ((name :initarg :name :accessor passwd-name)
365    (passwd :initarg :passwd :accessor passwd-passwd)
366    (uid :initarg :uid :accessor passwd-uid)
367    (gid :initarg :gid :accessor passwd-gid)
368    (gecos :initarg :gecos :accessor passwd-gecos)
369    (dir :initarg :dir :accessor passwd-dir)
370    (shell :initarg :shell :accessor passwd-shell)))
371
372 (defmacro define-pw-call (name arg type)
373   #-win32
374   ;; FIXME: this isn't the documented way of doing this, surely?
375   (let ((lisp-name (intern (string-upcase name) :sb-posix)))
376     `(progn
377       (export ',lisp-name :sb-posix)
378       (declaim (inline ,lisp-name))
379       (defun ,lisp-name (,arg)
380         (let ((r (alien-funcall (extern-alien ,name ,type) ,arg)))
381           (if (null r)
382               r
383               (alien-to-passwd r)))))))
384
385 (define-pw-call "getpwnam" login-name (function (* alien-passwd) c-string))
386 (define-pw-call "getpwuid" uid (function (* alien-passwd) uid-t))
387
388 #-win32
389 (define-protocol-class timeval alien-timeval ()
390   ((sec :initarg :tv-sec :accessor timeval-sec)
391    (usec :initarg :tv-usec :accessor timeval-usec)))
392
393 (define-protocol-class stat alien-stat ()
394   ((mode :initarg :mode :accessor stat-mode)
395    (ino :initarg :ino :accessor stat-ino)
396    (dev :initarg :dev :accessor stat-dev)
397    (nlink :initarg :nlink :accessor stat-nlink)
398    (uid :initarg :uid :accessor stat-uid)
399    (gid :initarg :gid :accessor stat-gid)
400    (size :initarg :size :accessor stat-size)
401    (atime :initarg :atime :accessor stat-atime)
402    (mtime :initarg :mtime :accessor stat-mtime)
403    (ctime :initarg :ctime :accessor stat-ctime)))
404
405 (defmacro define-stat-call (name arg designator-fun type)
406   ;; FIXME: this isn't the documented way of doing this, surely?
407   (let ((lisp-name (lisp-for-c-symbol name)))
408     `(progn
409       (export ',lisp-name :sb-posix)
410       (declaim (inline ,lisp-name))
411       (defun ,lisp-name (,arg &optional stat)
412         (declare (type (or null (sb-alien:alien (* alien-stat))) stat))
413         (with-alien-stat a-stat ()
414           (let ((r (alien-funcall
415                     (extern-alien ,(real-c-name (list name :options :largefile)) ,type)
416                     (,designator-fun ,arg)
417                     a-stat)))
418             (when (minusp r)
419               (syscall-error))
420             (alien-to-stat a-stat stat)))))))
421
422 (define-stat-call #-win32 "stat" #+win32 "_stat"
423                   pathname filename
424                   (function int c-string (* alien-stat)))
425
426 #-win32
427 (define-stat-call "lstat"
428                   pathname filename
429                   (function int c-string (* alien-stat)))
430 ;;; No symbolic links on Windows, so use stat
431 #+win32
432 (progn
433   (declaim (inline lstat))
434   (export (defun lstat (filename &optional stat)
435             (if stat (stat filename stat) (stat filename)))))
436
437 (define-stat-call #-win32 "fstat" #+win32 "_fstat"
438                   fd file-descriptor
439                   (function int int (* alien-stat)))
440
441
442 ;;; mode flags
443 (define-call "s_isreg" boolean never-fails (mode mode-t))
444 (define-call "s_isdir" boolean never-fails (mode mode-t))
445 (define-call "s_ischr" boolean never-fails (mode mode-t))
446 (define-call "s_isblk" boolean never-fails (mode mode-t))
447 (define-call "s_isfifo" boolean never-fails (mode mode-t))
448 (define-call "s_islnk" boolean never-fails (mode mode-t))
449 (define-call "s_issock" boolean never-fails (mode mode-t))
450
451 #-win32
452 (progn
453  (export 'pipe :sb-posix)
454  (declaim (inline pipe))
455  (defun pipe (&optional filedes2)
456    (declare (type (or null (simple-array (signed-byte 32) (2))) filedes2))
457    (unless filedes2
458      (setq filedes2 (make-array 2 :element-type '(signed-byte 32))))
459    (let ((r (alien-funcall
460              ;; FIXME: (* INT)?  (ARRAY INT 2) would be better
461              (extern-alien "pipe" (function int (* int)))
462              (sb-sys:vector-sap filedes2))))
463      (when (minusp r)
464        (syscall-error)))
465    (values (aref filedes2 0) (aref filedes2 1))))
466
467 #-win32
468 (define-protocol-class termios alien-termios ()
469   ((iflag :initarg :iflag :accessor sb-posix:termios-iflag)
470    (oflag :initarg :oflag :accessor sb-posix:termios-oflag)
471    (cflag :initarg :cflag :accessor sb-posix:termios-cflag)
472    (lflag :initarg :lflag :accessor sb-posix:termios-lflag)
473    (cc :initarg :cc :accessor sb-posix:termios-cc :array-length nccs)))
474
475 #-win32
476 (progn
477  (export 'tcsetattr :sb-posix)
478  (declaim (inline tcsetattr))
479  (defun tcsetattr (fd actions termios)
480    (declare (type termios termios))
481    (with-alien-termios a-termios ()
482      (termios-to-alien termios a-termios)
483      (let ((fd (file-descriptor fd)))
484        (let* ((r (alien-funcall
485                   (extern-alien
486                    "tcsetattr"
487                    (function int int int (* alien-termios)))
488                   fd actions a-termios)))
489          (when (minusp r)
490            (syscall-error)))
491        (values))))
492  (export 'tcgetattr :sb-posix)
493  (declaim (inline tcgetattr))
494  (defun tcgetattr (fd &optional termios)
495    (declare (type (or null termios) termios))
496    (with-alien-termios a-termios ()
497      (let ((r (alien-funcall
498                (extern-alien "tcgetattr"
499                              (function int int (* alien-termios)))
500                (file-descriptor fd)
501                a-termios)))
502        (when (minusp r)
503          (syscall-error))
504        (setf termios (alien-to-termios a-termios termios))))
505    termios)
506  (export 'cfsetispeed :sb-posix)
507  (declaim (inline cfsetispeed))
508  (defun cfsetispeed (speed &optional termios)
509    (declare (type (or null termios) termios))
510    (with-alien-termios a-termios ()
511      (let ((r (alien-funcall
512                (extern-alien "cfsetispeed"
513                              (function int (* alien-termios) speed-t))
514                a-termios
515                speed)))
516        (when (minusp r)
517          (syscall-error))
518        (setf termios (alien-to-termios a-termios termios))))
519    termios)
520  (export 'cfsetospeed :sb-posix)
521  (declaim (inline cfsetospeed))
522  (defun cfsetospeed (speed &optional termios)
523    (declare (type (or null termios) termios))
524    (with-alien-termios a-termios ()
525      (let ((r (alien-funcall
526                (extern-alien "cfsetospeed"
527                              (function int (* alien-termios) speed-t))
528                a-termios
529                speed)))
530        (when (minusp r)
531          (syscall-error))
532        (setf termios (alien-to-termios a-termios termios))))
533    termios)
534  (export 'cfgetispeed :sb-posix)
535  (declaim (inline cfgetispeed))
536  (defun cfgetispeed (termios)
537    (declare (type termios termios))
538    (with-alien-termios a-termios ()
539      (termios-to-alien termios a-termios)
540      (alien-funcall (extern-alien "cfgetispeed"
541                                   (function speed-t (* alien-termios)))
542                     a-termios)))
543  (export 'cfgetospeed :sb-posix)
544  (declaim (inline cfgetospeed))
545  (defun cfgetospeed (termios)
546    (declare (type termios termios))
547    (with-alien-termios a-termios ()
548      (termios-to-alien termios a-termios)
549      (alien-funcall (extern-alien "cfgetospeed"
550                                  (function speed-t (* alien-termios)))
551                     a-termios))))
552
553
554 #-win32
555 (progn
556   (export 'time :sb-posix)
557   (defun time ()
558     (let ((result (alien-funcall (extern-alien "time"
559                                                (function time-t (* time-t)))
560                                  nil)))
561       (if (minusp result)
562           (syscall-error)
563           result)))
564   (export 'utime :sb-posix)
565   (defun utime (filename &optional access-time modification-time)
566     (let ((fun (extern-alien "utime" (function int c-string
567                                                (* alien-utimbuf))))
568           (name (filename filename)))
569       (if (not (and access-time modification-time))
570           (alien-funcall fun name nil)
571           (with-alien ((utimbuf (struct alien-utimbuf)))
572             (setf (slot utimbuf 'actime) (or access-time 0)
573                   (slot utimbuf 'modtime) (or modification-time 0))
574             (let ((result (alien-funcall fun name (alien-sap utimbuf))))
575               (if (minusp result)
576                   (syscall-error)
577                   result))))))
578   (export 'utimes :sb-posix)
579   (defun utimes (filename &optional access-time modification-time)
580     (flet ((seconds-and-useconds (time)
581              (multiple-value-bind (integer fractional)
582                  (cl:truncate time)
583                (values integer (cl:truncate (* fractional 1000000)))))
584            (maybe-syscall-error (value)
585              (if (minusp value)
586                  (syscall-error)
587                  value)))
588       (let ((fun (extern-alien "utimes" (function int c-string
589                                                   (* (array alien-timeval 2)))))
590             (name (filename filename)))
591         (if (not (and access-time modification-time))
592             (maybe-syscall-error (alien-funcall fun name nil))
593             (with-alien ((buf (array alien-timeval 2)))
594               (let ((actime (deref buf 0))
595                     (modtime (deref buf 1)))
596                 (setf (values (slot actime 'sec)
597                               (slot actime 'usec))
598                       (seconds-and-useconds (or access-time 0))
599                       (values (slot modtime 'sec)
600                               (slot modtime 'usec))
601                       (seconds-and-useconds (or modification-time 0)))
602                 (maybe-syscall-error (alien-funcall fun name
603                                                     (alien-sap buf))))))))))
604
605
606 ;;; environment
607
608 (export 'getenv :sb-posix)
609 (defun getenv (name)
610   (let ((r (alien-funcall
611             (extern-alien "getenv" (function (* char) c-string))
612             name)))
613     (declare (type (alien (* char)) r))
614     (unless (null-alien r)
615       (cast r c-string))))
616 (define-call "putenv" int minusp (string c-string))
617
618 ;;; syslog
619 #-win32
620 (progn
621   (export 'openlog :sb-posix)
622   (export 'syslog :sb-posix)
623   (export 'closelog :sb-posix)
624   (defun openlog (ident options &optional (facility log-user))
625     (alien-funcall (extern-alien
626                     "openlog" (function void c-string int int))
627                    ident options facility))
628   (defun syslog (priority format &rest args)
629     "Send a message to the syslog facility, with severity level
630 PRIORITY.  The message will be formatted as by CL:FORMAT (rather
631 than C's printf) with format string FORMAT and arguments ARGS."
632     (flet ((syslog1 (priority message)
633              (alien-funcall (extern-alien
634                              "syslog" (function void int c-string c-string))
635                             priority "%s" message)))
636       (syslog1 priority (apply #'format nil format args))))
637   (define-call "closelog" void never-fails))