summaryrefslogtreecommitdiffstats
path: root/misc-utils/rename.c
blob: a9378af59be687a144efb8ba7d08430d645ee9c5 (plain) (blame)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * 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>
#ifdef HAVE_STDIO_EXT_H
#	include <stdio_ext.h>
#endif
#ifndef HAVE___FPURGE
# ifdef HAVE_FPURGE
#	define HAVE___FPURGE 1
#	define __fpurge fpurge
# endif
#endif
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "nls.h"
#include "xalloc.h"
#include "c.h"
#include "closestream.h"
#include "rpmatch.h"

#define RENAME_EXIT_SOMEOK	2
#define RENAME_EXIT_NOTHING	4
#define RENAME_EXIT_UNEXPLAINED	64

static int tty_cbreak = 0;

static int string_replace(char *from, char *to, char *s, char *orig, char **newname)
{
	char *p, *q, *where;

	where = strstr(s, from);
	if (where == NULL)
		return 1;
	p = orig;
	*newname = xmalloc(strlen(orig) + strlen(to) + 1);
	q = *newname;
	while (p < where)
		*q++ = *p++;
	p = to;
	while (*p)
		*q++ = *p++;
	p = where + strlen(from);
	while (*p)
		*q++ = *p++;
	*q = 0;
	return 0;
}

static int ask(char *name)
{
	int c;
	char buf[2];
	printf(_("%s: overwrite `%s'? "), program_invocation_short_name, name);
	fflush(stdout);
	if ((c = fgetc(stdin)) == EOF) {
		buf[0] = 'n';
		printf("n\n");
	}
	else {
		buf[0] = c;
		if (c != '\n' && tty_cbreak) {
#ifdef HAVE___FPURGE
			/* Possibly purge a multi-byte character; or do a
			   required purge of the rest of the line (including
			   the newline) if the tty has been put back in
			   canonical mode (for example by a shell after a
			   SIGTSTP signal). */
			__fpurge(stdin);
#endif
			printf("\n");
		}
		else if (c != '\n')
			while ((c = fgetc(stdin)) != '\n' && c != EOF);
	}
	buf[1] = '\0';
	if (rpmatch(buf) == RPMATCH_YES)
		return 0;
	else
		return 1;
}

static int do_symlink(char *from, char *to, char *s, int verbose, int noact,
                      int nooverwrite, int interactive)
{
	char *newname = NULL, *target = NULL;
	int ret = 1;
	struct stat sb;

	if ( faccessat(AT_FDCWD, s, F_OK, AT_SYMLINK_NOFOLLOW) != 0 &&
	     errno != EINVAL )
	   /* Skip if AT_SYMLINK_NOFOLLOW is not supported; lstat() below will
	      detect the access error */
	{
		warn(_("%s: not accessible"), s);
		return 2;
	}

	if (lstat(s, &sb) == -1) {
		warn(_("stat of %s failed"), s);
		return 2;
	}
	if (!S_ISLNK(sb.st_mode)) {
		warnx(_("%s: not a symbolic link"), s);
		return 2;
	}
	target = xmalloc(sb.st_size + 1);
	if (readlink(s, target, sb.st_size + 1) < 0) {
		warn(_("%s: readlink failed"), s);
		free(target);
		return 2;
	}
	target[sb.st_size] = '\0';
	if (string_replace(from, to, target, target, &newname) != 0)
		ret = 0;

	if (ret == 1 && (nooverwrite || interactive) && lstat(newname, &sb) != 0)
		nooverwrite = interactive = 0;

	if ( ret == 1 &&
	     (nooverwrite || (interactive && (noact || ask(newname) != 0))) )
	{
		if (verbose)
			printf(_("Skipping existing link: `%s' -> `%s'\n"), s, target);
		ret = 0;
	}

	if (ret == 1) {
		if (!noact && 0 > unlink(s)) {
			warn(_("%s: unlink failed"), s);
			ret = 2;
		}
		else if (!noact && symlink(newname, s) != 0) {
			warn(_("%s: symlinking to %s failed"), s, newname);
			ret = 2;
		}
	}
	if (verbose && (noact || ret == 1))
		printf("%s: `%s' -> `%s'\n", s, target, newname);
	free(newname);
	free(target);
	return ret;
}

