Where is PATH_MAX defined in Linux?

CLinuxGccIncludeClang

C Problem Overview


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

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:

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.

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

C Solutions


Solution 1 - C

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

#include <linux/limits.h>

char current_path[PATH_MAX];

PATH_MAX has some flaws as mentioned in this blog (thanks paulsm4)

Solution 2 - C

Be aware, that it is still unclear if PATH_MAX defines 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_MAX bytes of memory to buffer the string.)

Solution 3 - C

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

Solution 4 - C

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.

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).

./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.

Solution 5 - C

PATH_MAX is a system limit. There are three categories about system limits exists in POSIX environment. One of these categories is Pathname Variable Values. System limits which are depend on the file system falls into this category. PATHMAX is also pathname variable value. (so this value can change from file system to file system.) So, PATHNAME limit can be obtained with pathconf()/fpathconf() POSIX functions. This way is portable way of getting PATHNAME limit of spesific file system. Example code is like below:

long
get_pathmax(void)
{
  long pathmax = -1;

  errno = 0;
  pathmax = pathconf("/", _PC_PATH_MAX);
  if (-1 == pathmax)
  {
    if (0 == errno)
    {
#define PATHMAX_INFINITE_GUESS 4096
      pathmax = PATHMAX_INFINITE_GUESS;
    }
    else
    {
      fprintf (stderr, "pathconf() FAILED, %d, %s\n", errno, strerror(errno));
    }
  }

  return pathmax;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhazizView Question on Stackoverflow
Solution 1 - CShiplu MokaddimView Answer on Stackoverflow
Solution 2 - CKumashiroView Answer on Stackoverflow
Solution 3 - CemersionView Answer on Stackoverflow
Solution 4 - CKemin ZhouView Answer on Stackoverflow
Solution 5 - Cuser3104363View Answer on Stackoverflow