3 * Copyright (C) 2007 Simone Piccardi
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.
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.
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.
19 /*****************************************************************************
21 * File inotufy_monitor.c:
23 * An example for shared memory use: monitor a directory status,
24 * saving data in a shared memory segment
26 * Author: S. Piccardi Jul. 2007
28 *****************************************************************************/
29 #include <sys/types.h>
30 #include <sys/inotify.h>
31 #include <stdlib.h> /* C standard library */
33 #include <errno.h> /* error definitions and routines */
34 #include <stdio.h> /* standard I/O library */
35 #include <string.h> /* string functions */
40 /* Help printing routine */
43 int main(int argc, char *argv[])
49 * Input section: decode command line parameters
52 opterr = 0; /* don't want writing to stderr */
53 while ( (i = getopt(argc, argv, "hrwcda")) != -1) {
58 case 'h': /* help option */
59 printf("Wrong -h option use\n");
63 case 'r': /* read access */
66 case 'w': /* write access */
69 case 'c': /* creation */
72 case 'd': /* deletion */
75 case 'a': /* all events */
76 mask |= IN_ALL_EVENTS;
78 case '?': /* unrecognized options */
79 printf("Unrecognized options -%c\n",optopt);
81 default: /* should not reached */
85 /* ***********************************************************
87 * Options processing completed
91 * ***********************************************************/
92 if ((argc - optind) != 1) { /* There must be remaing parameters */
93 printf("Wrong number of arguments %d\n", argc - optind);
99 perror("Failing on inotify_init");
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));
112 * routine to print usage info and exit
115 printf("Program inotify_monitor: monitor file changes \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");