From b301240424c7fed2ca8b836e2c9531d6ec59750d Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 21 Oct 2019 17:20:34 -0500 Subject: Remove getdirname(), it's _not_ a drop-in replacement for dirname (it returns a malloc), and doesn't match the object lifetime of getbasename() (which always returns some or all of its argument string). The dirname() in libc modifies its argument string, but that's what posix says to do: https://pubs.opengroup.org/onlinepubs/9699919799.2008edition/functions/dirname.html so I guess we can live with it. --- lib/lib.c | 15 --------------- lib/lib.h | 1 - toys/posix/cp.c | 3 +-- 3 files changed, 1 insertion(+), 18 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 22b85b06..03c1db69 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1005,21 +1005,6 @@ void mode_to_string(mode_t mode, char *buf) *buf = c; } -// dirname() can modify its argument or return a pointer to a constant string -// This returns a malloc() copy of everyting before last (run of ) '/', -// or "." if no path. -char *getdirname(char *name) -{ - char *s, *ss, *keep; - - for (s = name, ss = keep = 0; ; s++) { - if (!s || !*s) return keep ? xstrndup(name, keep-name) : xstrdup("."); - if (*s != '/') keep = ss; - else if (s == name) keep = ss = s+1; - else if (s[-1] != '/') ss = s; - } -} - // basename() can modify its argument or return a pointer to a constant string // This just gives after the last '/' or the whole stirng if no / char *getbasename(char *name) diff --git a/lib/lib.h b/lib/lib.h index 6d63bb09..b48d320e 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -400,7 +400,6 @@ void list_signals(); mode_t string_to_mode(char *mode_str, mode_t base); void mode_to_string(mode_t mode, char *buf); -char *getdirname(char *name); char *getbasename(char *name); char *fileunderdir(char *file, char *dir); void names_to_pid(char **names, int (*callback)(pid_t pid, char *name), diff --git a/toys/posix/cp.c b/toys/posix/cp.c index bba54809..a843b90a 100644 --- a/toys/posix/cp.c +++ b/toys/posix/cp.c @@ -410,11 +410,10 @@ void cp_main(void) } if (destdir) { - char *s = FLAG(D) ? getdirname(src) : getbasename(src); + char *s = FLAG(D) ? dirname(src) : getbasename(src); TT.destname = xmprintf("%s/%s", destname, s); if (FLAG(D)) { - free(s); if (!(s = fileunderdir(TT.destname, destname))) { error_msg("%s not under %s", TT.destname, destname); continue; -- cgit v1.2.3