#include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { char *lex_path, *u_inp, *ful_p, *ev_buf, *parse_buf; DIR *path; struct dirent *dent; struct inotify_event *event; int inotify_fd, watch_fd, rbts; extern int errno; lex_path = (char *) calloc(1, NAME_MAX + 1); if(argc == 1) lex_path = "./"; else if(argc == 2) strlcpy(lex_path, argv[1], NAME_MAX + 1); else { printf("inappropriate number of args.\n"); return 1; } if(!(path = opendir(lex_path))) { if(errno == EACCES) printf("improper permissions to read dir.\n"); else if(errno == ENOENT) printf("given directory does not exist.\n"); else if(errno == ENFILE) printf("we're out of fd's.\n"); else if(errno == ENOTDIR) printf("argument is not a directory.\n"); else printf("an unspecified error occurred.\n"); return errno; } u_inp = (char *) calloc(1, NAME_MAX); ful_p = (char *) calloc(1, 8192); /* PATH_MAX workaround */ while(dent) if((dent = readdir(path)) != NULL) printf("%s\n", dent->d_name); closedir(path); printf("file to watch :: "); fgets(u_inp, NAME_MAX, stdin); strtok(u_inp, "\n"); /* trim newline */ strlcat(ful_p, lex_path, 8192); strlcat(ful_p, "/", NAME_MAX + 1); strlcat(ful_p, u_inp, 8192); if(!(inotify_fd = inotify_init())) { printf("could not access inotify api\n"); exit(1); } if((watch_fd = inotify_add_watch(inotify_fd, ful_p, IN_ACCESS | IN_MODIFY)) == -1) { printf("could not open watch on provided file\n"); exit(1); } ev_buf = (char *) calloc(1, 8192); parse_buf = (char *) calloc(1, 8192); for(;;) { rbts = read(inotify_fd, ev_buf, 8192); for(parse_buf = ev_buf; parse_buf < ev_buf + rbts;) { event = (struct inotify_event*) ev_buf; if(event->mask == 0x1) printf("file was read\n"); else printf("file was written\n"); parse_buf += sizeof(struct inotify_event) + event-> len; } } free(u_inp); free(ful_p); free(lex_path); free(ev_buf); free(parse_buf); return 0; }