Skip to content

Instantly share code, notes, and snippets.

@jkristoffer
Created May 12, 2015 06:42
Show Gist options
  • Save jkristoffer/8a064182330ea220e5b3 to your computer and use it in GitHub Desktop.
Save jkristoffer/8a064182330ea220e5b3 to your computer and use it in GitHub Desktop.
Unix Inotify in C
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/inotify.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];
char *p;
struct inotify_event *event;
fd = inotify_init();
if ( fd < 0 ) {
printf("inotify_init failed");
exit(EXIT_FAILURE);
}
wd = inotify_add_watch( fd, "/home/user/src/package.json", IN_MODIFY | IN_OPEN );
if ( wd < 0) {
printf("inotify_add_watch failed");
exit(EXIT_FAILURE);
}
printf("Using wd %d\n", wd);
for (;;) {
length = read( fd, buffer, EVENT_BUF_LEN );
if ( length < 0 ) {
printf("Something went wrong during read()");
perror( "read" );
}
for (p = buffer; p < buffer + length; ){
event = (struct inotify_event *) p;
if ( event->mask & IN_MODIFY ) {
/*system( "npm install" );*/
system("echo 'HelloWorld'");
}
p += EVENT_SIZE + event->len;
}
}
inotify_rm_watch( fd, wd );
close( fd );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment