ptg10805159
Section 2.5 Limits 51
errno = 0;
if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
if (errno == 0)
pathmax = PATH_MAX_GUESS; /* it’s indeterminate */
else
err_sys("pathconf error for _PC_PATH_MAX");
}else {
pathmax++; /* add one since it’s relative to root */
}
}
/*
*Before POSIX.1-2001, we aren’t guaranteed that PATH_MAX includes
*the terminating null byte. Same goes for XPG3.
*/
if ((posix_version < 200112L) && (xsi_version < 4))
size = pathmax + 1;
else
size = pathmax;
if ((ptr = malloc(size)) == NULL)
err_sys("malloc error for pathname");
if (sizep != NULL)
*sizep = size;
return(ptr);
}
Figure 2.16Dynamically allocate space for a pathname
Maximum Number of Open Files
Acommon sequence of code in a daemon process — a process that runs in the
background, not connected to a terminal—isone that closes all open files. Some
programs have the following code sequence, assuming the constantNOFILE was
defined in the<sys/param.h>header:
#include <sys/param.h>
for (i = 0; i < NOFILE; i++)
close(i);
Other programs use the constant_NFILEthat some versions of<stdio.h>provide as
the upper limit. Some hardcode the upper limit as 20. However,none of these
approaches is portable.
We would hope to use the POSIX.1 value OPEN_MAXto determine this value
portably,but if the value is indeterminate, we still have a problem. If we wrote the
following code and ifOPEN_MAXwas indeterminate, the loop would never execute,
sincesysconfwould return−1: