finished hw6
authorkremlin <ian@kremlin.cc>
Sun, 12 Oct 2014 14:30:34 +0000 (10:30 -0400)
committerkremlin <ian@kremlin.cc>
Sun, 12 Oct 2014 14:30:34 +0000 (10:30 -0400)
assgn6/Makefile [new file with mode: 0644]
assgn6/filewatch.c

diff --git a/assgn6/Makefile b/assgn6/Makefile
new file mode 100644 (file)
index 0000000..d4d686a
--- /dev/null
@@ -0,0 +1,12 @@
+.PHONY: all
+
+CC=/usr/bin/gcc
+CARGS=-Wall -Werror -Wextra -pedantic -Wno-unused-parameter -Wno-unused -std=gnu99 -lbsd
+SRC=filewatch.c -o filewatch
+
+all:
+       $(CC) $(CARGS) $(SRC) 
+
+debug:
+       $(CC) $(CARGS) $(SRC) -g -O0
+
index 3c0bc671ceccdca4c6b13a81c8dfaa3180ac81e5..3dc47bc8a516c17ae2bfec73079be3736b811306 100644 (file)
@@ -1,21 +1,31 @@
 #include <unistd.h>
+#include <stdlib.h>
 #include <stdio.h>
+
 #include <dirent.h>
 #include <errno.h>
 
+#include <bsd/string.h>
+#include <sys/stat.h>
+#include <sys/inotify.h>
+
 int main(int argc, char *argv[]) {
 
-    char *lex_path;
+    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)
-        lex_path = argv[1];
+        strlcpy(lex_path, argv[1], NAME_MAX + 1);
 
     else {
 
@@ -23,7 +33,7 @@ int main(int argc, char *argv[]) {
         return 1;
     }
 
-    if(!(path = opendir(path))) {
+    if(!(path = opendir(lex_path))) {
 
         if(errno == EACCES)
             printf("improper permissions to read dir.\n");
@@ -43,10 +53,61 @@ int main(int argc, char *argv[]) {
         return errno;
     }
 
-    if((dent = readdir(path)) != NULL) {
+       u_inp = (char *) calloc(1, NAME_MAX);
+       ful_p = (char *) calloc(1, 8192); /* PATH_MAX workaround */ 
 
-        if(
-    }
+       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;
 }