title: How do I check if a file is executable using ANSI C?
body: <p>I'm trying to write a program that checks files in a directory. When a new file is created I have to check if it executable and if so I have to execute it.</p>
  
  <p>I'm using the inotify interface and it works well, but I have some problems when I try to check whether a file is executable using stat(2). I found that I don't have permission to execute it.</p>
  
  <p>Does passing to the program an absolute path of the directory that I want to check create permissions problems?</p>
  
  <pre><code>int main(int argc,char * argv []){
  
  int fd,wd,len,i=0;
  char buffer [EVENT_SIZE_BUF];
  
  if(argc != 2)
      usage();
  
  if((fd = inotify_init()) == -1)
      perror("inotify_init()");   
  
  
  if((wd = inotify_add_watch(fd,argv[1],IN_CREATE) -1))
      perror("inotify_add_watch()");
  
  while(1){
  
  
      if((len = read(fd,buffer,EVENT_SIZE_BUF)) &lt; 0)
          perror("read()");
  
      struct inotify_event * ev = (struct inotify_event *)&amp;buffer;
  
      if(ev-&gt;len &gt; 0){
  
          if(ev-&gt;mask &amp; IN_CREATE &amp;&amp; ((ev-&gt; mask &amp; IN_ISDIR) == 0x00)){
  
              printf("SPY; new file is created %s\n",ev-&gt;name);
  
              char * path = strcat(argv[1],ev-&gt;name);
              printf("%s\n",path);
  
              struct stat sb;
  
              if(!stat(path,&amp;sb)){
  
                  printf( (S_ISDIR(sb.st_mode)) ? "d" : "-");
                  printf( (sb.st_mode &amp; S_IRUSR) ? "r" : "-");
                  printf( (sb.st_mode &amp; S_IWUSR) ? "w" : "-");
                  printf( (sb.st_mode &amp; S_IXUSR) ? "x" : "-");
                  printf( (sb.st_mode &amp; S_IRGRP) ? "r" : "-");
                  printf( (sb.st_mode &amp; S_IWGRP) ? "w" : "-");
                  printf( (sb.st_mode &amp; S_IXGRP) ? "x" : "-");
                  printf( (sb.st_mode &amp; S_IROTH) ? "r" : "-");
                  printf( (sb.st_mode &amp; S_IWOTH) ? "w" : "-");
                  printf( (sb.st_mode &amp; S_IXOTH) ? "x" : "-");
                  fflush(stdout);
                  printf("\n");
              }
  
  
  
  
          }else{ printf("dir\n"); }
      }               
  }
  
  inotify_rm_watch(fd,wd);
  close(fd);
  return 0;
  }
  </code></pre>