Uniformate la tabelle, riletta la sezione su epoll, altre correzioni
[gapil.git] / sources / inotify_monitor.c
1 /* inotify_monitor.c
2  * 
3  * Copyright (C) 2007 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*****************************************************************************
20  *
21  * File inotufy_monitor.c: 
22  *
23  * An example for shared memory use: monitor a directory status,
24  * saving data in a shared memory segment
25  *
26  * Author: S. Piccardi Jul. 2007
27  *
28  *****************************************************************************/
29 #include <sys/types.h>
30 #include <sys/inotify.h>
31 #include <stdlib.h>        /* C standard library */
32 #include <unistd.h>
33 #include <errno.h>       /* error definitions and routines */ 
34 #include <stdio.h>       /* standard I/O library */
35 #include <string.h>      /* string functions */
36
37
38 #include "macros.h"
39
40 /* Help printing routine */
41 void usage(void);
42
43 int main(int argc, char *argv[]) 
44 {
45     int i;
46     int fd;
47     unsigned int mask=0;
48     /*
49      * Input section: decode command line parameters 
50      * Use getopt function
51      */
52     opterr = 0;  /* don't want writing to stderr */
53     while ( (i = getopt(argc, argv, "hrwcda")) != -1) {
54         switch (i) {
55         /* 
56          * Handling options 
57          */ 
58         case 'h':                                             /* help option */
59             printf("Wrong -h option use\n");
60             usage();
61             return -1;
62             break;
63         case 'r':                                             /* read access */
64             mask |= IN_ACCESS;
65             break;
66         case 'w':                                            /* write access */
67             mask |= IN_MODIFY;
68             break;
69         case 'c':                                                /* creation */
70             mask |= IN_CREATE;
71             break;
72         case 'd':                                                /* deletion */
73             mask |= IN_DELETE;
74             break;
75         case 'a':                                              /* all events */
76             mask |= IN_ALL_EVENTS;
77             break;
78         case '?':                                    /* unrecognized options */
79             printf("Unrecognized options -%c\n",optopt);
80             usage();
81         default:                                       /* should not reached */
82             usage();
83         }
84     }
85     /* ***********************************************************
86      * 
87      *           Options processing completed
88      *
89      *                Main code beginning
90      * 
91      * ***********************************************************/
92     if ((argc - optind) != 1) {          /* There must be remaing parameters */
93         printf("Wrong number of arguments %d\n", argc - optind);
94         usage();
95     }
96     /* initalialize */
97     fd = inotify_init ();
98     if (fd < 0)
99         perror("Failing on inotify_init");
100
101     /* add watch */
102     if (inotify_add_watch(fd, argv[1], mask) != 0) {
103         printf("Failing to add watched file %s; %s\n", 
104               argv[1], strerror(errno));
105         exit(-1);
106     }
107
108     return 0;
109
110 }
111 /*
112  * routine to print usage info and exit
113  */
114 void usage(void) {
115     printf("Program inotify_monitor: monitor file changes \n");
116     printf("Usage:\n");
117     printf(" inotify_monitor [-h] -rwcd dirname/filename \n");
118     printf("  -h           print this help\n");
119     printf("  -w           watch write\n");
120     printf("  -r           watch read\n");
121     printf("  -c           watch create\n");
122     printf("  -d           watch delete\n");
123     printf("  -a           watch all\n");
124     exit(1);
125 }