summaryrefslogtreecommitdiffstats
path: root/misc-utils/rename.c
diff options
context:
space:
mode:
authorKarel Zak2006-12-07 00:25:41 +0100
committerKarel Zak2006-12-07 00:25:41 +0100
commiteb63b9b8f4cecb34c2478282567862bc48ef256d (patch)
tree99243f8eecb44c2bb6a559982b99c680fcb649e7 /misc-utils/rename.c
parentImported from util-linux-2.9v tarball. (diff)
downloadkernel-qcow2-util-linux-eb63b9b8f4cecb34c2478282567862bc48ef256d.tar.gz
kernel-qcow2-util-linux-eb63b9b8f4cecb34c2478282567862bc48ef256d.tar.xz
kernel-qcow2-util-linux-eb63b9b8f4cecb34c2478282567862bc48ef256d.zip
Imported from util-linux-2.10f tarball.
Diffstat (limited to 'misc-utils/rename.c')
-rw-r--r--misc-utils/rename.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/misc-utils/rename.c b/misc-utils/rename.c
new file mode 100644
index 000000000..af5ba44cc
--- /dev/null
+++ b/misc-utils/rename.c
@@ -0,0 +1,97 @@
+/*
+ * rename.c - aeb 2000-01-01
+ *
+--------------------------------------------------------------
+#!/bin/sh
+if [ $# -le 2 ]; then echo call: rename from to files; exit; fi
+FROM="$1"
+TO="$2"
+shift
+shift
+for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
+--------------------------------------------------------------
+ * This shell script will do renames of files, but may fail
+ * in cases involving special characters. Here a C version.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "nls.h"
+
+static char *progname;
+
+int
+do_rename(char *from, char *to, char *s) {
+ char *newname, *where, *p, *q;
+ int flen, tlen, slen;
+
+ where = strstr(s, from);
+ if (where == NULL)
+ return 0;
+
+ flen = strlen(from);
+ tlen = strlen(to);
+ slen = strlen(s);
+ newname = malloc(tlen+slen+1);
+ if (newname == NULL) {
+ fprintf(stderr, _("%s: out of memory\n"), progname);
+ exit(1);
+ }
+
+ p = s;
+ q = newname;
+ while (p < where)
+ *q++ = *p++;
+ p = to;
+ while (*p)
+ *q++ = *p++;
+ p = where+flen;
+ while (*p)
+ *q++ = *p++;
+ *p = 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);
+ }
+
+ return 1;
+}
+
+int
+main(int argc, char **argv) {
+ char *from, *to, *p;
+ int i, ct;
+
+ progname = argv[0];
+ if ((p = strrchr(progname, '/')) != NULL)
+ progname = p+1;
+
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
+
+ if (argc == 2) {
+ if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
+ printf(_("%s from %s\n"),
+ progname, util_linux_version);
+ return 0;
+ }
+ }
+
+ if (argc < 3) {
+ fprintf(stderr, _("call: %s from to files...\n"), progname);
+ exit(1);
+ }
+
+ from = argv[1];
+ to = argv[2];
+
+ ct = 0;
+ for (i=3; i<argc; i++)
+ ct += do_rename(from, to, argv[i]);
+ return 0;
+}