commit 33de3bffdb82c54d4e412d74444ce4245f53f927
parent 572f5e926ce5999286fa8992a7c5bb993ef7712e
Author: Connor Lane Smith <cls@lubutu.com>
Date: Sat, 25 Jun 2011 17:26:44 +0100
apathmax
Diffstat:
5 files changed, 28 insertions(+), 10 deletions(-)
diff --git 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
@@ -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
@@ -1,6 +1,4 @@
/* See LICENSE file for copyright and license details. */
-#include <stdio.h>
-#include <stdlib.h>
#include <unistd.h>
#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
@@ -0,0 +1,24 @@
+/* See LICENSE file for copyright and license details. */
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <unistd.h>
+#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
@@ -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]));