File: basic-unix.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (303 lines) | stat: -rw-r--r-- 8,345 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
;;;
;;; basic-unix.lisp --- CL-POSIX bindings.
;;;
;;; Copyright (C) 2005-2006, Matthew Backes  <lucca@accela.net>
;;; Copyright (C) 2005-2006, Dan Knapp  <dankna@accela.net> and
;;; Copyright (C) 2007, Stelian Ionescu  <stelian.ionescu-zeus@poste.it>
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without
;;; restriction, including without limitation the rights to use, copy,
;;; modify, merge, publish, distribute, sublicense, and/or sell copies
;;; of the Software, and to permit persons to whom the Software is
;;; furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;;; DEALINGS IN THE SOFTWARE.

(in-package #:osicat-posix)

#+windows (load-foreign-library "msvcrt.dll")

;;;; POSIX-ERROR

#+windows
(defcfun "strerror" :string
  "Look up the error message string for ERRNO. (reentrant)"
  (errnum :int))

;;; TODO: accept keywords too?
#-windows
(defun strerror (&optional (err (get-errno)))
  "Look up the error message string for ERRNO. (reentrant)"
  (let ((errno (if (keywordp err)
                   (foreign-enum-value 'errno-values err)
                   err)))
    #-linux ; XSI-compliant strerror_r() always stores the error
            ; message in the user-supplied buffer.
    (with-foreign-pointer-as-string ((buf bufsiz) 1024)
      (strerror-r errno buf bufsiz))
    #+linux ; GNU strerror_r() doesn't always store the error message
            ; in the user-supplied buffer, but it returns a string
            ; instead of an int.
    (with-foreign-pointer (buf 1024 bufsiz)
      (strerror-r errno buf bufsiz))))

(defmethod print-object ((posix-error posix-error) stream)
 (print-unreadable-object (posix-error stream :type t :identity nil)
   (let ((code (system-error-code posix-error))
         (identifier (system-error-identifier posix-error))
         (syscall (posix-error-syscall posix-error)))
     (format stream "~s ~s ~s ~s"
             (or syscall "[No syscall name]")
             (or code "[No code]") identifier
             (or (strerror code) "[Can't get error string.]")))))

;;;; stdlib.h

(defsyscall ("mktemp" %mktemp) :string
  (template :pointer))

(defun mktemp (&optional (template ""))
  (let ((template (concatenate 'string template "XXXXXX")))
    (with-foreign-string (ptr (filename template))
      (%mktemp ptr))))

;;;; string.h

(defcfun "memset" :pointer
  (buffer :pointer)
  (value  :int)
  (length size))

(defun bzero (buffer length)
  (memset buffer 0 length))

(defcfun "memcpy" :pointer
  (dest   :pointer)
  (src    :pointer)
  (length size))

(defcfun "memmove" :pointer
  (dest   :pointer)
  (src    :pointer)
  (length size))

;;;; unistd.h

;;; I/O

(defsyscall "read" ssize
  "Read at most COUNT bytes from FD into the foreign area BUF."
  (fd    file-descriptor-designator)
  (buf   :pointer)
  (count size))

(defsyscall "write" ssize
  "Write at most COUNT bytes to FD from the foreign area BUF."
  (fd    file-descriptor-designator)
  (buf   :pointer)
  (count size))

;;; directories

(defsyscall "chdir" :int
  "Changes the current working directory"
  (path filename-designator))

(defsyscall ("getcwd" %getcwd) :string
  (buf :pointer)
  (size size))

(defun getcwd ()
  "Returns the current working directory as a string."
  (with-foreign-pointer (buf path-max size)
    (%getcwd buf size)))

(defsyscall "rmdir" :int
  (path filename-designator))

;;; files

(defsyscall ("open" %open) :int
  (pathname filename-designator)
  (flags    :int)
  (mode     mode))

(defvar *default-open-mode* #o666)

(defun open (pathname flags &optional (mode *default-open-mode*))
  ;; Let's just use O_BINARY always unless there's a good reason not to.
  #+windows (setq flags (logior flags o-binary))
  (cond
    ((integerp mode) (%open pathname flags mode))
    (t (error "Wrong mode: ~S" mode))))

(defsyscall "creat" :int
  (pathname filename-designator)
  (mode     mode))

(defsyscall ("pipe" %pipe) :int
  (filedes :pointer))

(defun pipe ()
  "Create pipe, returns two values with the new FDs."
  (with-foreign-object (filedes :int 2)
    (%pipe filedes)
    (values (mem-aref filedes :int 0)
            (mem-aref filedes :int 1))))

(defcsyscall "rename" :int
  "Rename a file."
  (old filename-designator)
  (new filename-designator))

(defsyscall "close" :int
  "Close an open file descriptor."
  (fd file-descriptor-designator))

(defsyscall "unlink" :int
  (path filename-designator))

(defsyscall "access" :int
  (path  filename-designator)
  (amode :int))

(defsyscall "dup" :int
  (fildes file-descriptor-designator))

(defsyscall "dup2" :int
  (fildes1 file-descriptor-designator)
  (fildes2 file-descriptor-designator))

(defsyscall "chmod" :int
  (path filename-designator)
  (mode mode))

(defsyscall ("utime" %utime) :int
  (filename filename-designator)
  (times (:pointer (:struct utimbuf))))

(defun utime (filename atime mtime)
  (with-foreign-object (times '(:struct utimbuf))
    (setf (foreign-slot-value times '(:struct utimbuf) 'actime) atime
          (foreign-slot-value times '(:struct utimbuf) 'modtime) mtime)
    (%utime filename times)))

;;; environment

(defcvar (#+windows "_environ" #-windows "environ" *environ* :read-only t) (:pointer :string))

(defcfun "getenv" :string
  "Returns the value of an environment variable"
  (name :string))

(defsyscall "putenv" :int
  (string :string))

;;; time

(defsyscall "sleep" :unsigned-int
  (seconds :unsigned-int))

;;;; sys/time.h

(defcsyscall ("time" %time) time
  (tloc :pointer))

(defun time ()
  (%time (null-pointer)))

;;;; sys/stat.h

(define-c-struct-wrapper stat ())

;;; If necessary for performance reasons, we can add an optional
;;; argument to this function and use that to reuse a wrapper object.
(defun funcall-stat (fn arg)
  (with-foreign-object (buf '(:struct stat))
    (funcall fn arg buf)
    (make-instance 'stat :pointer buf)))

;;; Windows supports only seconds.
#+windows
(progn
  (defun stat-atime-sec (stat)
    (stat-atime stat))
  (defun stat-atime-nsec (stat)
    (declare (ignore stat))
    0)

  (defun stat-mtime-sec (stat)
    (stat-mtime stat))
  (defun stat-mtime-nsec (stat)
    (declare (ignore stat))
    0)

  (defun stat-ctime-sec (stat)
    (stat-ctime stat))
  (defun stat-ctime-nsec (stat)
    (declare (ignore stat))
    0))

#-windows
(progn
  (defun stat-atime (stat)
    (getf (stat-atimespec stat) 'sec))
  (defun stat-atime-sec (stat)
    (getf (stat-atimespec stat) 'sec))
  (defun stat-atime-nsec (stat)
    (getf (stat-atimespec stat) 'nsec))

  (defun stat-mtime (stat)
    (getf (stat-mtimespec stat) 'sec))
  (defun stat-mtime-sec (stat)
    (getf (stat-mtimespec stat) 'sec))
  (defun stat-mtime-nsec (stat)
    (getf (stat-mtimespec stat) 'nsec))

  (defun stat-ctime (stat)
    (getf (stat-ctimespec stat) 'sec))
  (defun stat-ctime-sec (stat)
    (getf (stat-ctimespec stat) 'sec))
  (defun stat-ctime-nsec (stat)
    (getf (stat-ctimespec stat) 'nsec)))

(defun stat (path)
  "Get information about a file."
  (funcall-stat #'%stat path))

(defun fstat (fd)
  "Get information about a file descriptor"
  (funcall-stat #'%fstat fd))

(defsyscall "umask" mode
  "Sets the umask and returns the old one"
  (new-mode mode))

(defsyscall "mkdir" :int
  "Create a directory."
  (path filename-designator)
  (mode mode))

(defsyscall ("realpath" %realpath) :string
  "Returns the canonicalized absolute pathname"  
  (path filename-designator)
  (resolved-name :string))

(defun realpath (path)
  "Returns the canonicalized absolute pathname"
  (with-foreign-pointer (resolved-name path-max)
    (%realpath path resolved-name)))