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:
parent
833670e06c
commit
a68c2a9e6e
2
Makefile
2
Makefile
@ -41,8 +41,6 @@ LIBUTFSRC =\
|
||||
|
||||
LIBUTIL = libutil.a
|
||||
LIBUTILSRC =\
|
||||
libutil/agetcwd.c\
|
||||
libutil/apathmax.c\
|
||||
libutil/concat.c\
|
||||
libutil/cp.c\
|
||||
libutil/crypt.c\
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
15
libutil/cp.c
15
libutil/cp.c
@ -27,8 +27,7 @@ int
|
||||
cp(const char *s1, const char *s2, int depth)
|
||||
{
|
||||
FILE *f1, *f2;
|
||||
char *ns1, *ns2;
|
||||
size_t size1, size2;
|
||||
char ns1[PATH_MAX], ns2[PATH_MAX];
|
||||
struct dirent *d;
|
||||
struct stat st;
|
||||
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)
|
||||
eprintf("mkdir %s:", s2);
|
||||
|
||||
apathmax(&ns1, &size1);
|
||||
apathmax(&ns2, &size2);
|
||||
while ((d = readdir(dp))) {
|
||||
if (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
|
||||
r = snprintf(ns1, size1, "%s/%s", s1, d->d_name);
|
||||
if (r >= size1 || r < 0) {
|
||||
r = snprintf(ns1, sizeof(ns1), "%s/%s", s1, d->d_name);
|
||||
if (r >= sizeof(ns1) || r < 0) {
|
||||
eprintf("%s/%s: filename too long\n",
|
||||
s1, d->d_name);
|
||||
}
|
||||
r = snprintf(ns2, size2, "%s/%s", s2, d->d_name);
|
||||
if (r >= size2 || r < 0) {
|
||||
r = snprintf(ns2, sizeof(ns2), "%s/%s", s2, d->d_name);
|
||||
if (r >= sizeof(ns2) || r < 0) {
|
||||
eprintf("%s/%s: filename too long\n",
|
||||
s2, d->d_name);
|
||||
}
|
||||
@ -89,8 +86,6 @@ cp(const char *s1, const char *s2, int depth)
|
||||
}
|
||||
}
|
||||
closedir(dp);
|
||||
free(ns1);
|
||||
free(ns2);
|
||||
goto preserve;
|
||||
}
|
||||
|
||||
|
@ -12,9 +12,9 @@ void
|
||||
enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, int))
|
||||
{
|
||||
struct stat st;
|
||||
char *buf, *dir;
|
||||
char buf[PATH_MAX], *dir;
|
||||
int i, len;
|
||||
size_t size, dlen;
|
||||
size_t dlen;
|
||||
|
||||
if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
|
||||
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];
|
||||
}
|
||||
|
||||
apathmax(&buf, &size);
|
||||
for (i = 0; i < argc; i++) {
|
||||
dlen = strlen(dir);
|
||||
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
|
||||
len = snprintf(buf, size, "%s/%s", dir, basename(argv[i]));
|
||||
if (len < 0 || len >= size) {
|
||||
len = snprintf(buf, sizeof(buf), "%s/%s", dir, basename(argv[i]));
|
||||
if (len < 0 || len >= sizeof(buf)) {
|
||||
eprintf("%s/%s: filename too long\n", dir,
|
||||
basename(argv[i]));
|
||||
}
|
||||
fnck(argv[i], buf, fn, 0);
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
|
6
ls.c
6
ls.c
@ -192,9 +192,10 @@ lsdir(const char *path)
|
||||
struct entry ent, *ents = NULL;
|
||||
struct dirent *d;
|
||||
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)))
|
||||
eprintf("opendir %s:", path);
|
||||
if (chdir(path) < 0)
|
||||
@ -247,7 +248,6 @@ lsdir(const char *path)
|
||||
if (chdir(cwd) < 0)
|
||||
eprintf("chdir %s:", cwd);
|
||||
free(ents);
|
||||
free(cwd);
|
||||
}
|
||||
|
||||
static void
|
||||
|
6
pwd.c
6
pwd.c
@ -3,6 +3,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
@ -29,7 +30,7 @@ usage(void)
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char *cwd;
|
||||
char cwd[PATH_MAX];
|
||||
char mode = 'L';
|
||||
|
||||
ARGBEGIN {
|
||||
@ -41,7 +42,8 @@ main(int argc, char *argv[])
|
||||
usage();
|
||||
} ARGEND;
|
||||
|
||||
cwd = agetcwd();
|
||||
if (!getcwd(cwd, sizeof(cwd)))
|
||||
eprintf("getcwd:");
|
||||
puts((mode == 'L') ? getpwd(cwd) : cwd);
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user