1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/*
* 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"
#include "xalloc.h"
static char *progname;
static 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 = xmalloc(tlen+slen+1);
p = s;
q = newname;
while (p < where)
*q++ = *p++;
p = to;
while (*p)
*q++ = *p++;
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);
}
return 1;
}
int
main(int argc, char **argv) {
char *from, *to, *p;
int i;
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 (%s)\n"),
progname, PACKAGE_STRING);
return 0;
}
}
if (argc < 3) {
fprintf(stderr, _("call: %s from to files...\n"), progname);
exit(1);
}
from = argv[1];
to = argv[2];
for (i=3; i<argc; i++)
do_rename(from, to, argv[i]);
return 0;
}
|