aboutsummaryrefslogtreecommitdiff
path: root/lib/notify.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-05-25 19:48:33 -0500
committerRob Landley <rob@landley.net>2019-05-25 19:48:33 -0500
commit94777894a95044c5253ec081503085ab9947cfec (patch)
treed1b706df996629cbde90046b9e841218210388cf /lib/notify.c
parentc82d3108947c93c9e27553a44f319171776e92a3 (diff)
downloadtoybox-94777894a95044c5253ec081503085ab9947cfec.tar.gz
Move notify.c into portability.c (collate the #ifdefs), move global variables
into a structure passed as arguments, add x prefix to functions that can fail, add overflow test.
Diffstat (limited to 'lib/notify.c')
-rw-r--r--lib/notify.c87
1 files changed, 0 insertions, 87 deletions
diff --git a/lib/notify.c b/lib/notify.c
deleted file mode 100644
index a7993e2a..00000000
--- a/lib/notify.c
+++ /dev/null
@@ -1,87 +0,0 @@
-#include "toys.h"
-
-static char **paths;
-static int *fds;
-static int count;
-
-#ifdef __APPLE__
-
-#include <sys/event.h>
-
-static int kq = -1;
-
-void notify_init(int max)
-{
- if ((kq = kqueue()) == -1) perror_exit("kqueue");
- paths = xmalloc(max * sizeof(char *));
- fds = xmalloc(max * sizeof(int));
-}
-
-int notify_add(int fd, char *path)
-{
- struct kevent event;
-
- EV_SET(&event, fd, EVFILT_VNODE, EV_ADD|EV_CLEAR, NOTE_WRITE, 0, NULL);
- if (kevent(kq, &event, 1, NULL, 0, NULL) == -1 || event.flags & EV_ERROR)
- return -1;
- paths[count] = path;
- fds[count++] = fd;
- return 0;
-}
-
-int notify_wait(char **path)
-{
- struct kevent event;
- int i;
-
- for (;;) {
- if (kevent(kq, NULL, 0, &event, 1, NULL) != -1) {
- // We get the fd for free, but still have to search for the path.
- for (i=0; i<count; i++) if (fds[i]==event.ident) {
- *path = paths[i];
- return event.ident;
- }
- }
- }
-}
-
-#else
-
-#include <sys/inotify.h>
-
-static int ffd = -1;
-static int *ids;
-
-void notify_init(int max)
-{
- if ((ffd = inotify_init()) < 0) perror_exit("inotify_init");
- fds = xmalloc(max * sizeof(int));
- ids = xmalloc(max * sizeof(int));
- paths = xmalloc(max * sizeof(char *));
-}
-
-int notify_add(int fd, char *path)
-{
- ids[count] = inotify_add_watch(ffd, path, IN_MODIFY);
- if (ids[count] == -1) return -1;
- paths[count] = path;
- fds[count++] = fd;
- return 0;
-}
-
-int notify_wait(char **path)
-{
- struct inotify_event ev;
- int i;
-
- for (;;) {
- if (sizeof(ev)!=read(ffd, &ev, sizeof(ev))) perror_exit("inotify");
-
- for (i=0; i<count; i++) if (ev.wd==ids[i]) {
- *path = paths[i];
- return fds[i];
- }
- }
-}
-
-#endif