3dc47bc8a516c17ae2bfec73079be3736b811306
8 #include <bsd/string.h>
10 #include <sys/inotify.h>
12 int main(int argc
, char *argv
[]) {
14 char *lex_path
, *u_inp
, *ful_p
, *ev_buf
, *parse_buf
;
17 struct inotify_event
*event
;
18 int inotify_fd
, watch_fd
, rbts
;
22 lex_path
= (char *) calloc(1, NAME_MAX
+ 1);
28 strlcpy(lex_path
, argv
[1], NAME_MAX
+ 1);
32 printf("inappropriate number of args.\n");
36 if(!(path
= opendir(lex_path
))) {
39 printf("improper permissions to read dir.\n");
41 else if(errno
== ENOENT
)
42 printf("given directory does not exist.\n");
44 else if(errno
== ENFILE
)
45 printf("we're out of fd's.\n");
47 else if(errno
== ENOTDIR
)
48 printf("argument is not a directory.\n");
51 printf("an unspecified error occurred.\n");
56 u_inp
= (char *) calloc(1, NAME_MAX
);
57 ful_p
= (char *) calloc(1, 8192); /* PATH_MAX workaround */
60 if((dent
= readdir(path
)) != NULL
)
61 printf("%s\n", dent
->d_name
);
65 printf("file to watch :: ");
67 fgets(u_inp
, NAME_MAX
, stdin
);
68 strtok(u_inp
, "\n"); /* trim newline */
70 strlcat(ful_p
, lex_path
, 8192);
71 strlcat(ful_p
, "/", NAME_MAX
+ 1);
72 strlcat(ful_p
, u_inp
, 8192);
74 if(!(inotify_fd
= inotify_init())) {
76 printf("could not access inotify api\n");
80 if((watch_fd
= inotify_add_watch(inotify_fd
, ful_p
, IN_ACCESS
| IN_MODIFY
)) == -1) {
82 printf("could not open watch on provided file\n");
86 ev_buf
= (char *) calloc(1, 8192);
87 parse_buf
= (char *) calloc(1, 8192);
91 rbts
= read(inotify_fd
, ev_buf
, 8192);
93 for(parse_buf
= ev_buf
; parse_buf
< ev_buf
+ rbts
;) {
95 event
= (struct inotify_event
*) ev_buf
;
97 if(event
->mask
== 0x1)
98 printf("file was read\n");
100 printf("file was written\n");
102 parse_buf
+= sizeof(struct inotify_event
) + event
-> len
;