我正在学习系统调用,因此正在编写代码以使用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") : …Run Code Online (Sandbox Code Playgroud)