Commit 5fd61f30ff315f216a2ead4f3052b5a40e65a930

Thomas de Grivel 2018-02-02T01:28:41

Rename fd-stream to unistd-stream.

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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
diff --git a/README.md b/README.md
index 2eb7597..2cfa047 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
-# fd-stream
+# unistd-stream
 
-fd-stream streams are `(unsigned-byte 8)` cl-stream streams
+unistd streams are `(unsigned-byte 8)` cl-stream streams
 using `unistd:read`, `unistd:write` and `unistd:close` to
 operate on Unix file descriptors.
 
@@ -9,26 +9,26 @@ Depends on :
 - https://github.com/cffi-posix/cffi-unistd
 - https://github.com/cl-stream/cl-stream
 
-## Class: fd-stream
+## Class: unistd-stream
 Base class for file descriptor streams.
 
-### Generic: stream-fd *fd-stream* => *fd*
-Returns the file descriptor of FD-STREAM.
+### Generic: stream-fd *unistd-stream* => *fd*
+Returns the file descriptor of UNISTD-STREAM.
 
-## Class: fd-input-stream
+## Class: unistd-input-stream
 A buffered input stream using UNISTD:READ.
 
-### Function: fd-input-stream *fd* => *stream*
+### Function: unistd-input-stream *fd* => *stream*
 Creates a buffered input stream for file descriptor *fd*.
 
-## Class: fd-output-stream
+## Class: unistd-output-stream
 A buffered output stream using UNISTD:WRITE.
 
-### Function: fd-output-stream *fd* => *stream*
+### Function: unistd-output-stream *fd* => *stream*
 Creates a buffered output stream for file descriptor *fd*.
 
-## Class: fd-io-stream
+## Class: unistd-io-stream
 A buffered input/output stream using UNISTD:READ and UNISTD:WRITE.
 
-### Function: fd-io-stream *fd* => *stream*
+### Function: unistd-io-stream *fd* => *stream*
 Creates a buffered input/output stream for file descriptor *fd*.
diff --git a/fd-stream.asd b/fd-stream.asd
deleted file mode 100644
index f85f3d5..0000000
--- a/fd-stream.asd
+++ /dev/null
@@ -1,36 +0,0 @@
-;;
-;;  fd-stream  -  Unix file descriptor layer for cl-stream
-;;
-;;  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 :common-lisp-user)
-
-(defpackage :fd-stream.system
-  (:use :common-lisp :asdf))
-
-(in-package :fd-stream.system)
-
-(defsystem :fd-stream
-  :name "fd-stream"
-  :author "Thomas de Grivel <thoxdg@gmail.com>"
-  :version "0.1"
-  :description "Unix file descriptor stream streams for Common Lisp"
-  :depends-on ("cffi-fcntl"
-	       "cffi-unistd"
-	       "cl-stream")
-  :components
-  ((:file "package")
-   (:file "fd-stream" :depends-on ("package"))))
diff --git a/fd-stream.lisp b/fd-stream.lisp
deleted file mode 100644
index 9cc73e9..0000000
--- a/fd-stream.lisp
+++ /dev/null
@@ -1,191 +0,0 @@
-;;
-;;  fd-stream  -  Unix file descriptor layer for cl-stream
-;;
-;;  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 :fd-stream)
-
-(deftype fixnum+ (&optional (start 0))
-  `(integer ,start ,most-positive-fixnum))
-
-(defclass fd-stream (stream)
-  ((fd :initarg :fd
-       :reader stream-fd
-       :type file-descriptor)
-   (blocking-p :initarg :blocking-p
-               :type boolean))
-  (:documentation "Base class for file descriptor streams."))
-
-(defmethod stream-blocking-p ((stream fd-stream))
-  (or (when (slot-boundp stream 'blocking-p)
-        (slot-value stream 'blocking-p))
-      (setf (slot-value stream 'blocking-p)
-            (let ((flags (fcntl:getfl (stream-fd stream))))
-              (not (= 0 (logand fcntl:+o-nonblock+ flags)))))))
-
-(defmethod (setf stream-blocking-p) (value (stream fd-stream))
-  (let* ((fd (stream-fd stream))
-         (flags (fcntl:getfl fd))
-         (o-nonblock (not (= 0 (logand fcntl:+o-nonblock+ flags)))))
-    (cond
-      ((and value o-nonblock)
-       t)
-      ((and (not value) (not o-nonblock))
-       nil)
-      (t
-       (fcntl:setfl fd (if value
-                           (logand (lognot fcntl:+o-nonblock+) flags)
-                           (logior fcntl:+o-nonblock+ flags)))
-       (setf (slot-value stream 'blocking-p) value)))))
-
-(defmethod stream-element-type ((stream fd-stream))
-  '(unsigned-byte 8))
-
-(defmethod close ((stream fd-stream))
-  (unistd:close (stream-fd stream))
-  (call-next-method))
-
-(defclass fd-input-stream (fd-stream buffered-input-stream)
-  ()
-  (:documentation "A buffered input stream using UNISTD:READ."))
-
-(defmethod make-stream-input-buffer ((stream fd-input-stream))
-  (cffi:foreign-alloc :unsigned-char
-                      :count (stream-input-buffer-size stream)))
-
-(defmethod discard-stream-input-buffer ((stream fd-input-stream))
-  (cffi:foreign-free (stream-input-buffer stream))
-  (setf (stream-input-buffer stream) nil))
-
-(defmethod stream-fill-input-buffer ((stream fd-input-stream))
-  (let* ((buffer (stream-input-buffer stream))
-         (length (stream-input-length stream))
-         (fd (stream-fd stream))
-         (buf (cffi:mem-aptr buffer :unsigned-char length))
-         (buflen (- (stream-input-buffer-size stream) length))
-         (r (if (stream-blocking-p stream)
-                (unistd:read fd buf buflen)
-                (unistd:read-non-blocking fd buf buflen))))
-    (cond ((eq :non-blocking r)
-           :non-blocking)
-          ((= r 0)
-           :eof)
-          ((< r 0)
-           (error 'stream-input-error :stream stream))
-          (t
-           (incf (stream-input-length stream) r)
-           nil))))
-
-(defmethod stream-read-element-from-buffer ((stream fd-input-stream))
-  (let ((element (cffi:mem-aref (stream-input-buffer stream) :unsigned-char
-                                (stream-input-index stream))))
-    (incf (stream-input-index stream))
-    (values element nil)))
-
-(defun fd-input-stream (fd)
-  "Creates a buffered input stream for file descriptor FD."
-  (make-instance 'fd-input-stream :fd fd))
-
-(defclass fd-output-stream (fd-stream buffered-output-stream)
-  ()
-  (:documentation "A buffered output stream using UNISTD:WRITE."))
-
-(defmethod make-stream-output-buffer ((stream fd-output-stream))
-  (cffi:foreign-alloc :unsigned-char
-                      :count (stream-output-buffer-size stream)))
-
-(defmethod discard-stream-output-buffer ((stream fd-output-stream))
-  (cffi:foreign-free (stream-output-buffer stream))
-  (setf (stream-output-buffer stream) nil))
-
-(defmethod stream-flush-output-buffer ((stream fd-output-stream))
-  (let ((buffer (stream-output-buffer stream)))
-    (let* ((fd (stream-fd stream))
-           (index (stream-output-index stream))
-           (buf (cffi:mem-aptr buffer :unsigned-char index))
-           (buflen (- (stream-output-length stream) index))
-           (r (if (stream-blocking-p stream)
-                  (unistd:write fd buf buflen)
-                  (unistd:write-non-blocking fd buf buflen))))
-      (cond ((eq :non-blocking r)
-             :non-blocking)
-            ((= 0 r)
-             :eof)
-            ((< r 0)
-             (error 'stream-output-error :stream stream))
-            (t
-             (incf (stream-output-index stream) r)
-             (when (= (stream-output-index stream)
-                      (stream-output-length stream))
-               (setf (stream-output-index stream) 0
-                     (stream-output-length stream) 0))
-             nil)))))
-
-(defmethod stream-write-element-to-buffer ((stream fd-output-stream)
-                                           element)
-  (setf (cffi:mem-aref (stream-output-buffer stream) :unsigned-char
-                       (stream-output-length stream))
-        element)
-  (incf (stream-output-length stream))
-  nil)
-
-(defun fd-output-stream (fd)
-  "Creates a buffered output stream for file descriptor FD."
-  (make-instance 'fd-output-stream :fd fd))
-
-(defclass fd-io-stream (fd-input-stream fd-output-stream)
-  ()
-  (:documentation "A buffered input/output stream using
-UNISTD:READ and UNISTD:WRITE."))
-
-(defmethod close ((stream fd-io-stream))
-  (call-next-method)
-  (cffi:foreign-free (stream-input-buffer stream))
-  (cffi:foreign-free (stream-output-buffer stream)))
-
-(defun fd-io-stream (fd)
-  "Creates a buffered input/output stream for file descriptor FD."
-  (make-instance 'fd-io-stream :fd fd))
-
-(defun open-file (pathname &key
-                             (append nil)
-                             (blocking t)
-                             (create t)
-                             (direction :io)
-                             (input-buffer-size *default-buffer-size*)
-                             (mode #o777)
-                             (output-buffer-size *default-buffer-size*))
-  (let* ((flags (logior (if append
-                            fcntl:+o-append+
-                            0)
-                        (if blocking
-                            0
-                            fcntl:+o-nonblock+)
-                        (if create
-                            fcntl:+o-creat+
-                            0)
-                        (ecase direction
-                          ((:input) fcntl:+o-rdonly+)
-                          ((:output) fcntl:+o-wronly+)
-                          ((:io) fcntl:+o-rdwr+))))
-         (fd (fcntl:open pathname flags mode)))
-    (make-instance (case direction
-                     ((:input) 'fd-input-stream)
-                     ((:output) 'fd-output-stream)
-                     ((:io) 'fd-io-stream))
-                   :fd fd
-                   :input-buffer-size input-buffer-size
-                   :output-buffer-size output-buffer-size)))
diff --git a/package.lisp b/package.lisp
index 4d08469..8549521 100644
--- a/package.lisp
+++ b/package.lisp
@@ -1,7 +1,7 @@
 ;;
-;;  fd-stream  -  Unix file descriptor layer for cl-stream
+;;  unistd-stream  -  Unix file descriptor layer for cl-stream
 ;;
-;;  Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
+;;  Copyright 2017,2018 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
@@ -18,14 +18,14 @@
 
 (in-package :common-lisp-user)
 
-(defpackage :fd-stream
+(defpackage :unistd-stream
   (:use
    :common-lisp
    :cl-stream)
   #.(cl-stream:shadowing-import-from)
   (:export
-   #:fd-input-stream
-   #:fd-io-stream
-   #:fd-output-stream
-   #:fd-stream
-   #:open-file))
+   #:unistd-input-stream
+   #:unistd-io-stream
+   #:unistd-output-stream
+   #:unistd-stream
+   #:unistd-stream-open))
diff --git a/unistd-stream.asd b/unistd-stream.asd
new file mode 100644
index 0000000..be1414f
--- /dev/null
+++ b/unistd-stream.asd
@@ -0,0 +1,36 @@
+;;
+;;  unistd-stream  -  Unix file descriptor layer for cl-stream
+;;
+;;  Copyright 2017,2018 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 :common-lisp-user)
+
+(defpackage :unistd-stream.system
+  (:use :common-lisp :asdf))
+
+(in-package :unistd-stream.system)
+
+(defsystem :unistd-stream
+  :name "unistd-stream"
+  :author "Thomas de Grivel <thoxdg@gmail.com>"
+  :version "0.1"
+  :description "Unix file descriptor layer for cl-stream"
+  :depends-on ("cffi-fcntl"
+	       "cffi-unistd"
+	       "cl-stream")
+  :components
+  ((:file "package")
+   (:file "unistd-stream" :depends-on ("package"))))
diff --git a/unistd-stream.lisp b/unistd-stream.lisp
new file mode 100644
index 0000000..a9a876e
--- /dev/null
+++ b/unistd-stream.lisp
@@ -0,0 +1,191 @@
+;;
+;;  unistd-stream  -  Unix file descriptor layer for cl-stream
+;;
+;;  Copyright 2017,2018 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 :unistd-stream)
+
+(deftype fixnum+ (&optional (start 0))
+  `(integer ,start ,most-positive-fixnum))
+
+(defclass unistd-stream (stream)
+  ((fd :initarg :fd
+       :reader stream-fd
+       :type file-descriptor)
+   (blocking-p :initarg :blocking-p
+               :type boolean))
+  (:documentation "Base class for file descriptor streams."))
+
+(defmethod stream-blocking-p ((stream unistd-stream))
+  (or (when (slot-boundp stream 'blocking-p)
+        (slot-value stream 'blocking-p))
+      (setf (slot-value stream 'blocking-p)
+            (let ((flags (fcntl:getfl (stream-fd stream))))
+              (not (= 0 (logand fcntl:+o-nonblock+ flags)))))))
+
+(defmethod (setf stream-blocking-p) (value (stream unistd-stream))
+  (let* ((fd (stream-fd stream))
+         (flags (fcntl:getfl fd))
+         (o-nonblock (not (= 0 (logand fcntl:+o-nonblock+ flags)))))
+    (cond
+      ((and value o-nonblock)
+       t)
+      ((and (not value) (not o-nonblock))
+       nil)
+      (t
+       (fcntl:setfl fd (if value
+                           (logand (lognot fcntl:+o-nonblock+) flags)
+                           (logior fcntl:+o-nonblock+ flags)))
+       (setf (slot-value stream 'blocking-p) value)))))
+
+(defmethod stream-element-type ((stream unistd-stream))
+  '(unsigned-byte 8))
+
+(defmethod close ((stream unistd-stream))
+  (unistd:close (stream-fd stream))
+  (call-next-method))
+
+(defclass unistd-input-stream (unistd-stream buffered-input-stream)
+  ()
+  (:documentation "A buffered input stream using UNISTD:READ."))
+
+(defmethod make-stream-input-buffer ((stream unistd-input-stream))
+  (cffi:foreign-alloc :unsigned-char
+                      :count (stream-input-buffer-size stream)))
+
+(defmethod discard-stream-input-buffer ((stream unistd-input-stream))
+  (cffi:foreign-free (stream-input-buffer stream))
+  (setf (stream-input-buffer stream) nil))
+
+(defmethod stream-fill-input-buffer ((stream unistd-input-stream))
+  (let* ((buffer (stream-input-buffer stream))
+         (length (stream-input-length stream))
+         (fd (stream-fd stream))
+         (buf (cffi:mem-aptr buffer :unsigned-char length))
+         (buflen (- (stream-input-buffer-size stream) length))
+         (r (if (stream-blocking-p stream)
+                (unistd:read fd buf buflen)
+                (unistd:read-non-blocking fd buf buflen))))
+    (cond ((eq :non-blocking r)
+           :non-blocking)
+          ((= r 0)
+           :eof)
+          ((< r 0)
+           (error 'stream-input-error :stream stream))
+          (t
+           (incf (stream-input-length stream) r)
+           nil))))
+
+(defmethod stream-read-element-from-buffer ((stream unistd-input-stream))
+  (let ((element (cffi:mem-aref (stream-input-buffer stream) :unsigned-char
+                                (stream-input-index stream))))
+    (incf (stream-input-index stream))
+    (values element nil)))
+
+(defun unistd-input-stream (fd)
+  "Creates a buffered input stream for file descriptor FD."
+  (make-instance 'unistd-input-stream :fd fd))
+
+(defclass unistd-output-stream (unistd-stream buffered-output-stream)
+  ()
+  (:documentation "A buffered output stream using UNISTD:WRITE."))
+
+(defmethod make-stream-output-buffer ((stream unistd-output-stream))
+  (cffi:foreign-alloc :unsigned-char
+                      :count (stream-output-buffer-size stream)))
+
+(defmethod discard-stream-output-buffer ((stream unistd-output-stream))
+  (cffi:foreign-free (stream-output-buffer stream))
+  (setf (stream-output-buffer stream) nil))
+
+(defmethod stream-flush-output-buffer ((stream unistd-output-stream))
+  (let ((buffer (stream-output-buffer stream)))
+    (let* ((fd (stream-fd stream))
+           (index (stream-output-index stream))
+           (buf (cffi:mem-aptr buffer :unsigned-char index))
+           (buflen (- (stream-output-length stream) index))
+           (r (if (stream-blocking-p stream)
+                  (unistd:write fd buf buflen)
+                  (unistd:write-non-blocking fd buf buflen))))
+      (cond ((eq :non-blocking r)
+             :non-blocking)
+            ((= 0 r)
+             :eof)
+            ((< r 0)
+             (error 'stream-output-error :stream stream))
+            (t
+             (incf (stream-output-index stream) r)
+             (when (= (stream-output-index stream)
+                      (stream-output-length stream))
+               (setf (stream-output-index stream) 0
+                     (stream-output-length stream) 0))
+             nil)))))
+
+(defmethod stream-write-element-to-buffer ((stream unistd-output-stream)
+                                           element)
+  (setf (cffi:mem-aref (stream-output-buffer stream) :unsigned-char
+                       (stream-output-length stream))
+        element)
+  (incf (stream-output-length stream))
+  nil)
+
+(defun unistd-output-stream (fd)
+  "Creates a buffered output stream for file descriptor FD."
+  (make-instance 'unistd-output-stream :fd fd))
+
+(defclass unistd-io-stream (unistd-input-stream unistd-output-stream)
+  ()
+  (:documentation "A buffered input/output stream using
+UNISTD:READ and UNISTD:WRITE."))
+
+(defmethod close ((stream unistd-io-stream))
+  (call-next-method)
+  (cffi:foreign-free (stream-input-buffer stream))
+  (cffi:foreign-free (stream-output-buffer stream)))
+
+(defun unistd-io-stream (fd)
+  "Creates a buffered input/output stream for file descriptor FD."
+  (make-instance 'unistd-io-stream :fd fd))
+
+(defun unistd-stream-open (pathname &key
+                                      (append nil)
+                                      (blocking t)
+                                      (create t)
+                                      (direction :io)
+                                      (input-buffer-size *stream-default-buffer-size*)
+                                      (mode #o777)
+                                      (output-buffer-size *stream-default-buffer-size*))
+  (let* ((flags (logior (if append
+                            fcntl:+o-append+
+                            0)
+                        (if blocking
+                            0
+                            fcntl:+o-nonblock+)
+                        (if create
+                            fcntl:+o-creat+
+                            0)
+                        (ecase direction
+                          ((:input) fcntl:+o-rdonly+)
+                          ((:output) fcntl:+o-wronly+)
+                          ((:io) fcntl:+o-rdwr+))))
+         (fd (fcntl:open pathname flags mode)))
+    (make-instance (case direction
+                     ((:input) 'unistd-input-stream)
+                     ((:output) 'unistd-output-stream)
+                     ((:io) 'unistd-io-stream))
+                   :fd fd
+                   :input-buffer-size input-buffer-size
+                   :output-buffer-size output-buffer-size)))