/* guile-inotify * Copyright (C) 2012-2014 sph@posteo.eu * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ #include #include #include #include #include "../extern/sph.c" b32 inotify_read_buf_size = sizeof(struct inotify_event) + NAME_MAX; SCM scm_inotify_init () { return scm_from_int(inotify_init()); } SCM scm_inotify_init1 (SCM flags) { return scm_from_int(inotify_init1(scm_to_int(flags))); } SCM scm_inotify_add_watch (SCM fd, SCM name, SCM mask) { return scm_from_int(inotify_add_watch(scm_to_int(fd), scm_to_locale_string(name), scm_to_uint32(mask))); } SCM scm_inotify_rm_watch (SCM fd, SCM wd) { return scm_from_int(inotify_rm_watch(scm_to_int(fd), scm_to_int(wd))); } SCM scm_inotify_read (SCM fd) { struct inotify_event* event; b8* buf = malloc(inotify_read_buf_size); b8* p; ssize_t size; SCM res = SCM_EOL; do { size = read(scm_to_int(fd), buf, inotify_read_buf_size); } while (size == -1 && errno == EINTR); if (size == -1) { assert(errno == EAGAIN || errno == EWOULDBLOCK); return scm_from_int(errno); } for (p = buf; p < buf + size; p += sizeof(struct inotify_event) + (* event).len) { event = (struct inotify_event*)p; SCM scm_event = scm_c_make_vector(4, 0); scm_c_vector_set_x(scm_event, 0, scm_from_int((* event).wd)); scm_c_vector_set_x(scm_event, 1, scm_from_uint32((* event).mask)); scm_c_vector_set_x(scm_event, 3, (* event).len ? scm_from_locale_string((* event).name) : SCM_BOOL_F); scm_c_vector_set_x(scm_event, 2, scm_from_uint32((* event).cookie)); res = scm_cons(scm_event, res); } return res; } b0 init_guile_inotify () { scm_c_define("IN_ACCESS", scm_from_uint32(IN_ACCESS)); scm_c_define("IN_MODIFY", scm_from_uint32(IN_MODIFY)); scm_c_define("IN_ATTRIB", scm_from_uint32(IN_ATTRIB)); scm_c_define("IN_CLOSE_WRITE", scm_from_uint32(IN_CLOSE_WRITE)); scm_c_define("IN_CLOSE_NOWRITE", scm_from_uint32(IN_CLOSE_NOWRITE)); scm_c_define("IN_CLOSE", scm_from_uint32(IN_CLOSE)); scm_c_define("IN_OPEN", scm_from_uint32(IN_OPEN)); scm_c_define("IN_MOVED_FROM", scm_from_uint32(IN_MOVED_FROM)); scm_c_define("IN_MOVED_TO", scm_from_uint32(IN_MOVED_TO)); scm_c_define("IN_MOVE", scm_from_uint32(IN_MOVE)); scm_c_define("IN_CREATE", scm_from_uint32(IN_CREATE)); scm_c_define("IN_DELETE", scm_from_uint32(IN_DELETE)); scm_c_define("IN_DELETE_SELF", scm_from_uint32(IN_DELETE_SELF)); scm_c_define("IN_MOVE_SELF", scm_from_uint32(IN_MOVE_SELF)); scm_c_define("IN_UNMOUNT", scm_from_uint32(IN_UNMOUNT)); scm_c_define("IN_Q_OVERFLOW", scm_from_uint32(IN_Q_OVERFLOW)); scm_c_define("IN_IGNORED", scm_from_uint32(IN_IGNORED)); scm_c_define("IN_ONLYDIR", scm_from_uint32(IN_ONLYDIR)); scm_c_define("IN_DONT_FOLLOW", scm_from_uint32(IN_DONT_FOLLOW)); scm_c_define("IN_EXCL_UNLINK", scm_from_uint32(IN_EXCL_UNLINK)); scm_c_define("IN_MASK_ADD", scm_from_uint32(IN_MASK_ADD)); scm_c_define("IN_ISDIR", scm_from_uint32(IN_ISDIR)); scm_c_define("IN_ONESHOT", scm_from_uint32(IN_ONESHOT)); scm_c_define("IN_ALL_EVENTS", scm_from_uint32(IN_ALL_EVENTS)); scm_c_define_gsubr("inotify-init", 0,0,0, scm_inotify_init); scm_c_define_gsubr("inotify-init1", 1,0,0, scm_inotify_init1); scm_c_define_gsubr("inotify-add-watch", 3,0,0, scm_inotify_add_watch); scm_c_define_gsubr("inotify-rm-watch", 2,0,0, scm_inotify_rm_watch); scm_c_define_gsubr("inotify-read", 1,0,0, scm_inotify_read); }