logo

utils-std

Collection of commonly available Unix tools
commit: 3f00af9a25a017b1d7fa7614cb964c151b969afc
parent 3b4f48ccbddf6819242f7056f20e91e88f4a70d9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 12 May 2024 16:52:07 +0200

cmd/mv: Handle stdin not being a TTY

Diffstat:

Mcmd/mv.c23++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/cmd/mv.c b/cmd/mv.c @@ -29,7 +29,7 @@ char *argv0 = "mv"; -bool no_clob = false, force = false; +bool no_clob = false, force = false, interact = false; struct named_fd { @@ -107,10 +107,20 @@ do_renameat(const char *restrict src, struct named_fd destdir, const char *restr return -1; } - if(!force && !consentf("mv: Destination file '%s/%s' already exists, overwrite? [yN] ", - destdir.name, - dest)) - return 0; + if(!force) + { + if(interact || isatty(STDIN_FILENO)) + { + errno = 0; + if(!consentf("mv: Destination file '%s/%s' already exists, overwrite? [yN] ", destdir.name, dest)) return 0; + } + else + { + errno = 0; + fprintf(stderr, "mv: Destination file '%s/%s' already exists.\n", destdir.name, dest); + return 0; + } + } } else { @@ -192,14 +202,17 @@ main(int argc, char *argv[]) { case 'f': force = true; + interact = false; no_clob = false; break; case 'i': force = false; + interact = true; no_clob = false; break; case 'n': force = false; + interact = false; no_clob = true; break; case 't':