-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.c
97 lines (93 loc) · 2.63 KB
/
final.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/inotify.h>
#include <unistd.h>
#include <sys/stat.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int main( )
{
int length, i = 0;
int fd;
int wd;
char buffer[EVENT_BUF_LEN];
fd = inotify_init();
if ( fd < 0 ) {
perror( "inotify_init" );
}
wd = inotify_add_watch( fd, "/home/hp-3564/project", IN_CREATE | IN_DELETE | IN_CLOSE_WRITE | IN_MOVED_FROM | IN_MOVED_TO); //give the location of the directory to be watched here
length = read( fd, buffer, EVENT_BUF_LEN );
if ( length < 0 ) {
perror( "read" );
}
while ( i < length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
if ( event->mask & IN_CREATE ) {
if ( event->mask & IN_ISDIR ) {
printf( "New directory %s created.\n", event->name );
system("./a.out");
}
else
{
printf( "New file %s created.\n", event->name );
system("./a.out");
}
}
else if ( event->mask & IN_DELETE ) {
if ( event->mask & IN_ISDIR ) {
printf( "Directory %s deleted.\n", event->name );
system("./a.out");
}
else {
printf( "File %s deleted.\n", event->name);
system("./a.out");
}
}
else if(event->mask & IN_CLOSE_WRITE)
{
if(event->mask & IN_ISDIR)
{
printf("Directory %s modified.\n",event->name);
system("./a.out");
}
else
{
printf("File %s modified.\n",event->name);
system("./a.out");
}
}
else if(event->mask & IN_MOVED_FROM)
{
if(event->mask & IN_ISDIR)
{
printf("Directory %s moved out of watch directory.\n",event->name);
system("./a.out");
}
else
{
printf("File %s moved out of watch directory.\n",event->name);
system("./a.out");
}
}
else if(event->mask & IN_MOVED_TO)
{
if(event->mask & IN_ISDIR)
{
printf("Directory %s moved to watch directory.\n",event->name);
system("./a.out");
}
else
{
printf("File %s moved to watch directory.\n",event->name);
system("./a.out");
}
}
}
i += EVENT_SIZE + event->len;
}
inotify_rm_watch( fd, wd );
close( fd );
}