From 74fd897a4388bbbf1dc5dc50ebe0656ee5d815e7 Mon Sep 17 00:00:00 2001 From: kremlin Date: Sun, 12 Oct 2014 10:30:34 -0400 Subject: [PATCH] finished hw6 --- assgn6/Makefile | 12 ++++++++ assgn6/filewatch.c | 73 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 assgn6/Makefile diff --git a/assgn6/Makefile b/assgn6/Makefile new file mode 100644 index 0000000..d4d686a --- /dev/null +++ b/assgn6/Makefile @@ -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 + diff --git a/assgn6/filewatch.c b/assgn6/filewatch.c index 3c0bc67..3dc47bc 100644 --- a/assgn6/filewatch.c +++ b/assgn6/filewatch.c @@ -1,21 +1,31 @@ #include +#include #include + #include #include +#include +#include +#include + 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; } -- 2.41.0