Aggiornamenti + alcune sockopt di IP
[gapil.git] / listati / inotify_monitor.c
1 #include <sys/inotify.h> /* Linux inotify interface */
2 ...
3 int main(int argc, char *argv[]) 
4 {
5     int i, narg, nread;
6     int fd, wd;
7     char buffer[512 * (sizeof(struct inotify_event) + 16)];
8     unsigned int mask=0;
9     struct inotify_event * event;
10     ...
11     narg = argc - optind; 
12     if (narg < 1) { /* There must be at least one argument */
13         printf("Wrong number of arguments %d\n", argc - optind);
14         usage();
15     }
16     fd = inotify_init();       /* initialize inotify */
17     if (fd < 0) {
18         perror("Failing on inotify_init");
19         exit(-1);
20     }
21     i = 0;
22     while (i < narg) {
23         wd = inotify_add_watch(fd, argv[optind+i], mask);  /* add watch */
24         if (wd <= 0) {
25             printf("Failing to add watched file %s, mask %i; %s\n", 
26                    argv[optind+i], mask, strerror(errno));
27             exit(-1);
28         }
29         i++;
30     }
31     /* Main Loop: read events and print them */
32     while (1) {
33         nread = read(fd, buffer, sizeof(buffer));
34         if (nread < 0) {
35             if (errno == EINTR) {
36                 continue;
37             } else {
38                 perror("error reading inotify data");
39                 exit(1);
40             }
41         } else {
42             i = 0;
43             while (i < nread) { 
44                 event = (struct inotify_event *) buffer + i;
45                 printf("Watch descriptor %i\n", event->wd);
46                 printf("Observed event on %s\n", argv[optind-1+event->wd]);
47                 if (event->len) {
48                     printf("On file %s\n", event->name);
49                 }
50                 printevent(event->mask);
51                 i += sizeof(struct inotify_event) + event->len;
52             }
53         }
54     }
55     return 0;
56 }