If I want to allocate a char array (in C) that is guaranteed to be large enough to hold any valid absolute path+filename, how big does it need to be.
On Win32, there is the MAX_PATH define. What is the equivalent for Unix/linux?
If I want to allocate a char array (in C) that is guaranteed to be large enough to hold any valid absolute path+filename, how big does it need to be.
On Win32, there is the MAX_PATH define. What is the equivalent for Unix/linux?
Well, on Linux at least, there is:
PATH_MAX
(defined inlimits.h
)FILENAME_MAX
(defined instdio.h
)both of these are set to
4096
on my system (x86 Linux).Update: : Some info from the glibc manual on this
You can use
pathconf()
to figure out at run-time, but there's also a PATH_MAX preprocessor define in<limits.h>
.limits.h
minwindef.h
FILENAME_MAX is part of the ISO C standard, it works on UNIX and Windows. However, the GNU C library documentation contains the following warnings:
"Unlike PATH_MAX, this macro is defined even if there is no actual limit imposed. In such a case, its value is typically a very large number. This is always the case on the GNU system.
Usage Note: Don't use FILENAME_MAX as the size of an array in which to store a file name! You can't possibly make an array that big! Use dynamic allocation."
You can use the
realpath
function to allocate a buffer big enough for a specific path. If you pass it a null pointer as the 2nd argument it will allocate a buffer big enough for the path. The man page probably explains it better than I can:http://linux.die.net/man/3/realpath
There is a
PATH_MAX
, but it is a bit problematic. From the bugs section of the realpath(3) man page: