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?
This is a good question. I am doing some simple C programming and encountered this problem. On your particular Linux/Unix, to to /usr/include directory, here are all the header files for your system.
You should see several headers defining PATH_MAX. And you can see that this value is defined differently in different places. Here is a listing from my Ubuntu (I also manually removed some false positive hits from the grep program).
Looks like /linux/limits.h has the largest defined number. I do concern about the portability of this file. So to be safe you might simply defined this MACRO yourself with a different name, say PATHLEN (4080 is long enough for most practical situations). If this is for a large software package, you also have control. This is my opinion.
Its in
linux/limits.h
.#define PATH_MAX 4096 /* # chars in a path name including nul */
PATH_MAX
has some flaws as mentioned in this blog (thanks paulsm4)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 ofPATH_MAX - 1
. Better safe than sorry. (Obviously, you still need to reserve at leastPATH_MAX
bytes of memory to buffer the string.)