summaryrefslogtreecommitdiffstats
path: root/misc-utils/rename.c
diff options
context:
space:
mode:
authorSami Kerola2011-06-11 23:21:31 +0200
committerSami Kerola2011-06-25 13:45:27 +0200
commitd200a926df885cbe054add38bc2e4be2c8c7b1a9 (patch)
tree39a8286c26cade125a87449222c2d0e41a055fdd /misc-utils/rename.c
parentdocs: inform about cal long options (diff)
downloadkernel-qcow2-util-linux-d200a926df885cbe054add38bc2e4be2c8c7b1a9.tar.gz
kernel-qcow2-util-linux-d200a926df885cbe054add38bc2e4be2c8c7b1a9.tar.xz
kernel-qcow2-util-linux-d200a926df885cbe054add38bc2e4be2c8c7b1a9.zip
rename: verbose option & maintenance fixes
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>
Diffstat (limited to 'misc-utils/rename.c')
-rw-r--r--misc-utils/rename.c93
1 files changed, 61 insertions, 32 deletions
diff --git a/misc-utils/rename.c b/misc-utils/rename.c
index 495e511c9..6f55b7820 100644
--- a/misc-utils/rename.c
+++ b/misc-utils/rename.c
@@ -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;
}