commit c61991306c508165490da707e7d887361af3e722
Author: FRIGN <[email protected]>
Date:   Wed Mar 18 15:20:35 2015 +0100

    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.

diff --git a/Makefile b/Makefile
index 5c55f43..7364c99 100644
--- a/Makefile
+++ b/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\
diff --git a/libutil/agetcwd.c b/libutil/agetcwd.c
deleted file mode 100644
index e61673c..0000000
--- a/libutil/agetcwd.c
+++ /dev/null
@@ -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;
-}
-
diff --git a/libutil/apathmax.c b/libutil/apathmax.c
deleted file mode 100644
index b99b31b..0000000
--- a/libutil/apathmax.c
+++ /dev/null
@@ -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);
-}
diff --git a/libutil/cp.c b/libutil/cp.c
index 135a23f..c6ff48e 100644
--- a/libutil/cp.c
+++ b/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;
        }
 
diff --git a/libutil/enmasse.c b/libutil/enmasse.c
index 54d02eb..a2e225a 100644
--- a/libutil/enmasse.c
+++ b/libutil/enmasse.c
@@ -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);
 }
diff --git a/ls.c b/ls.c
index 6a36ff4..ea63340 100644
--- a/ls.c
+++ b/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
diff --git a/pwd.c b/pwd.c
index d31eec1..759bdcd 100644
--- a/pwd.c
+++ b/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;
diff --git a/util.h b/util.h
index 39d7d0d..d4415a5 100644
--- a/util.h
+++ b/util.h
@@ -20,9 +20,6 @@
 
 extern char *argv0;
 
-char *agetcwd(void);
-void apathmax(char **, size_t *);
-
 void *ecalloc(size_t, size_t);
 void *emalloc(size_t);
 void *erealloc(void *, size_t);

Reply via email to