/*This is the sample program to notify us for the file creation and file deletion takes place in “/tmp” directory*/ #include #include #include #include #include #include #include #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) int main(void) { int length, i = 0; int fd; int wd; char buffer[EVENT_BUF_LEN]; /*creating the INOTIFY instance*/ fd = inotify_init(); /*checking for error*/ if ( fd < 0 ) { perror( "inotify_init" ); } /*adding the “/tmp” directory into watch list. Here, the suggestion is to validate the existence of the directory before adding into monitoring list.*/ wd = inotify_add_watch( fd, "/home/ache/.tmp", IN_CREATE ); /*actually read return the list of change events happens. Here, read the change event one by one and process it accordingly.*/ while ( 1 ) { read( fd, buffer, EVENT_BUF_LEN ); struct inotify_event *event = buffer + i; if ( event->len ) { if ( event->mask & IN_CREATE ) { if ( event->mask & IN_ISDIR ) { printf( "New directory %s created.\n", event->name ); } else { printf( "New file %s created.\n", event->name ); if( strcmp(event->name, "fox") == 0 ) { sleep(2); puts("🦊"); break; char filename[512] = "/home/ache/.tmp"; char filename_out[512] = "/tmp/"; strcat(filename, event->name); strcat(filename_out, event->name); rename(filename, filename_out); } } } } } /*removing the “/tmp” directory from the watch list.*/ inotify_rm_watch( fd, wd ); /*closing the INOTIFY instance*/ close( fd ); return EXIT_SUCCESS; }