joschua011

Untitled

Oct 31st, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6. #include <sys/inotify.h>
  7.  
  8. // make sure libc6 is installed for inotify
  9.  
  10. #define EVENT_SIZE ( sizeof (struct inotify_event) )
  11. #define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
  12.  
  13. int main( )
  14. {
  15. int length, i = 0;
  16. int fd;
  17. int wd;
  18. char buffer[EVENT_BUF_LEN];
  19. char ch;
  20.  
  21. fd = inotify_init();
  22.  
  23. if ( fd < 0 ) {
  24. perror( "inotify_init" );
  25. }
  26.  
  27. wd = inotify_add_watch( fd, "/tmp/source", IN_CREATE | IN_DELETE );
  28.  
  29. while(1){
  30. length = read( fd, buffer, EVENT_BUF_LEN );
  31.  
  32. if ( length < 0 ) {
  33. perror( "read" );
  34. }
  35.  
  36. while ( i < length ) { struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ]; if ( event->len ) {
  37. if ( event->mask & IN_CREATE ) {
  38. if ( event->mask & IN_ISDIR ) {
  39. printf("New directory %s created.\n", event->name );
  40. }
  41. else {
  42. printf("New file %s created.\n", event->name );
  43. }
  44. }
  45. else if ( event->mask & IN_DELETE ) {
  46. if ( event->mask & IN_ISDIR ) {
  47. printf( "Directory %s deleted.\n", event->name );
  48. }
  49. else {
  50. printf( "File %s deleted.\n", event->name );
  51. }
  52. }
  53. }
  54. i += EVENT_SIZE + event->len;
  55. }
  56. i=0;
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment