Remove apathmax() and implicitly agetcwd()

pathconf() is just an insane interface to use. All sane operating-
systems set sane values for PATH_MAX. Due to the by-runtime-nature of
pathconf(), it actually weakens the programs depending on its values.

Given over 3 years it has still not been possible to implement a sane
and easy to use apathmax()-utility-function, and after discussing this
on IRC, we'll dump this garbage.

We are careful enough not to overflow PATH_MAX and even if, any user
is able to set another limit in config.mk if he so desires.
This commit is contained in:
FRIGN 2015-03-18 15:20:35 +01:00
parent 833670e06c
commit a68c2a9e6e
8 changed files with 17 additions and 67 deletions

View File

@ -41,8 +41,6 @@ LIBUTFSRC =\
LIBUTIL = libutil.a LIBUTIL = libutil.a
LIBUTILSRC =\ LIBUTILSRC =\
libutil/agetcwd.c\
libutil/apathmax.c\
libutil/concat.c\ libutil/concat.c\
libutil/cp.c\ libutil/cp.c\
libutil/crypt.c\ libutil/crypt.c\

View File

@ -1,18 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <unistd.h>
#include "../util.h"
char *
agetcwd(void)
{
char *buf;
size_t size;
apathmax(&buf, &size);
if (!getcwd(buf, size))
eprintf("getcwd:");
return buf;
}

View File

@ -1,22 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include "../util.h"
void
apathmax(char **p, size_t *size)
{
errno = 0;
if ((*size = pathconf("/", _PC_PATH_MAX)) < 0) {
if (errno == 0) {
*size = PATH_MAX;
} else {
eprintf("pathconf:");
}
} else {
(*size)++;
}
*p = emalloc(*size);
}

View File

@ -27,8 +27,7 @@ int
cp(const char *s1, const char *s2, int depth) cp(const char *s1, const char *s2, int depth)
{ {
FILE *f1, *f2; FILE *f1, *f2;
char *ns1, *ns2; char ns1[PATH_MAX], ns2[PATH_MAX];
size_t size1, size2;
struct dirent *d; struct dirent *d;
struct stat st; struct stat st;
struct utimbuf ut; struct utimbuf ut;
@ -71,17 +70,15 @@ cp(const char *s1, const char *s2, int depth)
if (mkdir(s2, st.st_mode) < 0 && errno != EEXIST) if (mkdir(s2, st.st_mode) < 0 && errno != EEXIST)
eprintf("mkdir %s:", s2); eprintf("mkdir %s:", s2);
apathmax(&ns1, &size1);
apathmax(&ns2, &size2);
while ((d = readdir(dp))) { while ((d = readdir(dp))) {
if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) { if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
r = snprintf(ns1, size1, "%s/%s", s1, d->d_name); r = snprintf(ns1, sizeof(ns1), "%s/%s", s1, d->d_name);
if (r >= size1 || r < 0) { if (r >= sizeof(ns1) || r < 0) {
eprintf("%s/%s: filename too long\n", eprintf("%s/%s: filename too long\n",
s1, d->d_name); s1, d->d_name);
} }
r = snprintf(ns2, size2, "%s/%s", s2, d->d_name); r = snprintf(ns2, sizeof(ns2), "%s/%s", s2, d->d_name);
if (r >= size2 || r < 0) { if (r >= sizeof(ns2) || r < 0) {
eprintf("%s/%s: filename too long\n", eprintf("%s/%s: filename too long\n",
s2, d->d_name); s2, d->d_name);
} }
@ -89,8 +86,6 @@ cp(const char *s1, const char *s2, int depth)
} }
} }
closedir(dp); closedir(dp);
free(ns1);
free(ns2);
goto preserve; goto preserve;
} }

View File

@ -12,9 +12,9 @@ void
enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, int)) enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, int))
{ {
struct stat st; struct stat st;
char *buf, *dir; char buf[PATH_MAX], *dir;
int i, len; int i, len;
size_t size, dlen; size_t dlen;
if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) { if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
fnck(argv[0], argv[1], fn, 0); fnck(argv[0], argv[1], fn, 0);
@ -23,18 +23,16 @@ enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, int))
dir = (argc == 1) ? "." : argv[--argc]; dir = (argc == 1) ? "." : argv[--argc];
} }
apathmax(&buf, &size);
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
dlen = strlen(dir); dlen = strlen(dir);
if (dlen > 0 && dir[dlen - 1] == '/') if (dlen > 0 && dir[dlen - 1] == '/')
len = snprintf(buf, size, "%s%s", dir, basename(argv[i])); len = snprintf(buf, sizeof(buf), "%s%s", dir, basename(argv[i]));
else else
len = snprintf(buf, size, "%s/%s", dir, basename(argv[i])); len = snprintf(buf, sizeof(buf), "%s/%s", dir, basename(argv[i]));
if (len < 0 || len >= size) { if (len < 0 || len >= sizeof(buf)) {
eprintf("%s/%s: filename too long\n", dir, eprintf("%s/%s: filename too long\n", dir,
basename(argv[i])); basename(argv[i]));
} }
fnck(argv[i], buf, fn, 0); fnck(argv[i], buf, fn, 0);
} }
free(buf);
} }

6
ls.c
View File

@ -192,9 +192,10 @@ lsdir(const char *path)
struct entry ent, *ents = NULL; struct entry ent, *ents = NULL;
struct dirent *d; struct dirent *d;
size_t i, n = 0, len; size_t i, n = 0, len;
char *cwd, *p, *q, *name; char cwd[PATH_MAX], *p, *q, *name;
cwd = agetcwd(); if (!getcwd(cwd, sizeof(cwd)))
eprintf("getcwd:");
if (!(dp = opendir(path))) if (!(dp = opendir(path)))
eprintf("opendir %s:", path); eprintf("opendir %s:", path);
if (chdir(path) < 0) if (chdir(path) < 0)
@ -247,7 +248,6 @@ lsdir(const char *path)
if (chdir(cwd) < 0) if (chdir(cwd) < 0)
eprintf("chdir %s:", cwd); eprintf("chdir %s:", cwd);
free(ents); free(ents);
free(cwd);
} }
static void static void

6
pwd.c
View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "util.h" #include "util.h"
@ -29,7 +30,7 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *cwd; char cwd[PATH_MAX];
char mode = 'L'; char mode = 'L';
ARGBEGIN { ARGBEGIN {
@ -41,7 +42,8 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
cwd = agetcwd(); if (!getcwd(cwd, sizeof(cwd)))
eprintf("getcwd:");
puts((mode == 'L') ? getpwd(cwd) : cwd); puts((mode == 'L') ? getpwd(cwd) : cwd);
return 0; return 0;

3
util.h
View File

@ -20,9 +20,6 @@
extern char *argv0; extern char *argv0;
char *agetcwd(void);
void apathmax(char **, size_t *);
void *ecalloc(size_t, size_t); void *ecalloc(size_t, size_t);
void *emalloc(size_t); void *emalloc(size_t);
void *erealloc(void *, size_t); void *erealloc(void *, size_t);