UNIX系统编程中的S_IFMT是什么意思?
what is S_IFMT in UNIX system programming?
我正在学习系统调用,因此正在编写代码来使用 C 语言实现 ls。代码有效,但我无法理解
的工作原理
val=(mystat.st_mode & ~S_IFMT)
在下面给出的代码中?我理解其余的代码。
#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);
}
S_IFMT
是文件类型的位掩码(参见 man stat
)
直接与mystat.st_mode
(mystat.st_mode & S_IFMT
)按位与运算意味着只考虑确定文件类型(常规文件、套接字、块或字符设备等)所涉及的位
对 mystat.st_mode 与按位取反位掩码 (mystat.st_mode & ~S_IFMT
) 进行按位与意味着忽略上面解释的位,只保留需要确定文件权限的位(下面的 9 行)命令)。
我正在学习系统调用,因此正在编写代码来使用 C 语言实现 ls。代码有效,但我无法理解
的工作原理val=(mystat.st_mode & ~S_IFMT)
在下面给出的代码中?我理解其余的代码。
#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);
}
S_IFMT
是文件类型的位掩码(参见 man stat
)
直接与mystat.st_mode
(mystat.st_mode & S_IFMT
)按位与运算意味着只考虑确定文件类型(常规文件、套接字、块或字符设备等)所涉及的位
对 mystat.st_mode 与按位取反位掩码 (mystat.st_mode & ~S_IFMT
) 进行按位与意味着忽略上面解释的位,只保留需要确定文件权限的位(下面的 9 行)命令)。