Add wrappers for directory walking syscalls: opendir(), readdir_r(), etc ...
[iolib.git] / syscalls / ffi-types-unix.lisp
blob766575b9fca420d0d1406b9d1fe79a7c5c38a115
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Grovel definitions for *NIX systems.
4 ;;;
6 #+linux
7 (define "_GNU_SOURCE")
9 ;;; largefile support on linux
10 ;;; TODO: check if these flags are required on solaris too
11 #+linux
12 (progn
13 (define "_LARGEFILE_SOURCE")
14 (define "_LARGEFILE64_SOURCE")
15 (define "_FILE_OFFSET_BITS" 64))
17 (include "stdlib.h" "errno.h" "sys/types.h" "sys/stat.h" "sys/mman.h"
18 "fcntl.h" "signal.h" "unistd.h" "limits.h" "time.h" "sys/select.h"
19 "sys/poll.h" "sys/ioctl.h" "sys/resource.h" "pwd.h" "grp.h"
20 "dirent.h")
22 (in-package :iolib.syscalls)
24 (ctype size-t "size_t")
25 (ctype ssize-t "ssize_t")
26 (ctype pid-t "pid_t")
27 (ctype uid-t "uid_t")
28 (ctype gid-t "gid_t")
29 (ctype off-t "off_t")
30 (ctype mode-t "mode_t")
32 (constantenum (errno-values :define-constants t)
33 ((:eperm "EPERM"))
34 ((:enoent "ENOENT"))
35 ((:esrch "ESRCH"))
36 ((:eintr "EINTR"))
37 ((:eio "EIO"))
38 ((:enxio "ENXIO"))
39 ((:e2big "E2BIG"))
40 ((:enoexec "ENOEXEC"))
41 ((:ebadf "EBADF"))
42 ((:echild "ECHILD"))
43 ((:eagain "EAGAIN"))
44 ((:enomem "ENOMEM"))
45 ((:eacces "EACCES"))
46 ((:efault "EFAULT"))
47 ((:ebusy "EBUSY"))
48 ((:eexist "EEXIST"))
49 ((:exdev "EXDEV"))
50 ((:enodev "ENODEV"))
51 ((:enotdir "ENOTDIR"))
52 ((:eisdir "EISDIR"))
53 ((:einval "EINVAL"))
54 ((:enfile "ENFILE"))
55 ((:emfile "EMFILE"))
56 ((:enotty "ENOTTY"))
57 ((:efbig "EFBIG"))
58 ((:enospc "ENOSPC"))
59 ((:espipe "ESPIPE"))
60 ((:erofs "EROFS"))
61 ((:emlink "EMLINK"))
62 ((:epipe "EPIPE"))
63 ((:edom "EDOM"))
64 ((:erange "ERANGE"))
65 ((:edeadlk "EDEADLK"))
66 ((:enametoolong "ENAMETOOLONG"))
67 ((:enolck "ENOLCK"))
68 ((:enosys "ENOSYS"))
69 ((:enotempty "ENOTEMPTY"))
70 ((:echrng "ECHRNG") :optional t)
71 ((:el2nsync "EL2NSYNC") :optional t)
72 ((:el3hlt "EL3HLT") :optional t)
73 ((:el3rst "EL3RST") :optional t)
74 ((:elnrng "ELNRNG") :optional t)
75 ((:eunatch "EUNATCH") :optional t)
76 ((:enocsi "ENOCSI") :optional t)
77 ((:el2hlt "EL2HLT") :optional t)
78 ((:ebade "EBADE") :optional t)
79 ((:ebadr "EBADR") :optional t)
80 ((:exfull "EXFULL") :optional t)
81 ((:enoano "ENOANO") :optional t)
82 ((:ebadrqc "EBADRQC") :optional t)
83 ((:ebadslt "EBADSLT") :optional t)
84 ((:edeadlock "EDEADLOCK") :optional t)
85 ((:ebfont "EBFONT") :optional t)
86 ((:enostr "ENOSTR") :optional t)
87 ((:enodata "ENODATA") :optional t)
88 ((:etime "ETIME") :optional t)
89 ((:enosr "ENOSR") :optional t)
90 ((:enopkg "ENOPKG") :optional t)
91 ((:eadv "EADV") :optional t)
92 ((:esrmnt "ESRMNT") :optional t)
93 ((:ecomm "ECOMM") :optional t)
94 ((:edotdot "EDOTDOT") :optional t)
95 ((:enotuniq "ENOTUNIQ") :optional t)
96 ((:ebadfd "EBADFD") :optional t)
97 ((:eremchg "EREMCHG") :optional t)
98 ((:elibacc "ELIBACC") :optional t)
99 ((:elibbad "ELIBBAD") :optional t)
100 ((:elibscn "ELIBSCN") :optional t)
101 ((:elibmax "ELIBMAX") :optional t)
102 ((:elibexec "ELIBEXEC") :optional t)
103 ((:eilseq "EILSEQ"))
104 ((:erestart "ERESTART") :optional t)
105 ((:estrpipe "ESTRPIPE") :optional t)
106 ((:euclean "EUCLEAN") :optional t)
107 ((:enotnam "ENOTNAM") :optional t)
108 ((:enavail "ENAVAIL") :optional t)
109 ((:eremoteio "EREMOTEIO") :optional t)
110 ((:enomedium "ENOMEDIUM") :optional t)
111 ((:emediumtype "EMEDIUMTYPE") :optional t)
112 ((:estale "ESTALE"))
113 ((:enotblk "ENOTBLK"))
114 ((:etxtbsy "ETXTBSY"))
115 ((:eusers "EUSERS"))
116 ((:eloop "ELOOP"))
117 ((:ewouldblock "EWOULDBLOCK"))
118 ((:enomsg "ENOMSG"))
119 ((:eidrm "EIDRM"))
120 ((:eproto "EPROTO"))
121 ((:emultihop "EMULTIHOP"))
122 ((:ebadmsg "EBADMSG"))
123 ((:eoverflow "EOVERFLOW"))
124 ((:edquot "EDQUOT"))
125 ((:einprogress "EINPROGRESS"))
126 ((:ealready "EALREADY"))
127 ;; TODO: These errors are related to sockets. However they
128 ;; might not be unique to them. Remove those that are unique
129 ;; and keep those that might be set elsewhere.
130 ((:eprotonosupport "EPROTONOSUPPORT"))
131 ((:esocktnosupport "ESOCKTNOSUPPORT"))
132 ((:enotsock "ENOTSOCK"))
133 ((:edestaddrreq "EDESTADDRREQ"))
134 ((:emsgsize "EMSGSIZE"))
135 ((:eprototype "EPROTOTYPE"))
136 ((:enoprotoopt "ENOPROTOOPT"))
137 ((:eremote "EREMOTE"))
138 ((:enolink "ENOLINK"))
139 ((:epfnosupport "EPFNOSUPPORT"))
140 ((:eafnosupport "EAFNOSUPPORT"))
141 ((:eaddrinuse "EADDRINUSE"))
142 ((:eaddrnotavail "EADDRNOTAVAIL"))
143 ((:enetdown "ENETDOWN"))
144 ((:enetunreach "ENETUNREACH"))
145 ((:enetreset "ENETRESET"))
146 ((:econnaborted "ECONNABORTED"))
147 ((:econnreset "ECONNRESET"))
148 ((:eisconn "EISCONN"))
149 ((:enotconn "ENOTCONN"))
150 ((:eshutdown "ESHUTDOWN"))
151 ((:etoomanyrefs "ETOOMANYREFS"))
152 ((:etimedout "ETIMEDOUT"))
153 ((:econnrefused "ECONNREFUSED"))
154 ((:ehostdown "EHOSTDOWN"))
155 ((:ehostunreach "EHOSTUNREACH"))
156 ((:enonet "ENONET") :optional t)
157 ((:enobufs "ENOBUFS"))
158 ((:eopnotsupp "EOPNOTSUPP")))
160 ;;; open()
161 (constant (o-rdonly "O_RDONLY"))
162 (constant (o-wronly "O_WRONLY"))
163 (constant (o-rdwr "O_RDWR"))
164 (constant (o-creat "O_CREAT"))
165 (constant (o-excl "O_EXCL"))
166 (constant (o-trunc "O_TRUNC"))
167 (constant (o-append "O_APPEND"))
169 (constant (o-noctty "O_NOCTTY"))
170 (constant (o-nonblock "O_NONBLOCK"))
171 (constant (o-ndelay "O_NDELAY"))
172 (constant (o-sync "O_SYNC"))
173 (constant (o-nofollow "O_NOFOLLOW"))
174 (constant (o-async "O_ASYNC"))
176 ;;; lseek()
177 (constant (seek-set "SEEK_SET"))
178 (constant (seek-cur "SEEK_CUR"))
179 (constant (seek-end "SEEK_END"))
181 ;;; access()
182 (constant (r-ok "R_OK"))
183 (constant (w-ok "W_OK"))
184 (constant (x-ok "X_OK"))
185 (constant (f-ok "F_OK"))
187 ;;;; stat()
189 (constant (s-irwxu "S_IRWXU")
190 :documentation "read, write, execute/search by owner")
191 (constant (s-irusr "S_IRUSR") :documentation "read permission, owner")
192 (constant (s-iwusr "S_IWUSR") :documentation "write permission, owner")
193 (constant (s-ixusr "S_IXUSR") :documentation "execute/search permission, owner")
194 (constant (s-ifmt "S_IFMT") :documentation "bitmask for type of entry")
195 (constant (s-ififo "S_IFIFO") :documentation "named pipe, aka fifo")
196 (constant (s-ifchr "S_IFCHR") :documentation "special character-device")
197 (constant (s-ifdir "S_IFDIR") :documentation "directory")
198 (constant (s-ifblk "S_IFBLK") :documentation "special block-device")
199 (constant (s-ifreg "S_IFREG") :documentation "regular file")
200 (constant (s-ifwht "S_IFWHT") :documentation "whiteout" :optional t)
201 (constant (s-iread "S_IREAD"))
202 (constant (s-iwrite "S_IWRITE"))
203 (constant (s-iexec "S_IEXEC"))
205 (constant (s-irwxg "S_IRWXG")
206 :documentation "read, write, execute/search by group")
207 (constant (s-irgrp "S_IRGRP") :documentation "read permission, group")
208 (constant (s-iwgrp "S_IWGRP") :documentation "write permission, group")
209 (constant (s-ixgrp "S_IXGRP")
210 :documentation "execute/search permission, group")
211 (constant (s-irwxo "S_IRWXO")
212 :documentation "read, write, execute/search by others")
213 (constant (s-iroth "S_IROTH") :documentation "read permission, others")
214 (constant (s-iwoth "S_IWOTH") :documentation "write permission, others")
215 (constant (s-ixoth "S_IXOTH")
216 :documentation "execute/search permission, others")
217 (constant (s-isuid "S_ISUID") :documentation "set-user-ID on execution")
218 (constant (s-isgid "S_ISGID") :documentation "set-group-ID on execution")
219 (constant (s-isvtx "S_ISVTX")
220 :documentation "'sticky' bit, many meanings, nonportable")
221 (constant (s-iflnk "S_IFLNK") :documentation "symbolic link")
222 (constant (s-ifsock "S_IFSOCK") :documentation "socket")
224 (constant (path-max "PATH_MAX" "MAXPATHLEN"))
226 ;;;; from unistd.h
228 (ctype useconds-t "useconds_t")
230 ;;;; from time.h
232 (ctype time-t "time_t")
233 (ctype suseconds-t "suseconds_t")
235 #-darwin
236 (progn
237 (ctype clockid-t "clockid_t")
238 (constant (clock-monotonic "CLOCK_MONOTONIC"))
239 (constant (clock-realtime "CLOCK_REALTIME")))
241 (cstruct timespec "struct timespec"
242 "UNIX time specification in seconds and nanoseconds."
243 (sec "tv_sec" :type time-t)
244 (nsec "tv_nsec" :type :long))
246 ;;;; from sys/select.h
248 (cstruct timeval "struct timeval"
249 "UNIX time specification in seconds and microseconds."
250 (sec "tv_sec" :type time-t)
251 (usec "tv_usec" :type suseconds-t))
253 ;;;; from sys/stat.h
255 (ctype dev-t "dev_t")
256 (ctype ino-t "ino_t")
258 (ctype nlink-t "nlink_t")
259 (ctype blksize-t "blksize_t")
260 (ctype blkcnt-t "blkcnt_t")
262 (cstruct stat "struct stat"
263 (dev "st_dev" :type #-mips dev-t #+mips :unsigned-long)
264 (ino "st_ino" :type ino-t)
265 (mode "st_mode" :type mode-t)
266 (nlink "st_nlink" :type nlink-t)
267 (uid "st_uid" :type uid-t)
268 (gid "st_gid" :type gid-t)
269 (rdev "st_rdev" :type #-mips dev-t #+mips :unsigned-long)
270 (size "st_size" :type off-t)
271 (blksize "st_blksize" :type blkcnt-t)
272 (blocks "st_blocks" :type blksize-t)
273 (atime "st_atime" :type time-t)
274 (mtime "st_mtime" :type time-t)
275 (ctime "st_ctime" :type time-t))
277 ;;; mmap()
278 (constant (prot-none "PROT_NONE") :documentation "mmap: no protection")
279 (constant (prot-read "PROT_READ") :documentation "mmap: read protection")
280 (constant (prot-write "PROT_WRITE") :documentation "mmap: write protection")
281 (constant (prot-exec "PROT_EXEC") :documentation "mmap: execute protection")
282 (constant (map-shared "MAP_SHARED") :documentation "mmap: shared memory")
283 (constant (map-private "MAP_PRIVATE") :documentation "mmap: private mapping")
284 (constant (map-fixed "MAP_FIXED") :documentation "mmap: map at location")
285 (constant (map-failed "MAP_FAILED") :documentation "mmap: failure")
287 ;;; poll()
289 (ctype nfds-t "nfds_t")
291 (cstruct pollfd "struct pollfd"
292 "Poll file descriptor activity specification structure."
293 (fd "fd" :type :int)
294 (events "events" :type :short)
295 (revents "revents" :type :short))
297 (constant (pollin "POLLIN"))
298 (constant (pollrdnorm "POLLRDNORM"))
299 (constant (pollrdband "POLLRDBAND"))
300 (constant (pollpri "POLLPRI"))
301 (constant (pollout "POLLOUT"))
302 (constant (pollwrnorm "POLLWRNORM"))
303 (constant (pollwrband "POLLWRBAND"))
304 (constant (pollerr "POLLERR"))
305 #-darwin (constant (pollrdhup "POLLRDHUP"))
306 (constant (pollhup "POLLHUP"))
307 (constant (pollnval "POLLNVAL"))
309 ;;;; from dirent.h
311 ;;; Apparently POSIX 1003.1-2001 (according to linux manpages) only
312 ;;; requires d_name. Sigh. I guess we should assemble some decent
313 ;;; wrapper functions. No, struct members can't be optional at this
314 ;;; point.
315 (cstruct dirent "struct dirent"
316 ;; POSIX actually requires this to be d_ino
317 (fileno "d_fileno" :type #-freebsd ino-t #+freebsd :uint32)
318 (type "d_type" :type :uint8)
319 (name "d_name" :type :uint8 :count :auto))
321 ;;; filetypes set in d_type slot of struct dirent
322 (constant (dt-unknown "DT_UNKNOWN"))
323 (constant (dt-fifo "DT_FIFO"))
324 (constant (dt-chr "DT_CHR"))
325 (constant (dt-dir "DT_DIR"))
326 (constant (dt-blk "DT_BLK"))
327 (constant (dt-reg "DT_REG"))
328 (constant (dt-lnk "DT_LNK"))
329 (constant (dt-sock "DT_SOCK"))
330 (constant (dt-wht "DT_WHT"))
332 ;;; ioctl()
334 (constant (fionbio "FIONBIO"))
335 (constant (fionread "FIONREAD"))
337 ;;;; from sys/resource.h
339 (ctype rlim-t "rlim_t")
340 (ctype id-t "id_t")
342 (cstruct rlimit "struct rlimit"
343 (cur "rlim_cur" :type rlim-t)
344 (max "rlim_max" :type rlim-t))
346 (cstruct rusage "struct rusage"
347 (utime "ru_utime" :type timeval)
348 (stime "ru_stime" :type timeval)
349 (maxrss "ru_maxrss" :type :long)
350 (ixrss "ru_ixrss" :type :long)
351 (idrss "ru_idrss" :type :long)
352 (isrss "ru_isrss" :type :long)
353 (minflt "ru_minflt" :type :long)
354 (majflt "ru_majflt" :type :long)
355 (nswap "ru_nswap" :type :long)
356 (inblock "ru_inblock" :type :long)
357 (oublock "ru_oublock" :type :long)
358 (msgsnd "ru_msgsnd" :type :long)
359 (msgrcv "ru_msgrcv" :type :long)
360 (nsignals "ru_nsignals" :type :long)
361 (nvcsw "ru_nvcsw" :type :long)
362 (nivcsw "ru_nivcsw" :type :long))
364 (constant (prio-process "PRIO_PROCESS"))
365 (constant (prio-pgrp "PRIO_PGRP"))
366 (constant (prio-user "PRIO_USER"))
367 (constant (rlim-infinity "RLIM_INFINITY"))
368 (constant (rusage-self "RUSAGE_SELF"))
369 (constant (rusage-children "RUSAGE_CHILDREN"))
370 (constant (rlimit-as "RLIMIT_AS"))
371 (constant (rlimit-core "RLIMIT_CORE"))
372 (constant (rlimit-cpu "RLIMIT_CPU"))
373 (constant (rlimit-data "RLIMIT_DATA"))
374 (constant (rlimit-fsize "RLIMIT_FSIZE"))
375 (constant (rlimit-memlock "RLIMIT_MEMLOCK"))
376 (constant (rlimit-nofile "RLIMIT_NOFILE"))
377 (constant (rlimit-nproc "RLIMIT_NPROC"))
378 (constant (rlimit-rss "RLIMIT_RSS"))
379 (constant (rlimit-stack "RLIMIT_STACK"))
381 #+linux
382 (progn
383 (constant (rlim-saved-max "RLIM_SAVED_MAX"))
384 (constant (rlim-saved-cur "RLIM_SAVED_CUR"))
385 (constant (rlimit-locks "RLIMIT_LOCKS"))
386 (constant (rlimit-msgqueue "RLIMIT_MSGQUEUE"))
387 (constant (rlimit-nlimits "RLIMIT_NLIMITS"))
388 (constant (rlimit-nice "RLIMIT_NICE"))
389 (constant (rlimit-rtprio "RLIMIT_RTPRIO"))
390 (constant (rlimit-sigpending "RLIMIT_SIGPENDING")))
392 ;;;; from pwd.h
394 (cstruct passwd-entry "struct passwd"
395 (name "pw_name" :type :string)
396 (passwd "pw_passwd" :type :string)
397 (uid "pw_uid" :type uid-t)
398 (gid "pw_gid" :type gid-t)
399 (gecos "pw_gecos" :type :string)
400 (dir "pw_dir" :type :string)
401 (shell "pw_shell" :type :string))
403 ;;;; from grp.h
405 ;;; FIXME What about gr_mem? Kinda the whole point?
406 (cstruct group-entry "struct group"
407 (name "gr_name" :type :string)
408 (passwd "gr_passwd" :type :string)
409 (gid "gr_gid" :type gid-t)
410 (mem "gr_mem" :type :pointer))