ACQ2XX_API
dir-watch.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <inotifytools/inotifytools.h>
00005 #include <inotifytools/inotify.h>
00006 
00007 /*
00008  * libinotifytools example program.
00009  * Compile with gcc -linotifytools example.c
00010  */
00011 
00012 #define DIR_WATCH_FMT   "DIR_WATCH_FMT"
00013 #define DIR_WATCH_EVENT "DIR_WATCH_EVENT"
00014 
00015 int main(int argc, char* argv[]) {
00016         const char* wd = argc > 1? argv[1]: ".";
00017         char fmt[80];
00018         unsigned long trig = IN_CLOSE_WRITE;
00019 
00020         if (getenv(DIR_WATCH_FMT)){
00021                 strcpy(fmt, getenv(DIR_WATCH_FMT));
00022                 strcat(fmt, "\n");
00023         }else{
00024                 strcpy(fmt, "%T %w%f %e\n"); 
00025         }
00026         if (getenv(DIR_WATCH_EVENT)){
00027                 trig = strtoul(getenv(DIR_WATCH_EVENT), 0, 0);
00028         }
00029 
00030         // initialize and watch the entire directory tree from the current working
00031         // directory downwards for all events
00032         if ( !inotifytools_initialize()
00033           || !inotifytools_watch_recursively( wd, trig) ) {
00034                 fprintf(stderr, "%s\n", strerror( inotifytools_error() ) );
00035                 return -1;
00036         }
00037 
00038         // set time format to 24 hour time, HH:MM:SS
00039         inotifytools_set_printf_timefmt( "%T" );
00040 
00041         // Output all events as "<timestamp> <path> <events>"
00042         struct inotify_event * event = inotifytools_next_event( -1 );
00043         while ( event ) {
00044                 inotifytools_printf( event, fmt );
00045                 fflush(stdout);
00046                 event = inotifytools_next_event( -1 );
00047         }
00048         return 0;
00049 }