diff options
-rw-r--r-- | lib/lib.c | 8 | ||||
-rw-r--r-- | lib/lib.h | 2 | ||||
-rw-r--r-- | toys/posix/cp.c | 9 |
3 files changed, 11 insertions, 8 deletions
@@ -1059,16 +1059,16 @@ char *getbasename(char *name) return name; } -// Is this file under this directory? -int fileunderdir(char *file, char *dir) +// Return pointer to xabspath(file) if file is under dir, else 0 +char *fileunderdir(char *file, char *dir) { char *s1 = xabspath(dir, 1), *s2 = xabspath(file, -1), *ss = s2; int rc = s1 && s2 && strstart(&ss, s1) && (!s1[1] || s2[strlen(s1)] == '/'); free(s1); - free(s2); + if (!rc) free(s2); - return rc; + return rc ? s2 : 0; } // Execute a callback for each PID that matches a process name from a list. @@ -371,7 +371,7 @@ 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); -int fileunderdir(char *file, char *dir); +char *fileunderdir(char *file, char *dir); void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)); pid_t __attribute__((returns_twice)) xvforkwrap(pid_t pid); diff --git a/toys/posix/cp.c b/toys/posix/cp.c index 3b30f8a8..751a718c 100644 --- a/toys/posix/cp.c +++ b/toys/posix/cp.c @@ -409,12 +409,15 @@ void cp_main(void) char *s = (toys.optflags&FLAG_D) ? getdirname(src) : getbasename(src); TT.destname = xmprintf("%s/%s", destname, s); - if (toys.optflags&FLAG_D) { + if (FLAG(D)) { free(s); - if (!fileunderdir(TT.destname, destname)) { + if (!(s = fileunderdir(TT.destname, destname))) { error_msg("%s not under %s", TT.destname, destname); continue; - } else mkpath(TT.destname); + } + // TODO: .. follows abspath, not links... + free(s); + mkpath(TT.destname); } } else TT.destname = destname; |