inotify.c (3375B)
1 //: generator/pp comment config 2 //:not cScript 3 #include "../release/libsheepy.h" 4 #include "inoty.h" 5 //:end 6 7 #define internal static 8 9 #include <stdlib.h> 10 #include <stdio.h> 11 12 #ifndef unitTest 13 #endif 14 int MAIN(int ARGC, char** ARGV); 15 16 int argc; char **argv; 17 18 #include <unistd.h> 19 #include <stdlib.h> 20 21 #ifndef unitTest 22 // Remove main when running the unit tests 23 #define MAIN main 24 #endif 25 int MAIN(int ARGC, char** ARGV) { 26 char **list = NULL; 27 int length; 28 int i; 29 int fd; 30 int wd; 31 char buffer[EVENT_BUF_LEN]; 32 33 argc = ARGC; argv = ARGV;;// run a commend when a c or h file is changed in current directory 34 35 // Steps 36 // check arguments 37 // print watched folders 38 // wait for changes and run command 39 40 // check arguments 41 if (argc < 2) { 42 printf("Give a command to run in parameter."); 43 printf("\n"); 44 XFAILURE; 45 } 46 47 printf("Started, will run >%s< when notified\n", argv[1]); { 48 printf("\n"); 49 50 // print watched folders 51 printf("Watching:"); 52 printf("\n"); 53 list = execOut("find . -type d"); 54 forEachCharP(list, l) { 55 printf("%s", *l); 56 printf("\n"); 57 } 58 59 // wait for changes and run command 60 while (1) { 61 62 // creating the INOTIFY instance 63 fd = inotiFy_init(); 64 if (fd == -1) { 65 perror("inotiFy_init error: "); 66 } 67 68 // adding the “/tmp” directory into watch list. Here, the suggestion is to validate the existence of the directory before adding into monitoring list. 69 // list of events: http://man7.org/linux/man-pages/man7/inotify.7.html 70 forEachCharP(list, e) { 71 wd = inotiFy_add_watch( fd, *e, IN_CREATE | IN_DELETE | IN_MODIFY);; 72 } 73 74 // read to determine the event change happens on “.” directory. Actually this read blocks until the change event occurs 75 length = read( fd, buffer, EVENT_BUF_LEN );; 76 77 //print '%d', length 78 79 if (length == -1) { 80 perror("read error: "); 81 } 82 83 i = 0; 84 // actually read return the list of change events happens. Here, read the change event one by one and process it accordingly. 85 while (( i < length )) { 86 struct inotiFy_event *event = ( struct inotiFy_event * ) &buffer[ i ]; 87 88 //print 'name %s', event->name 89 char *s = sliceS(event->name, -2, 0); 90 91 if (event->len && (strEq(s, ".c") || strEq(s, ".h"))) { 92 if ((event->mask & IN_CREATE) || (event->mask & IN_DELETE) || (event->mask & IN_MODIFY)) { 93 system(argv[1]); 94 } 95 } 96 /* if event->mask & IN_CREATE */ 97 /* if event->mask & IN_ISDIR */ 98 /* printf( "New directory %s created.\n", event->name ); */ 99 /* else */ 100 /* printf( "New file %s created.\n", event->name ); */ 101 /* else if event->mask & IN_DELETE */ 102 /* if event->mask & IN_ISDIR */ 103 /* printf( "Directory %s deleted.\n", event->name ); */ 104 /* else */ 105 /* printf( "File %s deleted.\n", event->name ); */ 106 /* else if event->mask & IN_MODIFY */ 107 /* if event->mask & IN_ISDIR */ 108 /* printf( "Directory %s modiFied.\n", event->name ); */ 109 /* else */ 110 /* printf( "File %s modiFied.\n", event->name ); */ 111 112 free(s); 113 i += EVENT_SIZE + event->len;; 114 } 115 116 117 // removing the “/tmp” directory from the watch list. 118 inotiFy_rm_watch( fd, wd ); 119 120 // closing the INOTIFY instance 121 close(fd); 122 } 123 124 XSUCCESS; 125 } 126 }