mv.c revision 1.38 1 /* $NetBSD: mv.c,v 1.38 2007/02/15 09:57:16 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ken Smith of The State University of New York at Buffalo.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
44 #else
45 __RCSID("$NetBSD: mv.c,v 1.38 2007/02/15 09:57:16 rillig Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <sys/wait.h>
52 #include <sys/stat.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <grp.h>
58 #include <locale.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 #include "pathnames.h"
66
67 int fflg, iflg, vflg;
68 int stdin_ok;
69
70 int copy(char *, char *);
71 int do_move(char *, char *);
72 int fastcopy(char *, char *, struct stat *);
73 void usage(void);
74 int main(int, char *[]);
75
76 int
77 main(int argc, char *argv[])
78 {
79 int ch, len, rval;
80 char *p, *endp;
81 struct stat sb;
82 char path[MAXPATHLEN + 1];
83 size_t baselen;
84
85 setprogname(argv[0]);
86 (void)setlocale(LC_ALL, "");
87
88 while ((ch = getopt(argc, argv, "ifv")) != -1)
89 switch (ch) {
90 case 'i':
91 fflg = 0;
92 iflg = 1;
93 break;
94 case 'f':
95 iflg = 0;
96 fflg = 1;
97 break;
98 case 'v':
99 vflg = 1;
100 break;
101 case '?':
102 default:
103 usage();
104 }
105 argc -= optind;
106 argv += optind;
107
108 if (argc < 2)
109 usage();
110
111 stdin_ok = isatty(STDIN_FILENO);
112
113 /*
114 * If the stat on the target fails or the target isn't a directory,
115 * try the move. More than 2 arguments is an error in this case.
116 */
117 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
118 if (argc > 2)
119 usage();
120 exit(do_move(argv[0], argv[1]));
121 }
122
123 /* It's a directory, move each file into it. */
124 baselen = strlcpy(path, argv[argc - 1], sizeof(path));
125 if (baselen >= sizeof(path))
126 errx(1, "%s: destination pathname too long", argv[argc - 1]);
127 endp = &path[baselen];
128 if (!baselen || *(endp - 1) != '/') {
129 *endp++ = '/';
130 ++baselen;
131 }
132 for (rval = 0; --argc; ++argv) {
133 p = *argv + strlen(*argv) - 1;
134 while (*p == '/' && p != *argv)
135 *p-- = '\0';
136 if ((p = strrchr(*argv, '/')) == NULL)
137 p = *argv;
138 else
139 ++p;
140
141 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
142 warnx("%s: destination pathname too long", *argv);
143 rval = 1;
144 } else {
145 memmove(endp, p, len + 1);
146 if (do_move(*argv, path))
147 rval = 1;
148 }
149 }
150 exit(rval);
151 /* NOTREACHED */
152 }
153
154 int
155 do_move(char *from, char *to)
156 {
157 struct stat sb;
158 char modep[15];
159
160 /*
161 * (1) If the destination path exists, the -f option is not specified
162 * and either of the following conditions are true:
163 *
164 * (a) The permissions of the destination path do not permit
165 * writing and the standard input is a terminal.
166 * (b) The -i option is specified.
167 *
168 * the mv utility shall write a prompt to standard error and
169 * read a line from standard input. If the response is not
170 * affirmative, mv shall do nothing more with the current
171 * source file...
172 */
173 if (!fflg && !access(to, F_OK)) {
174 int ask = 1;
175 int ch;
176
177 if (iflg) {
178 if (access(from, F_OK)) {
179 warn("rename %s", from);
180 return (1);
181 }
182 (void)fprintf(stderr, "overwrite %s? ", to);
183 } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
184 if (access(from, F_OK)) {
185 warn("rename %s", from);
186 return (1);
187 }
188 strmode(sb.st_mode, modep);
189 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
190 modep + 1, modep[9] == ' ' ? "" : " ",
191 user_from_uid(sb.st_uid, 0),
192 group_from_gid(sb.st_gid, 0), to);
193 } else
194 ask = 0;
195 if (ask) {
196 if ((ch = getchar()) != EOF && ch != '\n') {
197 int ch2;
198 while ((ch2 = getchar()) != EOF && ch2 != '\n')
199 continue;
200 }
201 if (ch != 'y' && ch != 'Y')
202 return (0);
203 }
204 }
205
206 /*
207 * (2) If rename() succeeds, mv shall do nothing more with the
208 * current source file. If it fails for any other reason than
209 * EXDEV, mv shall write a diagnostic message to the standard
210 * error and do nothing more with the current source file.
211 *
212 * (3) If the destination path exists, and it is a file of type
213 * directory and source_file is not a file of type directory,
214 * or it is a file not of type directory, and source file is
215 * a file of type directory, mv shall write a diagnostic
216 * message to standard error, and do nothing more with the
217 * current source file...
218 */
219 if (!rename(from, to)) {
220 if (vflg)
221 printf("%s -> %s\n", from, to);
222 return (0);
223 }
224
225 if (errno != EXDEV) {
226 warn("rename %s to %s", from, to);
227 return (1);
228 }
229
230 /*
231 * (4) If the destination path exists, mv shall attempt to remove it.
232 * If this fails for any reason, mv shall write a diagnostic
233 * message to the standard error and do nothing more with the
234 * current source file...
235 */
236 if (!lstat(to, &sb)) {
237 if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
238 warn("can't remove %s", to);
239 return (1);
240 }
241 }
242
243 /*
244 * (5) The file hierarchy rooted in source_file shall be duplicated
245 * as a file hierarchy rooted in the destination path...
246 */
247 if (lstat(from, &sb)) {
248 warn("%s", from);
249 return (1);
250 }
251
252 return (S_ISREG(sb.st_mode) ?
253 fastcopy(from, to, &sb) : copy(from, to));
254 }
255
256 int
257 fastcopy(char *from, char *to, struct stat *sbp)
258 {
259 struct timeval tval[2];
260 static u_int blen;
261 static char *bp;
262 int nread, from_fd, to_fd;
263
264 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
265 warn("%s", from);
266 return (1);
267 }
268 if ((to_fd =
269 open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
270 warn("%s", to);
271 (void)close(from_fd);
272 return (1);
273 }
274 if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
275 warn(NULL);
276 return (1);
277 }
278 while ((nread = read(from_fd, bp, blen)) > 0)
279 if (write(to_fd, bp, nread) != nread) {
280 warn("%s", to);
281 goto err;
282 }
283 if (nread < 0) {
284 warn("%s", from);
285 err: if (unlink(to))
286 warn("%s: remove", to);
287 (void)close(from_fd);
288 (void)close(to_fd);
289 return (1);
290 }
291 (void)close(from_fd);
292 #ifdef BSD4_4
293 TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
294 TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
295 #else
296 tval[0].tv_sec = sbp->st_atime;
297 tval[1].tv_sec = sbp->st_mtime;
298 tval[0].tv_usec = 0;
299 tval[1].tv_usec = 0;
300 #endif
301 #ifdef __SVR4
302 if (utimes(to, tval))
303 #else
304 if (futimes(to_fd, tval))
305 #endif
306 warn("%s: set times", to);
307 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
308 if (errno != EPERM)
309 warn("%s: set owner/group", to);
310 sbp->st_mode &= ~(S_ISUID | S_ISGID);
311 }
312 if (fchmod(to_fd, sbp->st_mode))
313 warn("%s: set mode", to);
314 if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP))
315 warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
316
317 if (close(to_fd)) {
318 warn("%s", to);
319 return (1);
320 }
321
322 if (unlink(from)) {
323 warn("%s: remove", from);
324 return (1);
325 }
326
327 if (vflg)
328 printf("%s -> %s\n", from, to);
329
330 return (0);
331 }
332
333 int
334 copy(char *from, char *to)
335 {
336 int pid, status;
337
338 if ((pid = vfork()) == 0) {
339 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to, NULL);
340 warn("%s", _PATH_CP);
341 _exit(1);
342 }
343 if (waitpid(pid, &status, 0) == -1) {
344 warn("%s: waitpid", _PATH_CP);
345 return (1);
346 }
347 if (!WIFEXITED(status)) {
348 warnx("%s: did not terminate normally", _PATH_CP);
349 return (1);
350 }
351 if (WEXITSTATUS(status)) {
352 warnx("%s: terminated with %d (non-zero) status",
353 _PATH_CP, WEXITSTATUS(status));
354 return (1);
355 }
356 if (!(pid = vfork())) {
357 execl(_PATH_RM, "mv", "-rf", "--", from, NULL);
358 warn("%s", _PATH_RM);
359 _exit(1);
360 }
361 if (waitpid(pid, &status, 0) == -1) {
362 warn("%s: waitpid", _PATH_RM);
363 return (1);
364 }
365 if (!WIFEXITED(status)) {
366 warnx("%s: did not terminate normally", _PATH_RM);
367 return (1);
368 }
369 if (WEXITSTATUS(status)) {
370 warnx("%s: terminated with %d (non-zero) status",
371 _PATH_RM, WEXITSTATUS(status));
372 return (1);
373 }
374 return (0);
375 }
376
377 void
378 usage(void)
379 {
380 (void)fprintf(stderr, "usage: %s [-fiv] source target\n"
381 " %s [-fiv] source ... directory\n", getprogname(),
382 getprogname());
383 exit(1);
384 /* NOTREACHED */
385 }
386