logo

utils-std

Collection of commonly available Unix tools
commit: dba7c89b63a14ec31c54b4a8a0a387cc9db14525
parent 5c8c4b15d94ad55722d8c5e6a98bd5e87fedf992
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun,  2 Jun 2024 20:56:50 +0200

cmd/mv: Use basename on destdir targets

Diffstat:

Mcmd/mv.c11++++++++++-
Mtest-cmd/mv.t13+++++++++++++
2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/cmd/mv.c b/cmd/mv.c @@ -12,6 +12,8 @@ #include <assert.h> #include <errno.h> #include <fcntl.h> // open +#include <limits.h> // PATH_MAX +#include <libgen.h> // basename #include <locale.h> // setlocale #include <stdbool.h> #include <stdint.h> // SIZE_MAX @@ -303,11 +305,18 @@ main(int argc, char *argv[]) } for(int i = 0; i < argc; i++) - if(do_renameat(argv[i], destdir, argv[i]) < 0) + { + char arg[PATH_MAX] = ""; + strcpy(arg, argv[i]); + + char *filename = basename(arg); + + if(do_renameat(argv[i], destdir, filename) < 0) { consent_finish(); return 1; } + } consent_finish(); diff --git a/test-cmd/mv.t b/test-cmd/mv.t @@ -128,6 +128,19 @@ Verbose (non-standard) $ test -e bar $ rm bar +Last component used for destination filename + $ mkdir -p src_last/dir dest_last + $ touch src_last/dir/file + $ mv src_last/dir/file dest_last/ + $ test -f dest_last/file + $ test ! -e dest_last/dir/file + $ test ! -e src_last/dir/file + $ mv src_last/dir dest_last + $ test -d dest_last/dir + $ test ! -e src_last/dir + $ test -f dest_last/file + $ rm -r src_last dest_last + No files should be left $ find . .