2017-10-18 130 views
0

在struct dirent定義中,我對使用d_name[256]NAME_MAX感到困惑。 d_name[256]是否意味着文件名長度可以是最多256個字符?然後它也提到了NAME_MAX(在底部引用)。那麼,我的問題是如何與NAME_MAX相關,我在哪裏可以找到NAME_MAX的價值和定義?C d_name [256]和NAME_MAX定義中的struct dirent

man readdirstruct 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 */ 
}; 

它還聲稱,

在的dirent結構的唯一字段由POSIX.1 規定是:d_name[],未指定大小,具有至多NAME_MAX字符 終止空前述字節('\ 0');和(作爲XSI exten- sion)d_ino。其他領域沒有標準化,並且在所有系統上不存在 ;有關更多詳細信息,請參閱下面的註釋。

回答

2

NAME_MAXlimits.h中聲明。您也可以使用pathconf()fpathconf()來獲取每個文件系統的限制。

long max = pathconf(pathname, _PC_NAME_MAX); 

由於結構具有這種硬編碼爲256,它不能實際上長文件名的文件系統處理。所以NAME_MAX最多隻能是255(這的確是它在我的OS X機器上的價值)。

+0

NAME_MAX不包含終止符,只是strlen,所以它與d_name匹配。 –