diff --git a/Makefile b/Makefile index 708a0fd..5e6386f 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ HDR = text.h util.h LIB = \ util/afgets.o \ util/agetcwd.o \ + util/apathmax.o \ util/concat.o \ util/enmasse.o \ util/eprintf.o \ diff --git a/util.h b/util.h index 15cde8a..728393a 100644 --- a/util.h +++ b/util.h @@ -4,6 +4,7 @@ char *agetcwd(void); void enmasse(int, char **, int (*)(const char *, const char *)); +void apathmax(char **, long *); void eprintf(const char *, ...); void enprintf(int, const char *, ...); long estrtol(const char *, int); diff --git a/util/agetcwd.c b/util/agetcwd.c index 832a7a7..4d34e94 100644 --- a/util/agetcwd.c +++ b/util/agetcwd.c @@ -1,6 +1,4 @@ /* See LICENSE file for copyright and license details. */ -#include -#include #include #include "../util.h" @@ -10,10 +8,7 @@ agetcwd(void) char *buf; long size; - if((size = pathconf(".", _PC_PATH_MAX)) == -1) - size = BUFSIZ; - if(!(buf = malloc(size))) - eprintf("malloc:"); + apathmax(&buf, &size); if(!getcwd(buf, size)) eprintf("getcwd:"); return buf; diff --git a/util/apathmax.c b/util/apathmax.c new file mode 100644 index 0000000..1f62dba --- /dev/null +++ b/util/apathmax.c @@ -0,0 +1,24 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include "../util.h" + +void +apathmax(char **p, long *size) +{ +#ifdef PATH_MAX + *size = PATH_MAX; +#else + errno = 0; + if((*size = pathconf(".", _PC_PATH_MAX)) == -1) { + if(errno == 0) + *size = BUFSIZ; + else + eprintf("pathconf:"); + } +#endif + if(!(*p = malloc(*size))) + eprintf("malloc:"); +} diff --git a/util/enmasse.c b/util/enmasse.c index dbbbdbb..adda9f9 100644 --- a/util/enmasse.c +++ b/util/enmasse.c @@ -24,10 +24,7 @@ enmasse(int argc, char **argv, int (*fn)(const char *, const char *)) else dir = (argc == 1) ? "." : argv[--argc]; - if((size = pathconf(dir, _PC_PATH_MAX)) == -1) - size = BUFSIZ; - if(!(buf = malloc(size))) - eprintf("malloc:"); + apathmax(&buf, &size); for(i = 0; i < argc; i++) { if(snprintf(buf, size, "%s/%s", dir, basename(argv[i])) > size) eprintf("%s/%s: filename too long\n", dir, basename(argv[i]));