Linux 中 PATH_MAX 在哪里定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9449241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:49:20  来源:igfitidea点击:

Where is PATH_MAX defined in Linux?

clinuxgccincludeclang

提问by haziz

Which header file should I invoke with #includeto be able to use PATH_MAX as an int for sizing a string?

我应该调用哪个头文件#include才能使用 PATH_MAX 作为 int 来调整字符串大小?

I want to be able to declare:

我希望能够声明:

char *current_path[PATH_MAX];

But when I do so my compiler (Clang/LLVM on Linux) issues the following error:

但是当我这样做时,我的编译器(Linux 上的 Clang/LLVM)会发出以下错误:

recursive_find6.c:29:20: error: use of undeclared identifier 'PATH_MAX'
char *current_path[PATH_MAX];
                   ^

I tried doing a google search but still no luck.

我尝试进行谷歌搜索,但仍然没有运气。

#include <limits.h>Does NOT fix the problem/error.

#include <limits.h>不解决问题/错误。

Am I also correct that the value of PATH_MAX is an int?

我是否也正确认为 PATH_MAX 的值是一个整数?

采纳答案by Shiplu Mokaddim

Its in linux/limits.h.
#define PATH_MAX 4096 /* # chars in a path name including nul */

它在linux/limits.h.
#define PATH_MAX 4096 /* # chars in a path name including nul */

#include <linux/limits.h>

char current_path[PATH_MAX];

PATH_MAXhas some flaws as mentioned in this blog(thanks paulsm4)

PATH_MAX本博客中提到的一些缺陷(感谢 paulsm4)

回答by Kumashiro

Be aware, that it is still unclear if PATH_MAXdefines a maximum length with or without a trailing nul byte. It may be one or the other on different operating systems. If you can't or don't want to check which case it is during compilation, it's safer to force artificial limit of PATH_MAX - 1. Better safe than sorry. (Obviously, you still need to reserve at least PATH_MAXbytes of memory to buffer the string.)

请注意,目前还不清楚是否PATH_MAX定义了带有或不带有尾随空字节的最大长度。在不同的操作系统上可能是其中之一。如果您不能或不想在编译期间检查它是哪种情况,则强制人为限制PATH_MAX - 1. 安全总比后悔好。(显然,您仍然需要至少保留PATH_MAX字节的内存来缓冲字符串。)

回答by Kemin Zhou

When doing simple C programming, I encountered the same challenge. On your particular Linux system, /usr/include directory contain many , here header files specific to a Linux OS.

在做简单的 C 编程时,我遇到了同样的挑战。在您的特定 Linux 系统上, /usr/include 目录包含许多特定于 Linux 操作系统的头文件。

find . -name "*.h" | xargs grep PATH_MAX 

You should see several headers defining PATH_MAX; unfortunately this value was defined differently in different headers. Here is a listing from my Ubuntu (I also manually removed some false positive hits from the grep program).

您应该会看到几个定义 PATH_MAX 的头文件;不幸的是,这个值在不同的标题中定义不同。这是我的 Ubuntu 的列表(我还手动从 grep 程序中删除了一些误报)。

./x86_64-linux-gnu/bits/posix1_lim.h:#define _POSIX_PATH_MAX      256
./X11/InitialI.h:#ifndef PATH_MAX
./X11/InitialI.h:#define PATH_MAX 512
./X11/InitialI.h:#ifndef PATH_MAX
./X11/InitialI.h:#define PATH_MAX MAXPATHLEN
./X11/InitialI.h:#define PATH_MAX 1024
./X11/Xos.h:#  define PATH_MAX 4096
./X11/Xwindows.h:#if defined(WIN32) && (!defined(PATH_MAX) || PATH_MAX < 1024)
./X11/Xwindows.h:# undef PATH_MAX
./X11/Xwindows.h:# define PATH_MAX 1024
./X11/Xosdefs.h:#  ifndef PATH_MAX
./X11/Xosdefs.h:#   define PATH_MAX 4096
./X11/Xosdefs.h:#  ifndef PATH_MAX
./X11/Xosdefs.h:#   define PATH_MAX 1024
./X11/extensions/XKBsrv.h:#define   PATH_MAX MAXPATHLEN
./X11/extensions/XKBsrv.h:#define   PATH_MAX 1024
./python2.7/osdefs.h:#ifndef PATH_MAX
./python2.7/osdefs.h:#define PATH_MAX MAXPATHLEN
./python2.7/osdefs.h:#if defined(PATH_MAX) && PATH_MAX > 1024
./python2.7/osdefs.h:#define MAXPATHLEN PATH_MAX
./linux/limits.h:#define PATH_MAX        4096   /* # chars in a path name including nul */
./linux/btrfs.h:#define BTRFS_INO_LOOKUP_PATH_MAX 4080
./linux/un.h:#define UNIX_PATH_MAX  108

The header /linux/limits.h had the largest number and should be the most authentic one to include. Alternative strategy is to define your own with a different name, say PATHLEN (4080 is long enough for most practical situations). My main point is to learn to use find to look for answers to your question.

头文件 /linux/limits.h 的数量最多,应该是最真实的。另一种策略是使用不同的名称定义您自己的名称,例如 PATHLEN(对于大多数实际情况而言,4080 足够长)。我的主要观点是学习使用 find 来寻找问题的答案。

回答by emersion

The portable way to do it is:

这样做的便携式方法是:

#define _POSIX_C_SOURCE 1
#include <limits.h>

Spec: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html

规范:https: //pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html