apathmax
This commit is contained in:
parent
572f5e926c
commit
33de3bffdb
1
Makefile
1
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 \
|
||||
|
1
util.h
1
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);
|
||||
|
@ -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;
|
||||
|
24
util/apathmax.c
Normal file
24
util/apathmax.c
Normal 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:");
|
||||
}
|
@ -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]));
|
||||
|
Loading…
Reference in New Issue
Block a user