static int do_file(char *from, char *to, char *s, int verbose, int noact,
                   int nooverwrite, int interactive)
{
	char *newname = NULL, *file=NULL;
	int ret = 1;

	if (access(s, F_OK) != 0) {
		warn(_("%s: not accessible"), s);
		return 2;
	}

	if (strchr(from, '/') == NULL && strchr(to, '/') == NULL)
		file = strrchr(s, '/');
	if (file == NULL)
		file = s;
	if (string_replace(from, to, file, s, &newname) != 0)
		return 0;

	if ((nooverwrite || interactive) && access(newname, F_OK) != 0)
		nooverwrite = interactive = 0;

	if (nooverwrite || (interactive && (noact || ask(newname) != 0))) {
		if (verbose)
			printf(_("Skipping existing file: `%s'\n"), newname);
		ret = 0;
	}
	else if (!noact && rename(s, newname) != 0) {
		warn(_("%s: rename to %s failed"), s, newname);
		ret = 2;
	}
	if (verbose && (noact || ret == 1))
		printf("`%s' -> `%s'\n", s, newname);
	free(newname);
	return ret;
}

static void __attribute__((__noreturn__)) usage(void)
{
	FILE *out = stdout;
	fputs(USAGE_HEADER, out);
	fprintf(out,
	      _(" %s [options] <expression> <replacement> <file>...\n"),
		program_invocation_short_name);

	fputs(USAGE_SEPARATOR, out);
	fputs(_("Rename files.\n"), out);

	fputs(USAGE_OPTIONS, out);
	fputs(_(" -v, --verbose       explain what is being done\n"), out);
	fputs(_(" -s, --symlink       act on the target of symlinks\n"), out);
	fputs(_(" -n, --no-act        do not make any changes\n"), out);
	fputs(_(" -o, --no-overwrite  don't overwrite existing files\n"), out);
	fputs(_(" -i, --interactive   prompt before overwrite\n"), out);
	fputs(USAGE_SEPARATOR, out);
	printf(USAGE_HELP_OPTIONS(21));
	printf(USAGE_MAN_TAIL("rename(1)"));
	exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
	char *from, *to;
	int i, c, ret = 0, verbose = 0, noact = 0, nooverwrite = 0, interactive = 0;
	struct termios tio;
	int (*do_rename)(char *from, char *to, char *s, int verbose, int noact,
	                 int nooverwrite, int interactive) = do_file;

	static const struct option longopts[] = {
		{"verbose", no_argument, NULL, 'v'},
		{"version", no_argument, NULL, 'V'},
		{"help", no_argument, NULL, 'h'},
		{"no-act", no_argument, NULL, 'n'},
		{"no-overwrite", no_argument, NULL, 'o'},
		{"interactive", no_argument, NULL, 'i'},
		{"symlink", no_argument, NULL, 's'},
		{NULL, 0, NULL, 0}
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	close_stdout_atexit();

	while ((c = getopt_long(argc, argv, "vsVhnoi", longopts, NULL)) != -1)
		switch (c) {
		case 'n':
			noact = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		case 'o':
			nooverwrite = 1;
			interactive = 0;
			break;
		case 'i':
			interactive = 1;
			nooverwrite = 0;
			break;
		case 's':
			do_rename = do_symlink;
			break;

		case 'V':
			print_version(EXIT_SUCCESS);
		case 'h':
			usage();
		default:
			errtryhelp(EXIT_FAILURE);
		}

	argc -= optind;
	argv += optind;

	if (argc < 3) {
		warnx(_("not enough arguments"));
		errtryhelp(EXIT_FAILURE);
	}

	from = argv[0];
	to = argv[1];

	if (!strcmp(from, to))
		return RENAME_EXIT_NOTHING;

	tty_cbreak = 0;
	if (interactive && isatty(STDIN_FILENO) != 0) {
		if (tcgetattr(STDIN_FILENO, &tio) != 0)
			warn(_("failed to get terminal attributes"));
		else if (!(tio.c_lflag & ICANON) && tio.c_cc[VMIN] == 1)
			tty_cbreak = 1;
	}

	for (i = 2; i < argc; i++)
		ret |= do_rename(from, to, argv[i], verbose, noact, nooverwrite, interactive);

	switch (ret) {
	case 0:
		return RENAME_EXIT_NOTHING;
	case 1:
		return EXIT_SUCCESS;
	case 2:
		return EXIT_FAILURE;
	case 3:
		return RENAME_EXIT_SOMEOK;
	default:
		return RENAME_EXIT_UNEXPLAINED;
	}
}