#include #include #include #include int main(int argc, char **argv) { int watchcount = 0; int watchlist[10]; char *watchlistrev[100000]; if(argc < 2) { printf("Please specify something to watch!\n"); } int myq = inotify_init(); if(myq == -1) { perror("inotify_init() failed"); exit(1); } /* skip argv[0] */ argc--; *argv++; printf("Setting up...\n"); while(argc) { printf("... adding %s...\n",*argv); int wd = inotify_add_watch(myq, *argv, IN_ALL_EVENTS); if(wd == -1) { perror("inotify_add_watch() error"); } else { watchlist[watchcount++] = wd; watchlistrev[wd] = strdup(*argv); } argc--; *argv++; } // struct inotify_event event; char buffer[1024]; printf("\n\nGo!\n"); int count = 0; while(1) { if(watchcount == 0) { printf("\n\nThe number of watched items is zero. Exiting!\n"); exit(0); } int ret; ret = read(myq,buffer,1024); if(ret < 0) { perror("read() error"); exit(2); } if(ret > 0) { count++; struct inotify_event *event = (struct inotify *)buffer; // not beautiful ;-( printf("event %d: activity on %s:\n",count,watchlistrev[event->wd]); printf(" ... "); if(IN_ACCESS & event->mask) { printf(" *access made* "); } if(IN_ATTRIB & event->mask) { printf(" *attribute change* "); } if(IN_CLOSE_WRITE & event->mask) { printf(" *file opened for writing was closed* "); } if(IN_CLOSE_NOWRITE & event->mask) { printf(" *file not opened for writing was closed* "); } if(IN_CREATE & event->mask) { printf(" *file was created* "); } if(IN_DELETE & event->mask) { printf(" *file was deleted* "); } if(IN_DELETE_SELF & event->mask) { printf(" *watched file was deleted* "); watchcount--; } if(IN_MODIFY & event->mask) { printf(" *file was modified* "); } if(IN_MOVE_SELF & event->mask) { printf(" *watched filed was moved* "); } if(IN_MOVED_FROM & event->mask) { printf(" *file was moved from watched directory* "); } if(IN_MOVED_TO & event->mask) { printf(" *file was moved into watched directory* "); } if(IN_OPEN & event->mask) { printf(" *file was opened* "); } printf(" ...\n"); if(event->len > 0) { printf(" .... filename == '%s'\n",event->name); } printf("\n\n"); } } }