我对在 struct dirent 定义中使用 d_name[256]
和 NAME_MAX
感到困惑。 d_name[256]
是否表示文件名长度最多为 256 个字符?然后它还提到了 NAME_MAX(在底部引用)。所以,我的问题是 NAME_MAX
在这里如何关联以及在哪里可以找到 NAME_MAX
值和定义?
在 man readdir
struct dirent
中定义如下。
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* not an offset; see NOTES */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all filesystem types */
char d_name[256]; /* filename */
};
它还断言
The only fields in the dirent structure that are mandated by POSIX.1 are:
d_name[]
, of unspecified size, with at mostNAME_MAX
characters preceding the terminating null byte ('\0'); and (as an XSI exten‐ sion)d_ino
. The other fields are unstandardized, and not present on all systems; see NOTES below for some further details.
最佳答案
NAME_MAX
在 limits.h
中声明。您还可以使用 pathconf()
或 fpathconf()
来获取每个文件系统的限制。
long max = pathconf(pathname, _PC_NAME_MAX);
由于该结构已硬编码为 256
,因此它实际上无法处理具有较长文件名的文件系统。所以 NAME_MAX
必须最多为 255
(这确实是它在我的 OS X 机器上的值)。
关于c - C d_name[256] 和 NAME_MAX 定义中的 struct dirent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46819975/