]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/rename.c
misc: never use usage(stderr)
[thirdparty/util-linux.git] / misc-utils / rename.c
CommitLineData
eb63b9b8
KZ
1/*
2 * rename.c - aeb 2000-01-01
3 *
4--------------------------------------------------------------
5#!/bin/sh
6if [ $# -le 2 ]; then echo call: rename from to files; exit; fi
7FROM="$1"
8TO="$2"
9shift
10shift
11for 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#include <string.h>
18#include <stdlib.h>
19#include <errno.h>
d200a926 20#include <getopt.h>
5a2a8177
JM
21#include <unistd.h>
22#include <sys/types.h>
23#include <sys/stat.h>
87f3feac 24
eb63b9b8 25#include "nls.h"
87f3feac 26#include "xalloc.h"
d200a926 27#include "c.h"
c05a80ca 28#include "closestream.h"
eb63b9b8 29
d6cf9e16
SK
30#define RENAME_EXIT_SOMEOK 2
31#define RENAME_EXIT_NOTHING 4
32#define RENAME_EXIT_UNEXPLAINED 64
33
5651128a 34static int string_replace(char *from, char *to, char *s, char *orig, char **newname)
d200a926 35{
5651128a
SK
36 char *p, *q, *where;
37
38 where = strstr(s, from);
39 if (where == NULL)
40 return 1;
41 p = orig;
42 *newname = xmalloc(strlen(orig) + strlen(to) + 1);
43 q = *newname;
eb63b9b8
KZ
44 while (p < where)
45 *q++ = *p++;
46 p = to;
47 while (*p)
48 *q++ = *p++;
5651128a 49 p = where + strlen(from);
eb63b9b8
KZ
50 while (*p)
51 *q++ = *p++;
22853e4a 52 *q = 0;
5651128a
SK
53 return 0;
54}
eb63b9b8 55
9a838c3c 56static int do_symlink(char *from, char *to, char *s, int verbose, int noact, int nooverwrite)
5651128a
SK
57{
58 char *newname = NULL, *target = NULL;
59 int ret = 1;
60 struct stat sb;
61
62 if (lstat(s, &sb) == -1) {
fc14ceba 63 warn(_("stat of %s failed"), s);
5651128a
SK
64 return 2;
65 }
66 if (!S_ISLNK(sb.st_mode)) {
67 warnx(_("%s: not a symbolic link"), s);
68 return 2;
69 }
70 target = xmalloc(sb.st_size + 1);
71 if (readlink(s, target, sb.st_size + 1) < 0) {
72 warn(_("%s: readlink failed"), s);
73 free(target);
74 return 2;
75 }
76 target[sb.st_size] = '\0';
77 if (string_replace(from, to, target, target, &newname))
78 ret = 0;
fabb9067 79
9a838c3c 80 if (ret == 1 && nooverwrite && lstat(newname, &sb) == 0) {
fabb9067
DG
81 if (verbose)
82 printf(_("Skipping existing link: `%s'\n"), newname);
83
84 ret = 0;
85 }
86
87 if (ret == 1) {
88 if (!noact && 0 > unlink(s)) {
89 warn(_("%s: unlink failed"), s);
90 ret = 2;
91 } else if (!noact && symlink(newname, s) != 0) {
92 warn(_("%s: symlinking to %s failed"), s, newname);
93 ret = 2;
94 }
5a2a8177 95 }
990bf1f0 96 if (verbose && (noact || ret == 1))
fabb9067
DG
97 if (verbose)
98 printf("%s: `%s' -> `%s'\n", s, target, newname);
d200a926 99 free(newname);
23b4715b 100 free(target);
d6cf9e16 101 return ret;
eb63b9b8
KZ
102}
103
9a838c3c 104static int do_file(char *from, char *to, char *s, int verbose, int noact, int nooverwrite)
5651128a 105{
9fa6088a 106 char *newname = NULL, *file=NULL;
5651128a 107 int ret = 1;
fabb9067 108 struct stat sb;
5651128a 109
9fa6088a
AH
110 if (strchr(from, '/') == NULL && strchr(to, '/') == NULL)
111 file = strrchr(s, '/');
5651128a
SK
112 if (file == NULL)
113 file = s;
114 if (string_replace(from, to, file, s, &newname))
115 return 0;
9a838c3c 116 if (nooverwrite && stat(newname, &sb) == 0) {
fabb9067
DG
117 printf(_("Skipping existing file: `%s'\n"), newname);
118 ret = 0;
119 }
990bf1f0 120 else if (!noact && rename(s, newname) != 0) {
5651128a
SK
121 warn(_("%s: rename to %s failed"), s, newname);
122 ret = 2;
123 }
990bf1f0 124 if (verbose && (noact || ret == 1))
5651128a
SK
125 printf("`%s' -> `%s'\n", s, newname);
126 free(newname);
127 return ret;
128}
129
6e1eda6f 130static void __attribute__((__noreturn__)) usage(void)
d200a926 131{
6e1eda6f 132 FILE *out = stdout;
540dfebe 133 fputs(USAGE_HEADER, out);
d200a926 134 fprintf(out,
09af3db4 135 _(" %s [options] <expression> <replacement> <file>...\n"),
d200a926 136 program_invocation_short_name);
451dbcfa
BS
137
138 fputs(USAGE_SEPARATOR, out);
139 fputs(_("Rename files.\n"), out);
140
540dfebe 141 fputs(USAGE_OPTIONS, out);
9a838c3c
DG
142 fputs(_(" -v, --verbose explain what is being done\n"), out);
143 fputs(_(" -s, --symlink act on the target of symlinks\n"), out);
144 fputs(_(" -n, --no-act do not make any changes\n"), out);
145 fputs(_(" -o, --no-overwrite don't overwrite existing files\n"), out);
540dfebe
SK
146 fputs(USAGE_SEPARATOR, out);
147 fputs(USAGE_HELP, out);
148 fputs(USAGE_VERSION, out);
149 fprintf(out, USAGE_MAN_TAIL("rename(1)"));
6e1eda6f 150 exit(EXIT_SUCCESS);
d200a926 151}
eb63b9b8 152
d200a926
SK
153int main(int argc, char **argv)
154{
155 char *from, *to;
9a838c3c
DG
156 int i, c, ret = 0, verbose = 0, noact = 0, nooverwrite = 0;
157 int (*do_rename)(char *from, char *to, char *s, int verbose, int noact, int nooverwrite) = do_file;
d200a926
SK
158
159 static const struct option longopts[] = {
160 {"verbose", no_argument, NULL, 'v'},
161 {"version", no_argument, NULL, 'V'},
162 {"help", no_argument, NULL, 'h'},
990bf1f0 163 {"no-act", no_argument, NULL, 'n'},
9a838c3c 164 {"no-overwrite", no_argument, NULL, 'o'},
5a2a8177 165 {"symlink", no_argument, NULL, 's'},
d200a926
SK
166 {NULL, 0, NULL, 0}
167 };
eb63b9b8
KZ
168
169 setlocale(LC_ALL, "");
170 bindtextdomain(PACKAGE, LOCALEDIR);
171 textdomain(PACKAGE);
c05a80ca 172 atexit(close_stdout);
eb63b9b8 173
fabb9067 174 while ((c = getopt_long(argc, argv, "vsVhno", longopts, NULL)) != -1)
d200a926 175 switch (c) {
990bf1f0
AR
176 case 'n':
177 noact = 1;
8c1ce08d 178 /* fallthrough */
fabb9067 179 case 'o':
9a838c3c 180 nooverwrite = 1;
fabb9067 181 break;
8c1ce08d
SK
182 case 'v':
183 verbose = 1;
990bf1f0 184 break;
5a2a8177 185 case 's':
5651128a 186 do_rename = do_symlink;
5a2a8177 187 break;
d200a926 188 case 'V':
c717b032 189 printf(UTIL_LINUX_VERSION);
d200a926
SK
190 return EXIT_SUCCESS;
191 case 'h':
6e1eda6f 192 usage();
d200a926 193 default:
677ec86c 194 errtryhelp(EXIT_FAILURE);
eb63b9b8 195 }
d200a926
SK
196
197 argc -= optind;
198 argv += optind;
eb63b9b8
KZ
199
200 if (argc < 3) {
8c219bf4 201 warnx(_("not enough arguments"));
6e1eda6f 202 errtryhelp(EXIT_FAILURE);
eb63b9b8
KZ
203 }
204
d200a926
SK
205 from = argv[0];
206 to = argv[1];
207
9f430c8a
SK
208 if (!strcmp(from, to))
209 return RENAME_EXIT_NOTHING;
210
d200a926 211 for (i = 2; i < argc; i++)
9a838c3c 212 ret |= do_rename(from, to, argv[i], verbose, noact, nooverwrite);
d6cf9e16
SK
213
214 switch (ret) {
215 case 0:
216 return RENAME_EXIT_NOTHING;
217 case 1:
218 return EXIT_SUCCESS;
219 case 2:
220 return EXIT_FAILURE;
221 case 3:
222 return RENAME_EXIT_SOMEOK;
223 default:
224 return RENAME_EXIT_UNEXPLAINED;
225 }
eb63b9b8 226}