]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rename: ask(): call __fpurge() to cater for multi-byte characters
authorG.raud Meyer <graud@gmx.com>
Tue, 5 Apr 2011 09:31:03 +0000 (11:31 +0200)
committerG.raud Meyer <graud@gmx.com>
Mon, 9 Apr 2018 15:21:17 +0000 (17:21 +0200)
Making a purge in cbreak mode also makes the code compatible with
canonical mode.  This can be useful in the case a shell, like bash, does
not restore the tty state of stopped jobs before restarting them.  An
alternative fix to this minor shortcoming would be to retest the tty
state each time inside ask().

misc-utils/rename.c

index 032a039bfac9a5133a803e86cb5fa8ff1eb9d66c..c9add77803b05a41b332ce13557368f5012615cf 100644 (file)
@@ -14,6 +14,7 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
  * in cases involving special characters. Here a C version.
  */
 #include <stdio.h>
+#include <stdio_ext.h>
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -71,8 +72,15 @@ static int ask(char *name)
        }
        else {
                buf[0] = c;
-               if (c != '\n' && tty_cbreak) /* no purge necessary */
+               if (c != '\n' && tty_cbreak) {
+                       /* Possibly purge a multi-byte character; or do a
+                          required purge of the rest of the line (including
+                          the newline) if the tty has been put back in
+                          canonical mode (for example by a shell after a
+                          SIGTSTP signal). */
+                       __fpurge(stdin);
                        printf("\n");
+               }
                else if (c != '\n')
                        while ((c = fgetc(stdin)) != '\n' && c != EOF);
        }