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
| #include <sys/inotify.h>
#include <stdlib.h>
#include <stdio.h>
// File descriptor & watch descriptor
int fd, wd;
int main() {
// event structure
struct inotify_event *event;
// creates a notify instance
fd = inotify_init();
if(fd < 0) {
perror("inotify_init");
return EXIT_FAILURE;
}
while(1) {
// Starts waiting for file modification
wd = inotify_add_watch( fd ,"./testin.txt", IN_MODIFY);
if(wd < 0) {
perror("inotify add watch ");
return EXIT_FAILURE;
}
size_t r;
char buffer[8192];
// Get informaation when an event occurs
r = read(fd, buffer, sizeof(buffer));
if (r <= 0) {
perror("read");
return EXIT_FAILURE;
}
event = (struct inotify_event *) buffer;
printf("Fichier surveillé n°%d\t", event->wd);
}
return 0;
} |
Partager