Linux路径名和文件名最大长度限制

UNIX标准对路径名和文件名最大长度限制做出了说明,但其上限值在实际应用长过小,Linux在具体实现时提升了该上限,该限制在Linux的 /usr/include/linux/limits.h 中做出了说明,具体如下:

 1 #ifndef _LINUX_LIMITS_H
 2 #define _LINUX_LIMITS_H
 3 
 4 #define NR_OPEN            1024
 5 
 6 #define NGROUPS_MAX    65536    /* supplemental group IDs are available */
 7 #define ARG_MAX       131072    /* # bytes of args + environ for exec() */
 8 #define LINK_MAX         127    /* # links a file may have */
 9 #define MAX_CANON        255    /* size of the canonical input queue */
10 #define MAX_INPUT        255    /* size of the type-ahead buffer */
11 #define NAME_MAX         255    /* # 文件名最大字符数 */
12 #define PATH_MAX        4096    /* # 相对路径名最大字符数 */
13 #define PIPE_BUF        4096    /* # bytes in atomic write to a pipe */
14 #define XATTR_NAME_MAX   255    /* # chars in an extended attribute name */
15 #define XATTR_SIZE_MAX 65536    /* size of an extended attribute value (64k) */
16 #define XATTR_LIST_MAX 65536    /* size of extended attribute namelist (64k) */
17 
18 #define RTSIG_MAX      32
19 
20 #endif 

上述文件内容的第11行和第12行分别说明了文件名和相对路径名的最大长度。需要说明的是,字符指的是ASCII字符,如果是汉字或者其他语言,则需要视编码而定。

上述头文件可以被包含到程序中,然后直接加以引用,这些值也可以使用pathconf( )函数来查询,pathconf( )函数的参数可以参阅该文章中的表格:UNIX环境高级编程 第2章 UNIX标准及实现

一个简单示例demonstration如下:

#include <iostream>
#include <unistd.h>

using namespace std;

int main()
{
    cout << pathconf("/",_PC_NAME_MAX) << endl;return 0;
}
原文地址:https://www.cnblogs.com/pluse/p/7489251.html