pathalloc.c (807B)
1 #include <errno.h> 2 #include <limits.h> 3 #include "ourhdr.h" 4 5 #ifdef PATH_MAX 6 static int pathmax = PATH_MAX; 7 #else 8 static int pathmax = 0; 9 #endif 10 11 #define PATH_MAX_GUESS 1024 /* if PATH_MAX is indeterminate */ 12 /* we're not guaranteed this is adequate */ 13 char * 14 path_alloc(int *size) 15 /* also return allocated size, if nonnull */ 16 { 17 char *ptr; 18 19 if (pathmax == 0) { /* first time through */ 20 errno = 0; 21 if ( (pathmax = pathconf("/", _PC_PATH_MAX)) < 0) { 22 if (errno == 0) 23 pathmax = PATH_MAX_GUESS; /* it's indeterminate */ 24 else 25 err_sys("pathconf error for _PC_PATH_MAX"); 26 } else 27 pathmax++; /* add one since it's relative to root */ 28 } 29 30 if ( (ptr = malloc(pathmax + 1)) == NULL) 31 err_sys("malloc error for pathname"); 32 33 if (size != NULL) 34 *size = pathmax + 1; 35 return(ptr); 36 }