Commit df7cc8a3de4bb0fe4cbb72b5db27fd77946fd0b6

Authored by Georg Hopp
1 parent ab2e59a7

remove dynamic allocation... also the previous stuff did not work correctly anymore.

Showing 1 changed file with 26 additions and 37 deletions
1 1 #include <stdio.h>
2 2 #include <errno.h>
3   -#include <string.h>
4   -#include <sys/ioctl.h>
  3 +#include <limits.h>
5 4 #include <sys/select.h>
6 5
7 6 #include <inotify.h>
8 7
  8 +#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))
  9 +
9 10 int
10 11 main (int arg, char * argv [])
11 12 {
... ... @@ -35,39 +36,31 @@ main (int arg, char * argv [])
35 36
36 37 while (sel_ret = select (notify_d+1, &rfds, NULL, NULL, NULL))
37 38 {
38   - char * in_ev = NULL;
39   - struct inotify_event * act_ev = NULL;
40   - size_t size = 0;
41   - size_t size_avail;
42   - char * ev_name = NULL;
43   -
44   - ioctl (notify_d, FIONREAD, &size_avail);
45   - in_ev = (char *)malloc (size_avail);
46   - if (in_ev == NULL)
  39 + ssize_t numRead;
  40 + char buf[BUF_LEN] __attribute__ ((aligned(8)));
  41 + char * p;
  42 + struct inotify_event *event;
  43 + char * ev_name = NULL;
  44 +
  45 + numRead = read(notify_d, buf, BUF_LEN);
  46 +
  47 + if (numRead == 0)
47 48 {
48   - perror ("could not get memory for event list");
49   - return 4;
  49 + perror("read() from inotify fd returned 0!");
  50 + return 3;
50 51 }
51   - memset (in_ev, 0, size_avail);
52 52
53   - while (size < size_avail)
  53 + if (numRead == -1)
54 54 {
55   - int got = read (notify_d, in_ev + size,
56   - size_avail - size);
57   - if (got == 0)
58   - {
59   - perror ("notify FD was closed unexpectedly");
60   - return 3;
61   - }
62   - size += got;
  55 + perror("read");
  56 + return 3;
63 57 }
64 58
65   - for (act_ev = (struct inotify_event *) in_ev;
66   - (char *) act_ev < in_ev + size_avail && IN_NO_EVENT (act_ev);
67   - act_ev = IN_NEXT_EVENT (act_ev))
  59 + for (p = buf; p < buf + numRead; )
68 60 {
69   - printf ("Watch Descriptor: %d\n", act_ev->wd);
70   - switch (act_ev->mask)
  61 + event = (struct inotify_event *) p;
  62 + printf ("Watch Descriptor: %d\n", event->wd);
  63 + switch (event->mask)
71 64 {
72 65 case IN_ACCESS:
73 66 ev_name = "IN_ACCESS"; break;
... ... @@ -94,16 +87,12 @@ main (int arg, char * argv [])
94 87 case IN_OPEN:
95 88 ev_name = "IN_OPEN"; break;
96 89 }
97   - printf ("Mask of Events: %s(%d)\n", ev_name, act_ev->mask);
98   - printf ("Events Cookie: %u\n", act_ev->cookie);
99   - printf ("Length of name: %u\n", act_ev->len);
100   - printf ("Event Name: %s\n", act_ev->name);
101   - }
  90 + printf ("Mask of Events: %s(%d)\n", ev_name, event->mask);
  91 + printf ("Events Cookie: %u\n", event->cookie);
  92 + printf ("Length of name: %u\n", event->len);
  93 + printf ("Event Name: %s\n", event->name);
102 94
103   - if (in_ev)
104   - {
105   - free (in_ev);
106   - in_ev = NULL;
  95 + p += sizeof(struct inotify_event) + event->len;
107 96 }
108 97
109 98 puts ("---");
... ...
Please register or login to post a comment