fb4aa7ce |
1 | #include <unistd.h> |
74fd897a |
2 | #include <stdlib.h> |
fb4aa7ce |
3 | #include <stdio.h> |
74fd897a |
4 | |
fb4aa7ce |
5 | #include <dirent.h> |
6 | #include <errno.h> |
7 | |
74fd897a |
8 | #include <bsd/string.h> |
9 | #include <sys/stat.h> |
10 | #include <sys/inotify.h> |
11 | |
fb4aa7ce |
12 | int main(int argc, char *argv[]) { |
13 | |
74fd897a |
14 | char *lex_path, *u_inp, *ful_p, *ev_buf, *parse_buf; |
fb4aa7ce |
15 | DIR *path; |
16 | struct dirent *dent; |
74fd897a |
17 | struct inotify_event *event; |
18 | int inotify_fd, watch_fd, rbts; |
fb4aa7ce |
19 | |
20 | extern int errno; |
21 | |
74fd897a |
22 | lex_path = (char *) calloc(1, NAME_MAX + 1); |
23 | |
fb4aa7ce |
24 | if(argc == 1) |
25 | lex_path = "./"; |
26 | |
27 | else if(argc == 2) |
74fd897a |
28 | strlcpy(lex_path, argv[1], NAME_MAX + 1); |
fb4aa7ce |
29 | |
30 | else { |
31 | |
32 | printf("inappropriate number of args.\n"); |
33 | return 1; |
34 | } |
35 | |
74fd897a |
36 | if(!(path = opendir(lex_path))) { |
fb4aa7ce |
37 | |
38 | if(errno == EACCES) |
39 | printf("improper permissions to read dir.\n"); |
40 | |
41 | else if(errno == ENOENT) |
42 | printf("given directory does not exist.\n"); |
43 | |
44 | else if(errno == ENFILE) |
45 | printf("we're out of fd's.\n"); |
46 | |
47 | else if(errno == ENOTDIR) |
48 | printf("argument is not a directory.\n"); |
49 | |
50 | else |
51 | printf("an unspecified error occurred.\n"); |
52 | |
53 | return errno; |
54 | } |
55 | |
74fd897a |
56 | u_inp = (char *) calloc(1, NAME_MAX); |
57 | ful_p = (char *) calloc(1, 8192); /* PATH_MAX workaround */ |
fb4aa7ce |
58 | |
74fd897a |
59 | while(dent) |
60 | if((dent = readdir(path)) != NULL) |
61 | printf("%s\n", dent->d_name); |
62 | |
63 | closedir(path); |
64 | |
65 | printf("file to watch :: "); |
66 | |
67 | fgets(u_inp, NAME_MAX, stdin); |
68 | strtok(u_inp, "\n"); /* trim newline */ |
69 | |
70 | strlcat(ful_p, lex_path, 8192); |
71 | strlcat(ful_p, "/", NAME_MAX + 1); |
72 | strlcat(ful_p, u_inp, 8192); |
73 | |
74 | if(!(inotify_fd = inotify_init())) { |
75 | |
76 | printf("could not access inotify api\n"); |
77 | exit(1); |
78 | } |
79 | |
80 | if((watch_fd = inotify_add_watch(inotify_fd, ful_p, IN_ACCESS | IN_MODIFY)) == -1) { |
81 | |
82 | printf("could not open watch on provided file\n"); |
83 | exit(1); |
84 | } |
85 | |
86 | ev_buf = (char *) calloc(1, 8192); |
87 | parse_buf = (char *) calloc(1, 8192); |
88 | |
89 | for(;;) { |
90 | |
91 | rbts = read(inotify_fd, ev_buf, 8192); |
92 | |
93 | for(parse_buf = ev_buf; parse_buf < ev_buf + rbts;) { |
94 | |
95 | event = (struct inotify_event*) ev_buf; |
96 | |
97 | if(event->mask == 0x1) |
98 | printf("file was read\n"); |
99 | else |
100 | printf("file was written\n"); |
101 | |
102 | parse_buf += sizeof(struct inotify_event) + event-> len; |
103 | } |
104 | } |
105 | |
106 | free(u_inp); |
107 | free(ful_p); |
108 | free(lex_path); |
109 | free(ev_buf); |
110 | free(parse_buf); |
fb4aa7ce |
111 | |
112 | return 0; |
113 | } |