Commit 81bccbfc11b67e317db7216e5686484dcd64c9e7

Thomas de Grivel 2017-05-27T21:42:58

Read, read-non-blocking, write, write-non-blocking.

diff --git a/cffi-unistd.lisp b/cffi-unistd.lisp
index 1b0b421..c151aec 100644
--- a/cffi-unistd.lisp
+++ b/cffi-unistd.lisp
@@ -15,11 +15,49 @@
   (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 NIL 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))
+	    nil
+	    (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 NIL 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))
+	    nil
+	    (error-errno "write"))
+	r)))
+
 (defcfun ("pipe" c-pipe) :int
   (pipefd (:pointer :int)))
 
diff --git a/package.lisp b/package.lisp
index 4fb50ed..c13e9eb 100644
--- a/package.lisp
+++ b/package.lisp
@@ -8,12 +8,18 @@
    :common-lisp
    :errno)
   (:shadow
-   #:close)
+   #:close
+   #:read
+   #:write)
   (:export
    #:c-read
+   #:read
+   #:read-non-blocking
    #:c-close
    #:close
    #:c-write
+   #:write
+   #:write-non-blocking
    #:c-pipe
    #:pipe
    #:with-pipe