[cc lang="bash"]
\$ vi notifier.c
\$ gcc notifier.c -o notifier
\$ ./notifier
file: test-touch, event: 0x00000100
file: test-touch, event: 0x00000020
file: test-touch, event: 0x00000004
file: test-touch, event: 0x00000008
[/cc]
[cc lang="bash"]
\$ touch /tmp/test-touch
\$ grep '0x00000100' /usr/include/sys/inotify.h
#define IN_CREATE 0x00000100 /* Subfile was created. */
\$ grep '0x00000020' /usr/include/sys/inotify.h
#define IN_OPEN 0x00000020 /* File was opened. */
\$ grep '0x00000004' /usr/include/sys/inotify.h
#define IN_ATTRIB 0x00000004 /* Metadata changed. */
\$ grep '0x00000008' /usr/include/sys/inotify.h
#define IN_CLOSE_WRITE 0x00000008 /* Writtable file was closed.
*/
[/cc]
notifier.c
[cc lang="bash"]
#include
#include
#include
#include
#include
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int main( ) {
int length, i;
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 directory into watch list - validate the existence of the
directory before adding into monitoring list*/
wd = inotify_add_watch( fd, "/tmp", IN_ALL_EVENTS );
// loop
for (;;) {
i = 0;
/*read to determine the event change happens on directory; actually
this read blocks until the change event occurs*/
length = read( fd, buffer, EVENT_BUF_LEN );
/*checking for error*/
if ( length \< 0 ) {
perror( "read" );
}
/*actually read return the list of change events happens; read the
change event one by one and process it accordingly*/
while ( i \< length ) {
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i
];
if ( event->len ) {
printf( "file: %s, event: %#010x\n", event->name, event->mask );
}
i += EVENT_SIZE + event->len;
}
}
// end loop
/*removing the directory from the watch list*/
inotify_rm_watch( fd, wd );
/*closing the INOTIFY instance*/
close( fd );
}
[/cc]
source: http://www.thegeekstuff.com/2010/04/inotify-c-program-example/