logo

utils-std

Collection of commonly available Unix tools
commit: 8f3a7a5b8fe1234c35d7e919652788f659b9dcf6
parent ef5891f35c5bd86fc11e12aa60cbf3bc2caaea96
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Wed, 22 May 2024 10:46:49 +0200

cmd/mv: Add support for -v (verbose)

Diffstat:

Mcmd/mv.15+++++
Mcmd/mv.c19+++++++++++++------
Mtest-cmd/mv.t8++++++++
3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/cmd/mv.1 b/cmd/mv.1 @@ -10,14 +10,17 @@ .Sh SYNOPSIS .Nm .Op Fl f Ns | Ns Fl i Ns | Ns Fl n +.Op Fl v .Ar source .Ar dest .Nm .Op Fl f Ns | Ns Fl i Ns | Ns Fl n +.Op Fl v .Ar source... .Ar destdir .Nm .Op Fl f Ns | Ns Fl i Ns | Ns Fl n +.Op Fl v .Fl t Ar destdir .Ar source... .Sh DESCRIPTION @@ -66,6 +69,8 @@ and options. .It Fl t Ar destdir Set the destination directory. +.It Fl v +Verbose, write which action has been done. .El .Sh STANDARDS The diff --git a/cmd/mv.c b/cmd/mv.c @@ -29,7 +29,7 @@ char *argv0 = "mv"; -bool no_clob = false, force = false, interact = false; +bool no_clob = false, force = false, interact = false, verbose = false; struct named_fd { @@ -112,7 +112,9 @@ do_renameat(const char *restrict src, struct named_fd destdir, const char *restr if(interact || isatty(STDIN_FILENO)) { errno = 0; - if(!consentf("mv: Destination file '%s/%s' already exists, overwrite? [yN] ", destdir.name, dest)) return 0; + if(!consentf( + "mv: Destination file '%s/%s' already exists, overwrite? [yN] ", destdir.name, dest)) + return 0; } else { @@ -176,15 +178,17 @@ do_renameat(const char *restrict src, struct named_fd destdir, const char *restr } } + if(verbose) fprintf(stderr, "mv: renamed '%s' -> '%s/%s'\n", src, destdir.name, dest); + return 0; } static void usage() { - fprintf(stderr, "Usage: mv [-f|-i|-n] source dest\n"); - fprintf(stderr, " mv [-f|-i|-n] source... destdir\n"); - fprintf(stderr, " mv [-f|-i|-n] -t destdir source...\n"); + fprintf(stderr, "Usage: mv [-f|-i|-n] [-v] source dest\n"); + fprintf(stderr, " mv [-f|-i|-n] [-v] source... destdir\n"); + fprintf(stderr, " mv [-f|-i|-n] [-v] -t destdir source...\n"); } int @@ -196,7 +200,7 @@ main(int argc, char *argv[]) }; int c = -1; - while((c = getopt(argc, argv, ":fint:")) != -1) + while((c = getopt(argc, argv, ":fint:v")) != -1) { switch(c) { @@ -226,6 +230,9 @@ main(int argc, char *argv[]) return 1; } break; + case 'v': + verbose = true; + break; case ':': fprintf(stderr, "mv: Error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/test-cmd/mv.t b/test-cmd/mv.t @@ -81,6 +81,14 @@ POSIX mv(1) step 2, same file $ test -e same-s $ rm same Same same-s +Verbose (non-standard) + $ touch foo + $ mv -v foo bar + mv: renamed 'foo' -> './bar' + $ test ! -e foo + $ test -e bar + $ rm bar + No files should be left $ find . .