This commit is contained in:
Connor Lane Smith 2011-06-25 17:26:44 +01:00
parent 572f5e926c
commit 33de3bffdb
5 changed files with 28 additions and 10 deletions

View File

@ -4,6 +4,7 @@ HDR = text.h util.h
LIB = \ LIB = \
util/afgets.o \ util/afgets.o \
util/agetcwd.o \ util/agetcwd.o \
util/apathmax.o \
util/concat.o \ util/concat.o \
util/enmasse.o \ util/enmasse.o \
util/eprintf.o \ util/eprintf.o \

1
util.h
View File

@ -4,6 +4,7 @@
char *agetcwd(void); char *agetcwd(void);
void enmasse(int, char **, int (*)(const char *, const char *)); void enmasse(int, char **, int (*)(const char *, const char *));
void apathmax(char **, long *);
void eprintf(const char *, ...); void eprintf(const char *, ...);
void enprintf(int, const char *, ...); void enprintf(int, const char *, ...);
long estrtol(const char *, int); long estrtol(const char *, int);

View File

@ -1,6 +1,4 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "../util.h" #include "../util.h"
@ -10,10 +8,7 @@ agetcwd(void)
char *buf; char *buf;
long size; long size;
if((size = pathconf(".", _PC_PATH_MAX)) == -1) apathmax(&buf, &size);
size = BUFSIZ;
if(!(buf = malloc(size)))
eprintf("malloc:");
if(!getcwd(buf, size)) if(!getcwd(buf, size))
eprintf("getcwd:"); eprintf("getcwd:");
return buf; return buf;

24
util/apathmax.c Normal file
View File

@ -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:");
}

View File

@ -24,10 +24,7 @@ enmasse(int argc, char **argv, int (*fn)(const char *, const char *))
else else
dir = (argc == 1) ? "." : argv[--argc]; dir = (argc == 1) ? "." : argv[--argc];
if((size = pathconf(dir, _PC_PATH_MAX)) == -1) apathmax(&buf, &size);
size = BUFSIZ;
if(!(buf = malloc(size)))
eprintf("malloc:");
for(i = 0; i < argc; i++) { for(i = 0; i < argc; i++) {
if(snprintf(buf, size, "%s/%s", dir, basename(argv[i])) > size) if(snprintf(buf, size, "%s/%s", dir, basename(argv[i])) > size)
eprintf("%s/%s: filename too long\n", dir, basename(argv[i])); eprintf("%s/%s: filename too long\n", dir, basename(argv[i]));