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

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

#ifndef _LINUX_LIMITS_H
#define _LINUX_LIMITS_H

#define NR_OPEN            1024

#define NGROUPS_MAX    65536    /* supplemental group IDs are available */
#define ARG_MAX      131072    /* # bytes of args + environ for exec() */
#define LINK_MAX        127    /* # links a file may have */
#define MAX_CANON        255    /* size of the canonical input queue */
#define MAX_INPUT        255    /* size of the type-ahead buffer */
#define NAME_MAX        255    /* # 文件名最大字符数 */
#define PATH_MAX        4096    /* # 相对路径名最大字符数 */
#define PIPE_BUF        4096    /* # bytes in atomic write to a pipe */
#define XATTR_NAME_MAX  255    /* # chars in an extended attribute name */
#define XATTR_SIZE_MAX 65536    /* size of an extended attribute value (64k) */
#define XATTR_LIST_MAX 65536    /* size of extended attribute namelist (64k) */

#define RTSIG_MAX      32

#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;
}

猜你喜欢

转载自www.linuxidc.com/Linux/2017-10/147311.htm