Welcome to the CHICKEN Scheme pasting service

non-blocking inotify demo added by wasamasa on Tue Jan 10 10:01:48 2017

(import foreign)
(use srfi-18)

#> #include <sys/inotify.h> <#

(define inotify_init (foreign-lambda int "inotify_init"))
(define inotify_add_watch (foreign-lambda int "inotify_add_watch" int c-string int))
(define inotify_cleanup (foreign-lambda int "close" int))

;; adapted from https://github.com/xlevus/chicken-stuff/blob/master/inotify.scm

#>
void inotify_read(int fd) {
  int EVENT_SIZE = sizeof(struct inotify_event);
  int EVENT_COUNT = 1;
  int BUF_LEN = EVENT_COUNT*EVENT_SIZE+16; // TODO: figure out what's that for

  int length, i = 0;
  char buffer[BUF_LEN];

  length = read(fd, buffer, BUF_LEN);

  while (i < length) {
    struct inotify_event *event = (struct inotify_event *) &buffer[i];
    if (event->len) {
      printf("WD: %d, mask: %d, name: %s\n", event->wd, event->mask, event->name);
    }
    i += EVENT_SIZE + event->len;
  }
}
<#

(define inotify_read (foreign-lambda void "inotify_read" int))

(thread-start!
 (make-thread
  (lambda ()
    (let loop ()
      (print "~")
      (thread-sleep! 0.1)
      (loop)))))

(thread-sleep! 2)

(define fd (inotify_init))
(define wd (inotify_add_watch fd "/home/wasa/" 24)) ; IN_CLOSE
(thread-wait-for-i/o! fd) ; suspend main thread to allow bg thread to continue
(inotify_read fd) ; touch /home/wasa/test to trigger
(inotify_cleanup fd)

(thread-sleep! 2)

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
What does `(string-chomp "foobar")' produce?
Visually impaired? Let me spell it for you (wav file) download WAV