Add iolib functions.
[cl-inotify.git] / inotify.lisp
index 9319295..8c97386 100644 (file)
@@ -1,4 +1,4 @@
-;;; -*- mode: lisp; syntax: common-lisp; coding: utf-8; package: cl-inotify; -*-
+;;; -*- mode: lisp; syntax: common-lisp; coding: utf-8-unix; package: cl-inotify; -*-
 
 ;; Copyright (c) 2011-12, Olof-Joachim Frahm
 ;; All rights reserved.
 
 (in-package #:cl-inotify)
 
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (when (boundp 'in-cloexec)
+    (pushnew 'inotify1 *features*)))
+
+#+cl-inotify::inotify1
 (defbitfield (inotify1-flag :int)
   (:cloexec       #.in-cloexec)
   (:nonblock      #.in-nonblock))
@@ -97,18 +102,19 @@ thus should be used only with WATCH-RAW)."
 (defsyscall inotify-init :int
   "Initialises a new inotify event queue.")
 
+#+cl-inotify::inotify1
 (defsyscall inotify-init1 :int
   "Initialises a new inotify event queue and passes some flags along."
   (flags inotify1-flag))
 
 (defsyscall inotify-add-watch :int
-  "Watches a path on a event queue."
+  "Watches a path on an event queue."
   (fd :int)
   (pathname :string)
   (mask inotify-flag))
 
 (defsyscall inotify-rm-watch :int
-  "Removes a watched path from a event queue."
+  "Removes a watched path from an event queue."
   (fd :int)
   (wd :int))
 
@@ -198,8 +204,14 @@ the file descriptor is set to non-blocking I/O."
     (unwind-protect
          ;; file descriptor is collected with auto-close
          (progn
-           (setf fd (inotify-init1 (and (setf non-block nonblocking)
-                                        :nonblock)))
+           (setf non-block nonblocking)
+           #+inotify1
+           (setf fd (inotify-init1 (and non-block :nonblock)))
+           #-inotify1
+           (setf fd (inotify-init))
+           #-inotify1
+           (when non-block
+             (set-nonblocking fd T))
            (setf stream
                  ;; TODO: what about the blocking?
                  #-(or clisp sbcl)
@@ -404,6 +416,7 @@ instead.  Useful if you need to monitor just a fixed set of paths."
             ,.(mapcar (lambda (specifier)
                         `(watch-raw ,inotify ,@specifier))
                       rest)
+            (values)
             ,@body)
        (close-inotify ,inotify))))
 
@@ -422,5 +435,6 @@ UNWATCH calls on all WATCHed paths."
             ,.(mapcar (lambda (specifier)
                         `(watch ,inotify ,@specifier))
                       rest)
+            (values)
             ,@body)
        (close-inotify ,inotify))))