Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/inotify.h>
- #include <unistd.h>
- #define EVENT_SIZE ( sizeof (struct inotify_event) )
- #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
- const char * file_path = "/path/to/file";
- int main( int argc, char **argv )
- {
- int length, i;
- int fd;
- int wd;
- char buffer[BUF_LEN];
- fd = inotify_init();
- if ( fd < 0 ) {
- perror( "inotify_init" );
- }
- wd = inotify_add_watch(fd,
- file_path,
- IN_ALL_EVENTS);
- while(1)
- {
- length = read( fd, buffer, BUF_LEN );
- if ( length < 0 ) {
- perror( "read" );
- }
- i = 0;
- while ( i < length ) {
- struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
- printf("inotify event\n");
- if ( event->len ) {
- if ( event->mask & IN_CREATE ) {
- if ( event->mask & IN_ISDIR ) {
- printf( "The directory %s was created.\n", event->name );
- }
- else {
- printf( "The file %s was created.\n", event->name );
- }
- }
- else if ( event->mask & IN_DELETE ) {
- if ( event->mask & IN_ISDIR ) {
- printf( "The directory %s was deleted.\n", event->name );
- }
- else {
- printf( "The file %s was deleted.\n", event->name );
- }
- }
- else if ( event->mask & IN_MODIFY ) {
- if ( event->mask & IN_ISDIR ) {
- printf( "The directory %s was modified.\n", event->name );
- }
- else {
- printf( "The file %s was modified.\n", event->name );
- }
- }
- }
- i += EVENT_SIZE + event->len;
- }
- }
- ( void ) inotify_rm_watch( fd, wd );
- ( void ) close( fd );
- exit( 0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement