title: what is S_IFMT in UNIX system programming?
body: <p>I am learning system calls and was thus writing a code to implement ls using C language. The code works, but I cannot understand the working of</p>
  
  <pre><code>val=(mystat.st_mode &amp; ~S_IFMT)
  </code></pre>
  
  <p>in the code given below? I understand the rest of the code.</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;time.h&gt;
  
  int main(int argc, char* argv[])
  {
      DIR *mydir;
      struct dirent *myfile;
      struct stat mystat;
  
      mydir = opendir(argv[1]);
      char buf[512];
      while((myfile = readdir(mydir)) != NULL)
      {
          struct tm *time_stamp=localtime(&amp;mystat.st_mtime);
          sprintf(buf, "%s/%s", argv[1], myfile-&gt;d_name);
          stat(buf, &amp;mystat);
          //stat(myfile-&gt;d_name, &amp;mystat);   
          mode_t val;
  
          val=(mystat.st_mode &amp; ~S_IFMT);
          (val &amp; S_IRUSR) ? printf("r") : printf("-");
          (val &amp; S_IWUSR) ? printf("w") : printf("-");    
          (val &amp; S_IXUSR) ? printf("x") : printf("-");
          (val &amp; S_IRGRP) ? printf("r") : printf("-");
          (val &amp; S_IWGRP) ? printf("w") : printf("-");
          (val &amp; S_IXGRP) ? printf("x") : printf("-");
          (val &amp; S_IROTH) ? printf("r") : printf("-");
          (val &amp; S_IWOTH) ? printf("w") : printf("-");
          (val &amp; S_IXOTH) ? printf("x") : printf("-");
          printf("\t%d",mystat.st_nlink);
          printf("\t%d",mystat.st_uid);
          printf("\t%d",mystat.st_gid); 
          printf("\t%d",mystat.st_size);
          char buffer[80];
          strftime(buffer,10,"%b",time_stamp);
  
          printf("\t%4d %s %2d ", time_stamp-&gt;tm_year+1900,buffer,time_stamp-&gt;tm_mday);
          printf(" %s\n", myfile-&gt;d_name);
      }
      closedir(mydir);
  }
  </code></pre>