-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.c
46 lines (44 loc) · 1.08 KB
/
10.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
#include <unistd.h>
#include <sys/inotify.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#define Fail(msg) \
do {perror(msg); return 1;} while(0);
int main(int argc, char* argv[])
{
if (argc!=2)
Fail("Wrong input");
int inotfd, watchdesc;
size_t const max_size = sizeof(struct inotify_event) + NAME_MAX + 1;
size_t size;
void * buf;
uint32_t cookie;
buf = (void *) malloc(max_size);
inotfd = inotify_init();
if (inotfd == -1) Fail("Fail to init\n");
watchdesc = inotify_add_watch(inotfd,argv[1] , IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE);
if (watchdesc == -1)
Fail("Fail to add_watch\n");
do {
size = read(inotfd, buf, max_size);
if ((ssize_t)size == -1)
Fail("Fail to read\n");
struct inotify_event *struc = buf;
switch(struc->mask) {
case IN_CREATE :
printf("event: CREATION, name: %s\n", struc->name);
break;
case IN_MOVED_FROM :
cookie = struc->cookie;
break;
case IN_MOVED_TO :
if (struc->cookie != cookie) {
printf("event: MOVING, name: %s\n", struc->name);
}
cookie = 0;
break;
}
} while(1);
return(0);
}