aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2019-10-21 17:20:34 -0500
committerRob Landley <rob@landley.net>2019-10-21 17:20:34 -0500
commitb301240424c7fed2ca8b836e2c9531d6ec59750d (patch)
tree20c0b3516bb13a19d4778dbe89d082d17afcb988
parent944d818cd97cc5e5d5f710a595405faf075da9d3 (diff)
downloadtoybox-b301240424c7fed2ca8b836e2c9531d6ec59750d.tar.gz
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.
-rw-r--r--lib/lib.c15
-rw-r--r--lib/lib.h1
-rw-r--r--toys/posix/cp.c3
3 files changed, 1 insertions, 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;