diff --git a/inotify1.c b/inotify1.c index e86c0c8..0623003 100644 --- a/inotify1.c +++ b/inotify1.c @@ -1,11 +1,12 @@ #include #include -#include -#include +#include #include #include +#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1)) + int main (int arg, char * argv []) { @@ -35,39 +36,31 @@ main (int arg, char * argv []) while (sel_ret = select (notify_d+1, &rfds, NULL, NULL, NULL)) { - char * in_ev = NULL; - struct inotify_event * act_ev = NULL; - size_t size = 0; - size_t size_avail; - char * ev_name = NULL; - - ioctl (notify_d, FIONREAD, &size_avail); - in_ev = (char *)malloc (size_avail); - if (in_ev == NULL) + ssize_t numRead; + char buf[BUF_LEN] __attribute__ ((aligned(8))); + char * p; + struct inotify_event *event; + char * ev_name = NULL; + + numRead = read(notify_d, buf, BUF_LEN); + + if (numRead == 0) { - perror ("could not get memory for event list"); - return 4; + perror("read() from inotify fd returned 0!"); + return 3; } - memset (in_ev, 0, size_avail); - while (size < size_avail) + if (numRead == -1) { - int got = read (notify_d, in_ev + size, - size_avail - size); - if (got == 0) - { - perror ("notify FD was closed unexpectedly"); - return 3; - } - size += got; + perror("read"); + return 3; } - for (act_ev = (struct inotify_event *) in_ev; - (char *) act_ev < in_ev + size_avail && IN_NO_EVENT (act_ev); - act_ev = IN_NEXT_EVENT (act_ev)) + for (p = buf; p < buf + numRead; ) { - printf ("Watch Descriptor: %d\n", act_ev->wd); - switch (act_ev->mask) + event = (struct inotify_event *) p; + printf ("Watch Descriptor: %d\n", event->wd); + switch (event->mask) { case IN_ACCESS: ev_name = "IN_ACCESS"; break; @@ -94,16 +87,12 @@ main (int arg, char * argv []) case IN_OPEN: ev_name = "IN_OPEN"; break; } - printf ("Mask of Events: %s(%d)\n", ev_name, act_ev->mask); - printf ("Events Cookie: %u\n", act_ev->cookie); - printf ("Length of name: %u\n", act_ev->len); - printf ("Event Name: %s\n", act_ev->name); - } + printf ("Mask of Events: %s(%d)\n", ev_name, event->mask); + printf ("Events Cookie: %u\n", event->cookie); + printf ("Length of name: %u\n", event->len); + printf ("Event Name: %s\n", event->name); - if (in_ev) - { - free (in_ev); - in_ev = NULL; + p += sizeof(struct inotify_event) + event->len; } puts ("---");