title: inotify program in c create extra notifications
body: <p>while creating a file this program displays as follows when created "untitled document" and after that it renamed to "rr.txt"</p>
  
  <pre><code>The file Untitled Document was Created with WD 1
  The file Untitled Document was opened with WD 1
  The writtable file Untitled Document was closed with WD 1
  The file Untitled Document was opened with WD 1
  unwrittable file Untitled Document was closed with WD 1
  The file Untitled Document was opened with WD 1
  unwrittable file Untitled Document was closed with WD 1
  The file Untitled Document was opened with WD 1
  unwrittable file Untitled Document was closed with WD 1
  The file Untitled Document was opened with WD 1
  unwrittable file Untitled Document was closed with WD 1
  The file Untitled Document was opened with WD 1
  unwrittable file Untitled Document was closed with WD 1
  The file Untitled Document was opened with WD 1
  unwrittable file Untitled Document was closed with WD 1
  The file Untitled Document was opened with WD 1
  unwrittable file Untitled Document was closed with WD 1
  The file Untitled Document was opened with WD 1
  unwrittable file Untitled Document was closed with WD 1
  The file Untitled Document was opened with WD 1
  unwrittable file Untitled Document was closed with WD 1
  The file Untitled Document was moved from X with WD 1
  The file rr.txt was moved to Y with WD 1
  The file rr.txt was opened with WD 1
  unwrittable file rr.txt was closed with WD 1
  </code></pre>
  
  <p>the program written as follows</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/inotify.h&gt;
  #include &lt;limits.h&gt;
  
  #define MAX_EVENTS 1024 /*Max. number of events to process at one go*/
  #define LEN_NAME 16 /*Assuming that the length of the filename won't exceed 16 bytes*/
  #define EVENT_SIZE  ( sizeof (struct inotify_event) ) /*size of one event*/
  #define BUF_LEN     ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME )) /*buffer to store the data of events*/
  
  int main( int argc, char **argv )
  {
      int length, i = 0, wd;
      int fd;
      char buffer[BUF_LEN];
  
      /* Initialize Inotify*/
      fd = inotify_init();
      if ( fd &lt; 0 )
      {
          perror( "Couldn't initialize inotify");
      }
  
      /* add watch to starting directory */
      wd = inotify_add_watch(fd, argv[1], IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
  
      if (wd == -1)
      {
          printf("Couldn't add watch to %s\n",argv[1]);
      }
      else
      {
          printf("Watching:: %s\n",argv[1]);
      }
  
      /* do it forever*/
      while(1)
      {
          i = 0;
          length = read( fd, buffer, BUF_LEN );
  
          if ( length &lt; 0 )
          {
              perror( "read" );
          }
  
          while ( i &lt; length )
          {
              struct inotify_event *event = ( struct inotify_event * ) &amp;buffer[ i ];
              if ( event-&gt;len )
              {
                  if ( event-&gt;mask &amp; IN_CREATE)
                  {
                      if (event-&gt;mask &amp; IN_ISDIR)
                          printf( "The directory %s was created.\n", event-&gt;name );
                      else
                          printf( "The file %s was Created with WD %d\n", event-&gt;name, event-&gt;wd );     
                  }
  
                  if ( event-&gt;mask &amp; IN_MODIFY)
                  {
                      if (event-&gt;mask &amp; IN_ISDIR)                                 
                          printf( "The directory %s was modified.\n", event-&gt;name );     
                      else
                          printf( "The file %s was modified with WD %d\n", event-&gt;name, event-&gt;wd );     
                  }
  
                  if ( event-&gt;mask &amp; IN_DELETE)
                  {
                      if (event-&gt;mask &amp; IN_ISDIR)
                          printf( "The directory %s was deleted.\n", event-&gt;name );     
                      else
                          printf( "The file %s was deleted with WD %d\n", event-&gt;name, event-&gt;wd );     
                  }
  
                  if ( event-&gt;mask &amp; IN_ATTRIB)
                  {
                      if (event-&gt;mask &amp; IN_ISDIR)
                          printf( "The metadata of directory %s was changed.\n", event-&gt;name );
                      else
                          printf( "The metadata of file %s was changed with WD %d\n", event-&gt;name, event-&gt;wd );
                  }
  
                  if ( event-&gt;mask &amp; IN_CLOSE_WRITE)
                  {
                      printf( "The writtable file %s was closed with WD %d\n", event-&gt;name, event-&gt;wd );
                  }
  
                  if ( event-&gt;mask &amp; IN_CLOSE_NOWRITE)
                  {
                      printf( "unwrittable file %s was closed with WD %d\n", event-&gt;name, event-&gt;wd );
                  }
  
                  if ( event-&gt;mask &amp; IN_OPEN)
                  {
                      if (event-&gt;mask &amp; IN_ISDIR)
                          printf( "The directory %s was opened.\n", event-&gt;name );
                      else
                          printf( "The file %s was opened with WD %d\n", event-&gt;name, event-&gt;wd );
                  }
  
                  if ( event-&gt;mask &amp; IN_MOVED_FROM)
                  {
                      if (event-&gt;mask &amp; IN_ISDIR)
                          printf( "The directory %s was moved from X.\n", event-&gt;name );
                      else
                          printf( "The file %s was moved from X with WD %d\n", event-&gt;name, event-&gt;wd );
                  }
  
                  if ( event-&gt;mask &amp; IN_MOVED_TO)
                  {
                      if (event-&gt;mask &amp; IN_ISDIR)
                          printf( "The directory %s was moved to Y\n", event-&gt;name );
                      else
                          printf( "The file %s was moved to Y with WD %d\n", event-&gt;name, event-&gt;wd );
                  }
  
                  if ( event-&gt;mask &amp; IN_DELETE_SELF)
                  {
                      if (event-&gt;mask &amp; IN_ISDIR)
                          printf( "The directory %s was deleted.\n", event-&gt;name );
                      else
                          printf( "The file %s was deleted with WD %d\n", event-&gt;name, event-&gt;wd );
                  }
  
                  if ( event-&gt;mask &amp; IN_ACCESS)
                  {
                      if (event-&gt;mask &amp; IN_ISDIR)
                          printf( "The directory %s was accessed.\n", event-&gt;name );
                      else
                          printf( "The file %s was accessed with WD %d\n", event-&gt;name, event-&gt;wd );
                  }
  
                  i += EVENT_SIZE + event-&gt;len;
              }
          }
      }
  
      /* Clean up*/
      inotify_rm_watch( fd, wd );
      close( fd );
  
      return 0;
  }
  </code></pre>
  
  <p>i didnt open ad close the file. but many times prints it open and close. also prints it moved which i didnt do. plz help me to correct the bug</p>