-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
82 lines (73 loc) · 2.09 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/inotify.h>
#include <signal.h>
int fd = -1;
int main(int argc, char *argv[])
{
typedef struct inotify_event EVENT ;
const EVENT* watchevent;
char* prompt;
char* path = NULL;
char* token = NULL;
const uint32_t bitmask = IN_ACCESS | IN_CREATE | IN_CLOSE | IN_DELETE | IN_MODIFY | IN_MOVE_SELF;
char buffer[4096];
int readn;
if (argc<2) {
fprintf(stderr, "bleh: try, bleh PATH");
exit(1);
}
path = malloc(sizeof(char)*(strlen(argv[1]))+1);
strcpy(path, argv[1]);
token = strtok(path, "/");
while (token!= NULL) {
path = token;
token = strtok(NULL, "/");
}
fd = inotify_init();
if (fd==1) {
fprintf(stderr, "Error init-ing the instance");
exit(2);
}
int watchman = inotify_add_watch(fd, argv[1], bitmask);
if (watchman==-1) {
fprintf(stderr, "Unable to add file to watchman");
exit(3);
}
while (1) {
printf("OOOOOOO");
readn = read(fd, buffer, sizeof(buffer));
if (readn==-1) {
fprintf(stderr, "another error msg");
exit(4);
}
for (char* ptr = buffer; ptr< buffer+ readn; ptr+=sizeof(EVENT)+watchevent->len) {
prompt = NULL;
watchevent = (const EVENT*) ptr;
if (watchevent->mask & IN_ACCESS) {
prompt = "File accessed";
}
if (watchevent->mask & IN_DELETE) {
prompt = "File deleted";
}
if (watchevent->mask & IN_CLOSE) {
prompt = "File closed";
}
if (watchevent->mask & IN_CREATE) {
prompt = "File created";
}
if (watchevent->mask & IN_MODIFY) {
prompt = "File modified";
}
if (watchevent->mask & IN_MOVE_SELF) {
prompt = "File moved";
}
printf("%s\n", prompt);
}
}
return EXIT_SUCCESS;
}