X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=sources%2Finotify_monitor.c;fp=sources%2Finotify_monitor.c;h=d2c4f2dd543b684d340442327ff94297349782f9;hb=60e20d29c0515f95b8a171fb33c7214c9bf92021;hp=0000000000000000000000000000000000000000;hpb=fa8f6170f9e769b98378040e6e847e5a5db3b836;p=gapil.git diff --git a/sources/inotify_monitor.c b/sources/inotify_monitor.c new file mode 100644 index 0000000..d2c4f2d --- /dev/null +++ b/sources/inotify_monitor.c @@ -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 +#include +#include /* C standard library */ +#include +#include /* error definitions and routines */ +#include /* standard I/O library */ +#include /* 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); +}