Is there a WinAPI MAX_PATH equivalent on linux / unix?

If I want to allocate a char array (in C) that is guaranteed to be large enough to hold any valid absolute path + file name, as far as it should be.

In Win32, MAX_PATH is defined. What is equivalent for Unix / linux?

+55
c ++ c linux unix posix
May 7, '09 at 7:16
source share
7 answers

There is PATH_MAX , but this is a bit problematic. From the error section of the realpath (3) man section:

The standard version of this POSIX.1-2001 function has been disrupted because it is not possible to determine the appropriate size for the output buffer permitted by path_. According to POSIX.1-2001, the buffer size is PATH_MAX , but PATH_MAX does not have to be a constant and can be obtained using pathconf (3) . Also, the pathconf (3) request doesn’t really help, because, on the one hand, POSIX warns that the result of pathconf (3) can be huge and unsuitable for mallocing memory, and on the other hand, pathconf (3) can return -1 to indicate -1 that PATH_MAX is not limited.

+48
May 7 '09 at 7:24
source share

The rest of the answers still seem correct with respect to the * nix side, but I will add a warning about this on Windows.

In accordance with the documentation you are accused (by omission).

MAX_PATH indeed defined and may even apply to files stored on FAT or FAT32. However, any path name may be prefixed with \\?\ To tell the Windows API to ignore MAX_PATH and allow the file system driver itself. After this, the definitions become fuzzy.

Add to the confusion the fact that the path names are actually Unicode (well, UTS-16) and that when using the "ANSI" API, the conversion to and from the Unicode internal name depends on many factors, including the current code page, and you have recipe for confusion.

A good description of the rules for Windows is MSDN . The rules are much more complicated than here.

Edit: I changed \\.\ To \\?\ In the above KitsuneYMG comment example.

Windows paths and namespaces are complex. Some may even argue that they are too complex. One source of complexity is that the Win32 (and now Win64) API is a subsystem that sits on top of its own Windows NT system.

The prefix-free path is compatible with a wide range of Windows platforms. If it is limited to 7-bit ASCII characters, then it is compatible with 16-bit DOS from version 2.0 or so (whenever subdirectories that could have been in DOS 3, but in DOS 1.0 there were only root directories, and \ didn't really matter).

The prefix \\?\ MAX_PATH path name balance to be transferred verbatim to the corresponding file system driver, which gives the effect of dropping the restriction on MAX_PATH characters. If the long path name is also included in the network share, you can use the UNC extended name for it with the prefix \\?\UNC\server\share\ instead of the usual UNC name \\server\share\ . Using this prefix limits portability for Win32 and later Windows platforms, but if you don't need 16-bit Windows support on legacy hardware, this is not a big problem.

The prefix \\.\ Is another animal. It allows you to access device objects outside of a set of specially named devices that are automatically mapped to Windows as special file names in each file folder. These special names include CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, ​​COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8 and LPT9. Note that all of these names are special regardless of whether the extension is used or in any combination of upper or lower case. But it is possible that you have 10 or more COM ports installed. This happens quickly if you play with USB modems or USB serial adapters, as each unique USB serial port will be given a separate COMn name. If you need to access the 50th serial port, you can only do this with the name \\.\COM50 , because COM50 is not a special name, for example COM1.

The MSDN page above was eligible for a difference, I just typed the wrong prefix in my original answer.

+40
May 08 '09 at 1:30
source share

Well, at least on Linux there is:

  • PATH_MAX (defined in limits.h )

  • FILENAME_MAX (defined in stdio.h )

both of them are installed on 4096 on my system (x86 Linux).

Update:: Some information from the glibc manual on this subject

Each of the following macros is defined within .h only if the system has a fixed uniform limit for the parameter in question. If the system allows different file systems or files to have different limits, the macro is undefined; use pathconf or fpathconf to find out the restriction that applies to a specific file

+18
May 7, '09 at 7:19
source share

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 the actual limit is not set. In this case, its value is usually a very large number. This always happens on the GNU system.

Usage note: do not use FILENAME_MAX as the size of the array in which to save the file name! You cannot make an array so big! Use dynamic allocation.

+6
Jun 28 '11 at 17:20
source share

You can use pathconf() to determine at runtime, but also the PATH_MAX preprocessor defines in <limits.h> .

+5
May 7 '09 at 7:20
source share

You can use the realpath function to allocate a buffer large enough for a specific path. If you pass it a null pointer as the second argument, it will allocate a buffer large enough for the path. The user page probably explains this better than I can:

realpath () expands all symbolic links and resolves links to /./,/../ and optional '/' characters in a string with a null character by name to create a canonical absolute path name. The resulting path name is stored as a zero-terminated string, up to the maximum number of PATH_MAX bytes, in the buffer pointed to by allowed_path. The resulting path will not have a symbolic link, /. / Or /../.

If resol_path is specified as NULL, then realpath () uses malloc (3) to allocate the buffer to the PATH_MAX bytes to store the allowed path and returns a pointer to this buffer. The caller must free this buffer using the free (3).

http://linux.die.net/man/3/realpath

+2
May 13 '14 at 19:12
source share

limits.h

 /* * File system limits * * NOTE: Apparently the actual size of PATH_MAX is 260, but a space is * required for the NUL. TODO: Test? * NOTE: PATH_MAX is the POSIX equivalent for Microsoft MAX_PATH; the two * are semantically identical, with a limit of 259 characters for the * path name, plus one for a terminating NUL, for a total of 260. */ #define PATH_MAX 260 

minwindef.h

 #define MAX_PATH 260 
0
Dec 20 '18 at 16:07
source share



All Articles