diff options
author | Rob Landley <rob@landley.net> | 2021-03-13 03:13:21 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2021-03-13 03:13:21 -0600 |
commit | 8e9d7aaa838ed58b7b354a3b378cda1bd84f8dc5 (patch) | |
tree | 3c19d5e2b5c446f761d4062c912e9c49e6f32479 | |
parent | 4ff131ff958e2bbe7f1cc8a3d0e0acdd4a48c7ee (diff) | |
download | toybox-8e9d7aaa838ed58b7b354a3b378cda1bd84f8dc5.tar.gz |
Commit 3b9cfa70db needed some cleanup I forgot to do, oops. Elliott stumbled
across one of the issues I forgot to fix. Added a couple comments and
renamed a variable to try to clarify the code while I was there.
-rw-r--r-- | toys/posix/cp.c | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/toys/posix/cp.c b/toys/posix/cp.c index 56fbadf9..d98c2ae7 100644 --- a/toys/posix/cp.c +++ b/toys/posix/cp.c @@ -412,11 +412,11 @@ void cp_main(void) // Loop through sources for (i=0; i<toys.optc; i++) { - char *src = toys.optargs[i], *trail = src; - int rc = 1; + char *src = toys.optargs[i], *trail; + int send = 1; - while (*++trail); - if (*--trail == '/') *trail = 0; + if (!(trail = strrchr(src, '/')) || trail[1]) trail = 0; + else while (trail>src && *trail=='/') *trail-- = 0; if (destdir) { char *s = FLAG(D) ? src : getbasename(src); @@ -433,6 +433,7 @@ void cp_main(void) } } else TT.destname = destname; + // "mv across devices" triggers cp fallback path, so set that as default errno = EXDEV; if (CFG_MV && toys.which->name[0] == 'm') { int force = FLAG(f), no_clobber = FLAG(n); @@ -446,18 +447,18 @@ void cp_main(void) // _else_) but I don't care. if (exists && (FLAG(i) || (!(st.st_mode & 0222) && isatty(0)))) { fprintf(stderr, "%s: overwrite '%s'", toys.which->name, TT.destname); - if (!yesno(0)) rc = 0; + if (!yesno(0)) send = 0; else unlink(TT.destname); } // if -n and dest exists, don't try to rename() or copy - if (exists && no_clobber) rc = 0; + if (exists && no_clobber) send = 0; } - if (rc) rc = rename(src, TT.destname); - if (errno && !*trail) *trail = '/'; + if (send) send = rename(src, TT.destname); + if (trail) trail[1] = '/'; } - // Copy if we didn't mv, skipping nonexistent sources - if (rc) { + // Copy if we didn't mv or hit an error, skipping nonexistent sources + if (send) { if (errno!=EXDEV || dirtree_flagread(src, DIRTREE_SHUTUP+ DIRTREE_SYMFOLLOW*!!(FLAG(H)||FLAG(L)), TT.callback)) perror_msg("bad '%s'", src); |