"Hugo Smith" <23*2*1*
2@s*u*e*t*u*a*e*u*a*> wrote:
> Hey Chris
> What's the maximum number of:
> - files in a directory
> - characters in a file/directory name
> - directories specified in the command line
> Cheers
Hi Hugo,
I strongly suspect that you're asking for this information because you want to define fixed-sized arrays. However, that's the wrong approach for this project.
Firstly, there are no guaranteed upper-bounds on these values (but one), and even trying to determine them could only be done in a very non-portable way.
For example, while one system (even one version of the Linux kernel) may place an upper bound on the *total* size of a directory, the number of directory entries that can fit in that space will depend on what's being stored - lots of entries with very short names, or very few entries with very long names.
Similarly, there is a limit on the combined maximum size of all command-line arguments and environment variables, which sits at the top of the stack (and from memory it's 2MB, but don't quote me). So, again, you can have many very short command-line arguments, or far fewer very long arguments, and the number of command-line arguments will be limited by the amount of memory already storing the environment variables.
The kernel enforces and checks against these limits - for example, when calling
exec()
to load a new program, one of the possible errors is:
E2BIG The total number of bytes in the environment (envp) and argument list (argv) is too large.
But this 2nd project is different than the 1st - we don't wish to use fixed-sized arrays for everything, but strongly prefer (need) to employ dynamically allocated memory to store exactly what we need - if the command-line provides 4 directory names, we allocate memory for exactly 4 'things' to store those names, and further use
strdup()
to allocate exactly the needed memory for each name; if a directory has 83 directory entries, then we allocate memory for exactly 83 'things' to store the required information.
______
But there is one answer - the maximum size of any directory entry name (file or directory) is named
MAXPATHLEN
and is defined in <sys/param.h> on both Linux and macOS.