]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/rename.c
dmesg: add --follow-new
[thirdparty/util-linux.git] / misc-utils / rename.c
index 032a039bfac9a5133a803e86cb5fa8ff1eb9d66c..d17b25abe56b22bf2ec2d6e4c636626e4460dfa1 100644 (file)
@@ -14,6 +14,15 @@ 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>
+#ifdef HAVE_STDIO_EXT_H
+#      include <stdio_ext.h>
+#endif
+#ifndef HAVE___FPURGE
+# ifdef HAVE_FPURGE
+#      define HAVE___FPURGE 1
+#      define __fpurge fpurge
+# endif
+#endif
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -66,21 +75,29 @@ static int ask(char *name)
        fflush(stdout);
        if ((c = fgetc(stdin)) == EOF) {
                buf[0] = 'n';
-               clearerr(stdin); errno = 0;
                printf("n\n");
        }
        else {
                buf[0] = c;
-               if (c != '\n' && tty_cbreak) /* no purge necessary */
+               if (c != '\n' && tty_cbreak) {
+#ifdef HAVE___FPURGE
+                       /* 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);
+#endif
                        printf("\n");
+               }
                else if (c != '\n')
                        while ((c = fgetc(stdin)) != '\n' && c != EOF);
        }
        buf[1] = '\0';
        if (rpmatch(buf) == RPMATCH_YES)
                return 0;
-       else
-               return 1;
+
+       return 1;
 }
 
 static int do_symlink(char *from, char *to, char *s, int verbose, int noact,
@@ -114,7 +131,7 @@ static int do_symlink(char *from, char *to, char *s, int verbose, int noact,
                return 2;
        }
        target[sb.st_size] = '\0';
-       if (string_replace(from, to, target, target, &newname))
+       if (string_replace(from, to, target, target, &newname) != 0)
                ret = 0;
 
        if (ret == 1 && (nooverwrite || interactive) && lstat(newname, &sb) != 0)
@@ -132,14 +149,14 @@ static int do_symlink(char *from, char *to, char *s, int verbose, int noact,
                if (!noact && 0 > unlink(s)) {
                        warn(_("%s: unlink failed"), s);
                        ret = 2;
-               } else if (!noact && symlink(newname, s) != 0) {
+               }
+               else if (!noact && symlink(newname, s) != 0) {
                        warn(_("%s: symlinking to %s failed"), s, newname);
                        ret = 2;
                }
        }
        if (verbose && (noact || ret == 1))
-               if (verbose)
-                       printf("%s: `%s' -> `%s'\n", s, target, newname);
+               printf("%s: `%s' -> `%s'\n", s, target, newname);
        free(newname);
        free(target);
        return ret;
@@ -160,7 +177,7 @@ static int do_file(char *from, char *to, char *s, int verbose, int noact,
                file = strrchr(s, '/');
        if (file == NULL)
                file = s;
-       if (string_replace(from, to, file, s, &newname))
+       if (string_replace(from, to, file, s, &newname) != 0)
                return 0;
 
        if ((nooverwrite || interactive) && access(newname, F_OK) != 0)
@@ -226,7 +243,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while ((c = getopt_long(argc, argv, "vsVhnoi", longopts, NULL)) != -1)
                switch (c) {
@@ -247,9 +264,9 @@ int main(int argc, char **argv)
                case 's':
                        do_rename = do_symlink;
                        break;
+
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'h':
                        usage();
                default: