简体   繁体   English

C <sys/stat.h> 查找文件类型

[英]C <sys/stat.h> Find file type

I create my own ls command for a project and I use the "st_mode" variable to find the files permissions but I don't use the macros. 我为项目创建了自己的ls命令,并使用“ st_mode”变量来查找文件许可权,但我不使用宏。

Ex: st_mode = 16877 例如:st_mode = 16877

I convert it in octal base: 我将其转换为八进制:

st_mode = 40755 st_mode = 40755

I keep the three last characters and I get my permissions. 我保留了最后三个字符,并获得了我的许可。

But when I try to find the file's type, I try to use the two first characters but they don't really help me... So I would like to know if I can use the two first characters to find the file's type (Link, folder,...). 但是,当我尝试查找文件的类型时,我尝试使用前两个字符,但它们并没有真正帮助我...所以我想知道是否可以使用前两个字符来查找文件的类型(链接,文件夹...)。 And if I can't, what I should use to find the file's type 如果不能,应该使用什么来查找文件的类型

Thanks for your help. 谢谢你的帮助。

Per the POSIX <sys/stat.h> documentation : 根据POSIX <sys / stat.h>文档

The following macros shall be provided to test whether a file is of the specified type. 应提供以下宏以测试文件是否为指定类型。 The value m supplied to the macros is the value of st_mode from a stat structure. 提供给宏的值m是来自stat结构的st_mode的值。 The macro shall evaluate to a non-zero value if the test is true; 如果测试为真,则宏应评估为非零值; 0 if the test is false. 如果测试为假,则为0。

 S_ISBLK(m) Test for a block special file. S_ISCHR(m) Test for a character special file. S_ISDIR(m) Test for a directory. S_ISFIFO(m) Test for a pipe or FIFO special file. S_ISREG(m) Test for a regular file. S_ISLNK(m) Test for a symbolic link. S_ISSOCK(m) Test for a socket. 

yes you can use both method with predefined macros as below, open the man page of stat() system call, it says 是的,您可以将两种方法与预定义的宏一起使用,如下所示,打开stat()系统调用的手册页,它说

S_IFMT     0170000   bit mask for the file type bit fields

So do & of st_mode and S_IFMT , you will get file type 因此, st_modeS_IFMT &会得到文件类型

                struct stat v;
                stat(file,&v);  
                switch (v.st_mode & S_IFMT) // type of file
                {

                        case S_IFBLK:  printf("b");                 break;
                        case S_IFCHR:  printf("c");                 break;
                        case S_IFDIR:  printf("d");                 break;
                        case S_IFIFO:  printf("p");                 break;
                        case S_IFLNK:  printf("l");                 break;
                        case S_IFREG:  printf("-");                 break;
                        case S_IFSOCK: printf("s");                 break;
                        default:       printf("unknown?");          break;
                }

If you don't want to use macros then first find out st_mode value of each type of files & then write the logic. 如果您不想使用宏,则首先找出每种files类型的st_mode值,然后编写逻辑。 for eg st_mode value of regular file is 10664 , last 3 digits(664) for permission , write the binary of 10664 you will come to know that 15th bit is set so do st.mode >> 15 . 例如, regular file st_mode值为10664permission最后3位数字(664),写入二进制10664,您将知道15th位被设置,因此st.mode >> 15也是如此。 Similarly find the st_mode value of different types of file & analyse. 同样,找到不同类型的文件和分析的st_mode值。

if( ( v.st_mode >> 15 & 1) && ( v.st_mode >> 14 & 1) ) 
                printf("Socket File\n"); 
        else if( ( v.st_mode >> 15 & 1) && ( v.st_mode >> 13 & 1) )
                printf("Symbolic Link File\n");
        else if( v.st_mode >> 15 & 1) 
                printf("Regular File\n");
        else if((v.st_mode >> 14 & 1) && (v.st_mode >> 13 & 1) )
                printf("Block Dev File\n");
        else if(v.st_mode >> 14 & 1) 
                printf("Directory File\n");
        else if(v.st_mode >> 13 & 1)
                printf("Char Dev File\n");
        else if(v.st_mode >> 12 & 1)
                printf("FIFO/PIPE File\n");

I hope it help's . 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM