c - 什么是 `S_ISREG()` ,它有什么作用?

标签 c linux macos unix operating-system

我在检索文件属性的 C 程序中遇到宏 S_ISREG()。不幸的是,网上没有关于这个宏的任何基本信息。关于它有一些更高级的讨论,但它们超出了我正在寻找的范围。

什么是 S_ISREG(),它有什么作用?在检索文件属性的程序上下文中,它的用途是什么,它究竟做了什么?

谢谢。

最佳答案

S_ISREG() 是一个宏,用于解释从系统调用 stat() 返回的 stat-struct 中的值。如果参数(struct stat 中的 st_mode 成员)是一个常规文件,它的计算结果为真。

请参阅 man statman fstatman inode ( link to inode man page ) 了解更多详细信息。这是手册页的相关部分:

   Because tests of the above form are common, additional macros are defined by POSIX to allow the test of the file type in st_mode to be written more concisely:

       S_ISREG(m)  is it a regular file?

       S_ISDIR(m)  directory?

       S_ISCHR(m)  character device?

       S_ISBLK(m)  block device?

       S_ISFIFO(m) FIFO (named pipe)?

       S_ISLNK(m)  symbolic link?  (Not in POSIX.1-1996.)

       S_ISSOCK(m) socket?  (Not in POSIX.1-1996.)

   The preceding code snippet could thus be rewritten as:

       stat(pathname, &sb);
       if (S_ISREG(sb.st_mode)) {
           /* Handle regular file */
       }

关于c - 什么是 `S_ISREG()` ,它有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40163270/

相关文章:

linux - 将 Octave 4.0/4.2.1 中的默认编辑器设置为 Vim

java - 在 java 脚本中执行 shell 脚本或从浏览器触发扫描器

swift - Swift Mac 和 CloudKit 上的问题,应用程序运行后立即崩溃

C:大整数的表示

c - 使用 gcc 理解共享库

linux - 对 grep 结果调用 sed - Linux shell 脚本/文本处理

macos - 警告 : Your version of git is 1. 9.3。存在严重的安全漏洞

macos - 如何在 32 位或 64 位环境中以编程方式启动程序?

c++ - 幂运算符C语言程序

c - 根据 C 规范,中断是信号吗?