diff --git a/unistd-stream.lisp b/unistd-stream.lisp
index 9b1fa08..5fa8423 100644
--- a/unistd-stream.lisp
+++ b/unistd-stream.lisp
@@ -160,32 +160,35 @@ UNISTD:READ and UNISTD:WRITE."))
"Creates a buffered input/output stream for file descriptor FD."
(make-instance 'unistd-io-stream :fd fd))
+(defun compute-flags (read write append non-blocking create)
+ (logior (if append fcntl:+o-append+ 0)
+ (if non-blocking fcntl:+o-nonblock+ 0)
+ (if create fcntl:+o-creat+ 0)
+ (cond ((and read write) fcntl:+o-rdwr+)
+ (read fcntl:+o-rdonly+)
+ (write fcntl:+o-wronly+)
+ (t 0))))
+
+(defun compute-class (read write)
+ (cond ((and read write) 'unistd-io-stream)
+ (read 'unistd-input-stream)
+ (write 'unistd-output-stream)))
+
(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))
+ read write append
+ non-blocking
+ (create #o777)
+ (input-buffer-size
+ *stream-default-buffer-size*)
+ (output-buffer-size
+ *stream-default-buffer-size*))
+ (assert (or read write)
+ (read write)
+ "Open not for reading nor writing.")
+ (let* ((flags (compute-flags read write append non-blocking create))
+ (fd (fcntl:open pathname flags (or create 0)))
+ (class (compute-class read write)))
+ (make-instance class
:fd fd
:input-buffer-size input-buffer-size
:output-buffer-size output-buffer-size)))