views:

40

answers:

1

In limits.h, and in various places in the POSIX manpages, there are references to PATH_MAX and NAME_MAX. How do these relate to one another? Where is the official documentation for them? How can I obtain them at run time, and (where relevant) compile time for the C, Python, and GNU (shell) environments?

+1  A: 

PATH_MAX is the maximum length of a filesystem path. NAME_MAX is the maximum length of a filename (in a particular spot). So, /foo/bar is restricted by PATH_MAX, and only the bar portion has its length limited by NAME_MAX.

You can get these at run time via pathconf, as _PC_PATH_MAX and _PC_NAME_MAX, although standard practice is generally just to use the static macros at compile time. I suppose it would be better to use the run-time option because you could potentially support longer values that way, but I'm not sure what (if any) systems actually provide a return from pathconf which is greater than the value of the POSIX_FOO_MAX values.

Borealid
Can you comment on whether or not they're available in Python, and where?
Matt Joiner
@Matt Joiner: Your question only really makes sense for C; there is no "compile time" for Python or shell scripting. If you are using a scripting language, these variables are also unlikely to be useful to you - just use an indefinite-length string to manipulate your paths. Also, these are system-specific variables, and a Python script should ideally be portable, so you may not want to make use of them and instead just check for "path too long" errors when creating/accessing a file.
Borealid
I'm writing a filesystem in Python, so they are in fact of great interest. I'm calling `os.fpathconf` on my device file as a best guess for `PATH_MAX`, and `NAME_MAX` is a property of my implementation returned through `struct statvfs`. Despite missing the Python tips this is a good answer.
Matt Joiner