]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
rename: verbose option & maintenance fixes
authorSami Kerola <kerolasa@iki.fi>
Sat, 11 Jun 2011 21:21:31 +0000 (23:21 +0200)
committerSami Kerola <kerolasa@iki.fi>
Sat, 25 Jun 2011 11:45:27 +0000 (13:45 +0200)
The rename has new verbose option which will print which files
where renamed. Maintenance fixes includes long options, coding
style and freeing memory after usage.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
misc-utils/rename.c

index 495e511c97cd2dad649566ba02af7fac7ffa20b3..6f55b7820710b6f8b05e65969eb9c68ed8ff4737 100644 (file)
@@ -17,14 +17,14 @@ for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <getopt.h>
 
 #include "nls.h"
 #include "xalloc.h"
+#include "c.h"
 
-static char *progname;
-
-static int
-do_rename(char *from, char *to, char *s) {
+static int do_rename(char *from, char *to, char *s, int verbose)
+{
        char *newname, *where, *p, *q;
        int flen, tlen, slen;
 
@@ -35,7 +35,7 @@ do_rename(char *from, char *to, char *s) {
        flen = strlen(from);
        tlen = strlen(to);
        slen = strlen(s);
-       newname = xmalloc(tlen+slen+1);
+       newname = xmalloc(tlen + slen + 1);
 
        p = s;
        q = newname;
@@ -44,51 +44,80 @@ do_rename(char *from, char *to, char *s) {
        p = to;
        while (*p)
                *q++ = *p++;
-       p = where+flen;
+       p = where + flen;
        while (*p)
                *q++ = *p++;
        *q = 0;
 
-       if (rename(s, newname) != 0) {
-               int errsv = errno;
-               fprintf(stderr, _("%s: renaming %s to %s failed: %s\n"),
-                                 progname, s, newname, strerror(errsv));
-               exit(1);
-       }
+       if (rename(s, newname) != 0)
+               err(EXIT_FAILURE, _("renaming %s to %s failed"),
+                   s, newname);
+       if (verbose)
+               printf("`%s' -> `%s'\n", s, newname);
 
+       free(newname);
        return 1;
 }
 
-int
-main(int argc, char **argv) {
-       char *from, *to, *p;
-       int i;
+static void __attribute__ ((__noreturn__)) usage(FILE * out)
+{
+       fprintf(out,
+               _("Usage: %s [options] expression replacement file...\n"),
+               program_invocation_short_name);
+
+       fprintf(out, _("\nOptions:\n"
+                       " -v, --verbose    explain what is being done\n"
+                      " -V, --version    output version information and exit\n"
+                      " -h, --help       display this help and exit\n\n"));
+
+       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
 
-       progname = argv[0];
-       if ((p = strrchr(progname, '/')) != NULL)
-               progname = p+1;
+int main(int argc, char **argv)
+{
+       char *from, *to;
+       int i, c, verbose = 0;
+
+       static const struct option longopts[] = {
+               {"verbose", no_argument, NULL, 'v'},
+               {"version", no_argument, NULL, 'V'},
+               {"help", no_argument, NULL, 'h'},
+               {NULL, 0, NULL, 0}
+       };
 
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
 
-       if (argc == 2) {
-               if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
-                       printf(_("%s (%s)\n"),
-                              progname, PACKAGE_STRING);
-                       return 0;
+       while ((c = getopt_long(argc, argv, "vVh", longopts, NULL)) != -1)
+               switch (c) {
+               case 'v':
+                       verbose = 1;
+                       break;
+               case 'V':
+                       printf(_("%s from %s\n"),
+                              program_invocation_short_name,
+                              PACKAGE_STRING);
+                       return EXIT_SUCCESS;
+               case 'h':
+                       usage(stdout);
+               default:
+                       usage(stderr);
                }
-       }
+
+       argc -= optind;
+       argv += optind;
 
        if (argc < 3) {
-               fprintf(stderr, _("call: %s from to files...\n"), progname);
-               exit(1);
+               warnx("not enough arguments");
+               usage(stderr);
        }
 
-       from = argv[1];
-       to = argv[2];
+       from = argv[0];
+       to = argv[1];
+
+       for (i = 2; i < argc; i++)
+               do_rename(from, to, argv[i], verbose);
 
-       for (i=3; i<argc; i++)
-               do_rename(from, to, argv[i]);
-       return 0;
+       return EXIT_SUCCESS;
 }