Hash :
f2bf65a4
Author :
Thomas de Grivel
Date :
2017-06-23T21:26:30
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
;;
;; cffi-unistd - Common Lisp wrapper for unistd.h
;;
;; Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
;;
;; Permission to use, copy, modify, and distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
;; copyright notice and this permission notice appear in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(in-package :cffi-unistd)
(defcfun ("access" c-access) :int
(name :string)
(type :int))
(defun access (name type)
(let ((r (c-access name type)))
(when (< r 0)
(error-errno "access"))
r))
(defcfun ("lseek" c-lseek) off-t
(fd :int)
(offset off-t)
(whence :int))
(defun lseek (fd offset whence)
(let ((r (c-lseek fd offset whence)))
(when (< r 0)
(error-errno "lseek"))
r))
(defcfun ("close" c-close) :int
(fd :int))
(defun close (fd)
(let ((r (c-close fd)))
(when (< r 0)
(error-errno "close"))
r))
(defcfun ("read" c-read) ssize-t
(fd :int)
(buf :pointer)
(count size-t))
(defun read (fd buf count)
"Reads at most COUNT bytes from FD into BUF.
Returns number of bytes read."
(let ((r (c-read fd buf count)))
(when (< r 0)
(error-errno "read"))
r))
(defun read-non-blocking (fd buf count)
"Reads at most COUNT bytes from FD into BUF.
Returns :NON-BLOCKING if read would block,
or number of bytes read otherwise."
(let ((r (c-read fd buf count)))
(if (< r 0)
(if (or (= +eagain+ errno) (= +ewouldblock+ errno))
:non-blocking
(error-errno "read"))
r)))
(defcfun ("write" c-write) ssize-t
(fd :int)
(buf :pointer)
(count size-t))
(defun write (fd buf count)
"Writes at most COUNT bytes from BUF into FD.
Returns number of bytes written."
(let ((r (c-write fd buf count)))
(when (< r 0)
(error-errno "write"))
r))
(defun write-non-blocking (fd buf count)
"Writes at most COUNT bytes from BUF into FD.
Returns :NON-BLOCKING if write would block,
or number of bytes written otherwise."
(let ((r (c-write fd buf count)))
(if (< r 0)
(if (or (= +eagain+ errno) (= +ewouldblock+ errno))
:non-blocking
(error-errno "write"))
r)))
(defcfun ("pipe" c-pipe) :int
(pipefd (:pointer :int)))
(defun pipe ()
(with-foreign-object (fd :int 2)
(let ((r (c-pipe fd)))
(when (< r 0)
(error-errno "pipe"))
(values (mem-aref fd :int 0)
(mem-aref fd :int 1)))))
(defmacro with-pipe ((in-var out-var) &body body)
`(multiple-value-bind (,in-var ,out-var) (pipe)
(unwind-protect (progn ,@body)
(close ,out-var)
(close ,in-var))))
(defcfun ("dup" c-dup) :int
(oldfd :int))
(defun dup (oldfd)
(let ((r (c-dup oldfd)))
(when (< r 0)
(error-errno "dup"))
r))