]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/rename.c
Merge branch 'meson-more-build-options' of https://github.com/jwillikers/util-linux
[thirdparty/util-linux.git] / misc-utils / rename.c
1 /*
2 * rename.c - aeb 2000-01-01
3 *
4 --------------------------------------------------------------
5 #!/bin/sh
6 if [ $# -le 2 ]; then echo call: rename from to files; exit; fi
7 FROM="$1"
8 TO="$2"
9 shift
10 shift
11 for i in $@; do N=`echo "$i" | sed "s/$FROM/$TO/g"`; mv "$i" "$N"; done
12 --------------------------------------------------------------
13 * This shell script will do renames of files, but may fail
14 * in cases involving special characters. Here a C version.
15 */
16 #include <stdio.h>
17 #ifdef HAVE_STDIO_EXT_H
18 # include <stdio_ext.h>
19 #endif
20 #ifndef HAVE___FPURGE
21 # ifdef HAVE_FPURGE
22 # define HAVE___FPURGE 1
23 # define __fpurge fpurge
24 # endif
25 #endif
26 #include <string.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <getopt.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <termios.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include "nls.h"
37 #include "xalloc.h"
38 #include "c.h"
39 #include "closestream.h"
40 #include "rpmatch.h"
41
42 #define RENAME_EXIT_SOMEOK 2
43 #define RENAME_EXIT_NOTHING 4
44 #define RENAME_EXIT_UNEXPLAINED 64
45
46 static int tty_cbreak = 0;
47
48 static int string_replace(char *from, char *to, char *s, char *orig, char **newname)
49 {
50 char *p, *q, *where;
51
52 where = strstr(s, from);
53 if (where == NULL)
54 return 1;
55 p = orig;
56 *newname = xmalloc(strlen(orig) + strlen(to) + 1);
57 q = *newname;
58 while (p < where)
59 *q++ = *p++;
60 p = to;
61 while (*p)
62 *q++ = *p++;
63 p = where + strlen(from);
64 while (*p)
65 *q++ = *p++;
66 *q = 0;
67 return 0;
68 }
69
70 static int ask(char *name)
71 {
72 int c;
73 char buf[2];
74 printf(_("%s: overwrite `%s'? "), program_invocation_short_name, name);
75 fflush(stdout);
76 if ((c = fgetc(stdin)) == EOF) {
77 buf[0] = 'n';
78 printf("n\n");
79 }
80 else {
81 buf[0] = c;
82 if (c != '\n' && tty_cbreak) {
83 #ifdef HAVE___FPURGE
84 /* Possibly purge a multi-byte character; or do a
85 required purge of the rest of the line (including
86 the newline) if the tty has been put back in
87 canonical mode (for example by a shell after a
88 SIGTSTP signal). */
89 __fpurge(stdin);
90 #endif
91 printf("\n");
92 }
93 else if (c != '\n')
94 while ((c = fgetc(stdin)) != '\n' && c != EOF);
95 }
96 buf[1] = '\0';
97 if (rpmatch(buf) == RPMATCH_YES)
98 return 0;
99 else
100 return 1;
101 }
102
103 static int do_symlink(char *from, char *to, char *s, int verbose, int noact,
104 int nooverwrite, int interactive)
105 {
106 char *newname = NULL, *target = NULL;
107 int ret = 1;
108 struct stat sb;
109
110 if ( faccessat(AT_FDCWD, s, F_OK, AT_SYMLINK_NOFOLLOW) != 0 &&
111 errno != EINVAL )
112 /* Skip if AT_SYMLINK_NOFOLLOW is not supported; lstat() below will
113 detect the access error */
114 {
115 warn(_("%s: not accessible"), s);
116 return 2;
117 }
118
119 if (lstat(s, &sb) == -1) {
120 warn(_("stat of %s failed"), s);
121 return 2;
122 }
123 if (!S_ISLNK(sb.st_mode)) {
124 warnx(_("%s: not a symbolic link"), s);
125 return 2;
126 }
127 target = xmalloc(sb.st_size + 1);
128 if (readlink(s, target, sb.st_size + 1) < 0) {
129 warn(_("%s: readlink failed"), s);
130 free(target);
131 return 2;
132 }
133 target[sb.st_size] = '\0';
134 if (string_replace(from, to, target, target, &newname) != 0)
135 ret = 0;
136
137 if (ret == 1 && (nooverwrite || interactive) && lstat(newname, &sb) != 0)
138 nooverwrite = interactive = 0;
139
140 if ( ret == 1 &&
141 (nooverwrite || (interactive && (noact || ask(newname) != 0))) )
142 {
143 if (verbose)
144 printf(_("Skipping existing link: `%s' -> `%s'\n"), s, target);
145 ret = 0;
146 }
147
148 if (ret == 1) {
149 if (!noact && 0 > unlink(s)) {
150 warn(_("%s: unlink failed"), s);
151 ret = 2;
152 }
153 else if (!noact && symlink(newname, s) != 0) {
154 warn(_("%s: symlinking to %s failed"), s, newname);
155 ret = 2;
156 }
157 }
158 if (verbose && (noact || ret == 1))
159 printf("%s: `%s' -> `%s'\n", s, target, newname);
160 free(newname);
161 free(target);
162 return ret;
163 }
164
165 static int do_file(char *from, char *to, char *s, int verbose, int noact,
166 int nooverwrite, int interactive)
167 {
168 char *newname = NULL, *file=NULL;
169 int ret = 1;
170
171 if (access(s, F_OK) != 0) {
172 warn(_("%s: not accessible"), s);
173 return 2;
174 }
175
176 if (strchr(from, '/') == NULL && strchr(to, '/') == NULL)
177 file = strrchr(s, '/');
178 if (file == NULL)
179 file = s;
180 if (string_replace(from, to, file, s, &newname) != 0)
181 return 0;
182
183 if ((nooverwrite || interactive) && access(newname, F_OK) != 0)
184 nooverwrite = interactive = 0;
185
186 if (nooverwrite || (interactive && (noact || ask(newname) != 0))) {
187 if (verbose)
188 printf(_("Skipping existing file: `%s'\n"), newname);
189 ret = 0;
190 }
191 else if (!noact && rename(s, newname) != 0) {
192 warn(_("%s: rename to %s failed"), s, newname);
193 ret = 2;
194 }
195 if (verbose && (noact || ret == 1))
196 printf("`%s' -> `%s'\n", s, newname);
197 free(newname);
198 return ret;
199 }
200
201 static void __attribute__((__noreturn__)) usage(void)
202 {
203 FILE *out = stdout;
204 fputs(USAGE_HEADER, out);
205 fprintf(out,
206 _(" %s [options] <expression> <replacement> <file>...\n"),
207 program_invocation_short_name);
208
209 fputs(USAGE_SEPARATOR, out);
210 fputs(_("Rename files.\n"), out);
211
212 fputs(USAGE_OPTIONS, out);
213 fputs(_(" -v, --verbose explain what is being done\n"), out);
214 fputs(_(" -s, --symlink act on the target of symlinks\n"), out);
215 fputs(_(" -n, --no-act do not make any changes\n"), out);
216 fputs(_(" -o, --no-overwrite don't overwrite existing files\n"), out);
217 fputs(_(" -i, --interactive prompt before overwrite\n"), out);
218 fputs(USAGE_SEPARATOR, out);
219 printf(USAGE_HELP_OPTIONS(21));
220 printf(USAGE_MAN_TAIL("rename(1)"));
221 exit(EXIT_SUCCESS);
222 }
223
224 int main(int argc, char **argv)
225 {
226 char *from, *to;
227 int i, c, ret = 0, verbose = 0, noact = 0, nooverwrite = 0, interactive = 0;
228 struct termios tio;
229 int (*do_rename)(char *from, char *to, char *s, int verbose, int noact,
230 int nooverwrite, int interactive) = do_file;
231
232 static const struct option longopts[] = {
233 {"verbose", no_argument, NULL, 'v'},
234 {"version", no_argument, NULL, 'V'},
235 {"help", no_argument, NULL, 'h'},
236 {"no-act", no_argument, NULL, 'n'},
237 {"no-overwrite", no_argument, NULL, 'o'},
238 {"interactive", no_argument, NULL, 'i'},
239 {"symlink", no_argument, NULL, 's'},
240 {NULL, 0, NULL, 0}
241 };
242
243 setlocale(LC_ALL, "");
244 bindtextdomain(PACKAGE, LOCALEDIR);
245 textdomain(PACKAGE);
246 close_stdout_atexit();
247
248 while ((c = getopt_long(argc, argv, "vsVhnoi", longopts, NULL)) != -1)
249 switch (c) {
250 case 'n':
251 noact = 1;
252 break;
253 case 'v':
254 verbose = 1;
255 break;
256 case 'o':
257 nooverwrite = 1;
258 interactive = 0;
259 break;
260 case 'i':
261 interactive = 1;
262 nooverwrite = 0;
263 break;
264 case 's':
265 do_rename = do_symlink;
266 break;
267
268 case 'V':
269 print_version(EXIT_SUCCESS);
270 case 'h':
271 usage();
272 default:
273 errtryhelp(EXIT_FAILURE);
274 }
275
276 argc -= optind;
277 argv += optind;
278
279 if (argc < 3) {
280 warnx(_("not enough arguments"));
281 errtryhelp(EXIT_FAILURE);
282 }
283
284 from = argv[0];
285 to = argv[1];
286
287 if (!strcmp(from, to))
288 return RENAME_EXIT_NOTHING;
289
290 tty_cbreak = 0;
291 if (interactive && isatty(STDIN_FILENO) != 0) {
292 if (tcgetattr(STDIN_FILENO, &tio) != 0)
293 warn(_("failed to get terminal attributes"));
294 else if (!(tio.c_lflag & ICANON) && tio.c_cc[VMIN] == 1)
295 tty_cbreak = 1;
296 }
297
298 for (i = 2; i < argc; i++)
299 ret |= do_rename(from, to, argv[i], verbose, noact, nooverwrite, interactive);
300
301 switch (ret) {
302 case 0:
303 return RENAME_EXIT_NOTHING;
304 case 1:
305 return EXIT_SUCCESS;
306 case 2:
307 return EXIT_FAILURE;
308 case 3:
309 return RENAME_EXIT_SOMEOK;
310 default:
311 return RENAME_EXIT_UNEXPLAINED;
312 }
313 }