在UNIX系统编程中,S_IFMT是什么?

use*_*497 5 c linux operating-system system-calls

我正在学习系统调用,因此正在编写代码以使用C语言实现ls。该代码有效,但我无法理解

val=(mystat.st_mode & ~S_IFMT)
Run Code Online (Sandbox Code Playgroud)

在下面给出的代码中?我了解其余的代码。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>

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(&mystat.st_mtime);
        sprintf(buf, "%s/%s", argv[1], myfile->d_name);
        stat(buf, &mystat);
        //stat(myfile->d_name, &mystat);   
        mode_t val;

        val=(mystat.st_mode & ~S_IFMT);
        (val & S_IRUSR) ? printf("r") : printf("-");
        (val & S_IWUSR) ? printf("w") : printf("-");    
        (val & S_IXUSR) ? printf("x") : printf("-");
        (val & S_IRGRP) ? printf("r") : printf("-");
        (val & S_IWGRP) ? printf("w") : printf("-");
        (val & S_IXGRP) ? printf("x") : printf("-");
        (val & S_IROTH) ? printf("r") : printf("-");
        (val & S_IWOTH) ? printf("w") : printf("-");
        (val & 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->tm_year+1900,buffer,time_stamp->tm_mday);
        printf(" %s\n", myfile->d_name);
    }
    closedir(mydir);
}
Run Code Online (Sandbox Code Playgroud)

gen*_*ave 5

S_IFMT是文件类型的位掩码(请参阅man stat

直接与mystat.st_modemystat.st_mode & S_IFMT)按位AND运算意味着仅考虑确定文件类型(常规文件,套接字,块或char设备等)所涉及的位。

用位否定的位掩码(mystat.st_mode & ~S_IFMT)对mystat.st_mode进行按位AND 意味着忽略上述位,只保留那些需要确定文件许可的位(该命令下方的9行)。