Uniformate la tabelle, riletta la sezione su epoll, altre correzioni
[gapil.git] / sources / inotify_monitor.c
diff --git a/sources/inotify_monitor.c b/sources/inotify_monitor.c
new file mode 100644 (file)
index 0000000..d2c4f2d
--- /dev/null
@@ -0,0 +1,125 @@
+/* inotify_monitor.c
+ * 
+ * Copyright (C) 2007 Simone Piccardi
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/*****************************************************************************
+ *
+ * File inotufy_monitor.c: 
+ *
+ * An example for shared memory use: monitor a directory status,
+ * saving data in a shared memory segment
+ *
+ * Author: S. Piccardi Jul. 2007
+ *
+ *****************************************************************************/
+#include <sys/types.h>
+#include <sys/inotify.h>
+#include <stdlib.h>        /* C standard library */
+#include <unistd.h>
+#include <errno.h>       /* error definitions and routines */ 
+#include <stdio.h>       /* standard I/O library */
+#include <string.h>      /* string functions */
+
+
+#include "macros.h"
+
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[]) 
+{
+    int i;
+    int fd;
+    unsigned int mask=0;
+    /*
+     * Input section: decode command line parameters 
+     * Use getopt function
+     */
+    opterr = 0;         /* don't want writing to stderr */
+    while ( (i = getopt(argc, argv, "hrwcda")) != -1) {
+       switch (i) {
+       /* 
+        * Handling options 
+        */ 
+       case 'h':                                             /* help option */
+           printf("Wrong -h option use\n");
+           usage();
+           return -1;
+           break;
+       case 'r':                                             /* read access */
+           mask |= IN_ACCESS;
+           break;
+       case 'w':                                            /* write access */
+           mask |= IN_MODIFY;
+           break;
+       case 'c':                                                /* creation */
+           mask |= IN_CREATE;
+           break;
+       case 'd':                                                /* deletion */
+           mask |= IN_DELETE;
+           break;
+       case 'a':                                              /* all events */
+           mask |= IN_ALL_EVENTS;
+           break;
+       case '?':                                    /* unrecognized options */
+           printf("Unrecognized options -%c\n",optopt);
+           usage();
+       default:                                       /* should not reached */
+           usage();
+       }
+    }
+    /* ***********************************************************
+     * 
+     *          Options processing completed
+     *
+     *               Main code beginning
+     * 
+     * ***********************************************************/
+    if ((argc - optind) != 1) {          /* There must be remaing parameters */
+       printf("Wrong number of arguments %d\n", argc - optind);
+        usage();
+    }
+    /* initalialize */
+    fd = inotify_init ();
+    if (fd < 0)
+        perror("Failing on inotify_init");
+
+    /* add watch */
+    if (inotify_add_watch(fd, argv[1], mask) != 0) {
+       printf("Failing to add watched file %s; %s\n", 
+             argv[1], strerror(errno));
+       exit(-1);
+    }
+
+    return 0;
+
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+    printf("Program inotify_monitor: monitor file changes \n");
+    printf("Usage:\n");
+    printf(" inotify_monitor [-h] -rwcd dirname/filename \n");
+    printf("  -h          print this help\n");
+    printf("  -w          watch write\n");
+    printf("  -r          watch read\n");
+    printf("  -c          watch create\n");
+    printf("  -d          watch delete\n");
+    printf("  -a          watch all\n");
+    exit(1);
+}