summaryrefslogtreecommitdiffstats
path: root/misc-utils/rename.c
diff options
context:
space:
mode:
authorG.raud Meyer2018-03-29 12:08:46 +0200
committerG.raud Meyer2018-04-09 17:21:16 +0200
commitf43bdedaa7334412aabefa62b8ae559b5b1d9e63 (patch)
tree2aeef9ac7716da92dcf5fe6412a55a5d7dd6a93a /misc-utils/rename.c
parentrename.1: fix warning section (diff)
downloadkernel-qcow2-util-linux-f43bdedaa7334412aabefa62b8ae559b5b1d9e63.tar.gz
kernel-qcow2-util-linux-f43bdedaa7334412aabefa62b8ae559b5b1d9e63.tar.xz
kernel-qcow2-util-linux-f43bdedaa7334412aabefa62b8ae559b5b1d9e63.zip
rename: detect tty in cbreak mode to make ask() read a single byte
Set tty_cbreak only when tty has a VMIN of 1 to avoid having to purge at all in cbreak mode. The prompt is still compatible with a non interactive input from a pipe.
Diffstat (limited to 'misc-utils/rename.c')
-rw-r--r--misc-utils/rename.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/misc-utils/rename.c b/misc-utils/rename.c
index d98e9d8f9..1c24d7e32 100644
--- a/misc-utils/rename.c
+++ b/misc-utils/rename.c
@@ -20,6 +20,7 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
+#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -33,6 +34,8 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
#define RENAME_EXIT_NOTHING 4
#define RENAME_EXIT_UNEXPLAINED 64
+static int tty_cbreak = 0;
+
static int string_replace(char *from, char *to, char *s, char *orig, char **newname)
{
char *p, *q, *where;
@@ -68,7 +71,9 @@ static int ask(char *name)
}
else {
buf[0] = c; buf[1] = '\0';
- if (c != '\n')
+ if (c != '\n' && tty_cbreak) /* no purge necessary */
+ printf("\n");
+ else if (c != '\n')
while ((c = fgetc(stdin)) != '\n' && c != EOF);
}
if (rpmatch(buf) == RPMATCH_YES)
@@ -202,6 +207,7 @@ int main(int argc, char **argv)
{
char *from, *to;
int i, c, ret = 0, verbose = 0, noact = 0, nooverwrite = 0, interactive = 0;
+ struct termios tio;
int (*do_rename)(char *from, char *to, char *s, int verbose, int noact,
int nooverwrite, int interactive) = do_file;
@@ -263,6 +269,14 @@ int main(int argc, char **argv)
if (!strcmp(from, to))
return RENAME_EXIT_NOTHING;
+ tty_cbreak = 0;
+ if (interactive && isatty(STDIN_FILENO) != 0) {
+ if (tcgetattr(STDIN_FILENO, &tio) != 0)
+ warn(_("failed to get terminal attributes"));
+ else if (!(tio.c_lflag & ICANON) && tio.c_cc[VMIN] == 1)
+ tty_cbreak = 1;
+ }
+
for (i = 2; i < argc; i++)
ret |= do_rename(from, to, argv[i], verbose, noact, nooverwrite, interactive);