Browse Source

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

master
Georg Hopp 10 years ago
parent
commit
df7cc8a3de
  1. 57
      inotify1.c

57
inotify1.c

@ -1,11 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <sys/select.h> #include <sys/select.h>
#include <inotify.h> #include <inotify.h>
#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))
int int
main (int arg, char * argv []) 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)) 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;
ssize_t numRead;
char buf[BUF_LEN] __attribute__ ((aligned(8)));
char * p;
struct inotify_event *event;
char * ev_name = NULL; char * ev_name = NULL;
ioctl (notify_d, FIONREAD, &size_avail);
in_ev = (char *)malloc (size_avail);
if (in_ev == 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)
{
int got = read (notify_d, in_ev + size,
size_avail - size);
if (got == 0)
if (numRead == -1)
{ {
perror ("notify FD was closed unexpectedly");
perror("read");
return 3; return 3;
} }
size += got;
}
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: case IN_ACCESS:
ev_name = "IN_ACCESS"; break; ev_name = "IN_ACCESS"; break;
@ -94,16 +87,12 @@ main (int arg, char * argv [])
case IN_OPEN: case IN_OPEN:
ev_name = "IN_OPEN"; break; 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 ("---"); puts ("---");

Loading…
Cancel
Save