From 0fba3a76bd85ddde23cd442b9e0bc16612dc3f8a Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 4 Feb 2019 15:49:09 +0100 Subject: Ovh reboot --- inotify.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 inotify.c (limited to 'inotify.c') diff --git a/inotify.c b/inotify.c new file mode 100644 index 0000000..5ef0bf2 --- /dev/null +++ b/inotify.c @@ -0,0 +1,62 @@ +/*This is the sample program to notify us for the file creation and file deletion takes place in “/tmp” directory*/ +#include +#include +#include +#include +#include +#include +#include + +#define EVENT_SIZE ( sizeof (struct inotify_event) ) +#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) + +int main(void) { + int length, i = 0; + int fd; + int wd; + char buffer[EVENT_BUF_LEN]; + + /*creating the INOTIFY instance*/ + fd = inotify_init(); + + /*checking for error*/ + if ( fd < 0 ) { + perror( "inotify_init" ); + } + + /*adding the “/tmp” directory into watch list. Here, the suggestion is to validate the existence of the directory before adding into monitoring list.*/ + wd = inotify_add_watch( fd, "/home/ache/.tmp", IN_CREATE ); + + /*actually read return the list of change events happens. Here, read the change event one by one and process it accordingly.*/ + while ( 1 ) { + read( fd, buffer, EVENT_BUF_LEN ); + struct inotify_event *event = buffer + i; + if ( event->len ) { + if ( event->mask & IN_CREATE ) { + if ( event->mask & IN_ISDIR ) { + printf( "New directory %s created.\n", event->name ); + } + else { + printf( "New file %s created.\n", event->name ); + if( strcmp(event->name, "fox") == 0 ) { + sleep(2); + puts("🦊"); + break; + char filename[512] = "/home/ache/.tmp"; + char filename_out[512] = "/tmp/"; + strcat(filename, event->name); + strcat(filename_out, event->name); + rename(filename, filename_out); + } + } + } + } + } + /*removing the “/tmp” directory from the watch list.*/ + inotify_rm_watch( fd, wd ); + + /*closing the INOTIFY instance*/ + close( fd ); + + return EXIT_SUCCESS; +} -- cgit v1.2.3