mv.c revision 1.35 1 /* $NetBSD: mv.c,v 1.35 2005/06/03 13:55:04 hubertf 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.35 2005/06/03 13:55:04 hubertf 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 baselen, ch, len, rval;
80 char *p, *endp;
81 struct stat sb;
82 char path[MAXPATHLEN + 1];
83
84 setprogname(argv[0]);
85 (void)setlocale(LC_ALL, "");
86
87 while ((ch = getopt(argc, argv, "ifv")) != -1)
88 switch (ch) {
89 case 'i':
90 fflg = 0;
91 iflg = 1;
92 break;
93 case 'f':
94 iflg = 0;
95 fflg = 1;
96 break;
97 case 'v':
98 vflg = 1;
99 break;
100 case '?':
101 default:
102 usage();
103 }
104 argc -= optind;
105 argv += optind;
106
107 if (argc < 2)
108 usage();
109
110 stdin_ok = isatty(STDIN_FILENO);
111
112 /*
113 * If the stat on the target fails or the target isn't a directory,
114 * try the move. More than 2 arguments is an error in this case.
115 */
116 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
117 if (argc > 2)
118 usage();
119 exit(do_move(argv[0], argv[1]));
120 }
121
122 /* It's a directory, move each file into it. */
123 (void)strlcpy(path, argv[argc - 1], sizeof(path));
124 baselen = strlen(path);
125 endp = &path[baselen];
126 *endp++ = '/';
127 ++baselen;
128 for (rval = 0; --argc; ++argv) {
129 p = *argv + strlen(*argv) - 1;
130 while (*p == '/' && p != *argv)
131 *p-- = '\0';
132 if ((p = strrchr(*argv, '/')) == NULL)
133 p = *argv;
134 else
135 ++p;
136
137 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
138 warnx("%s: destination pathname too long", *argv);
139 rval = 1;
140 } else {
141 memmove(endp, p, len + 1);
142 if (do_move(*argv, path))
143 rval = 1;
144 }
145 }
146 exit(rval);
147 /* NOTREACHED */
148 }
149
150 int
151 do_move(char *from, char *to)
152 {
153 struct stat sb;
154 char modep[15];
155
156 /*
157 * (1) If the destination path exists, the -f option is not specified
158 * and either of the following conditions are true:
159 *
160 * (a) The permissions of the destination path do not permit
161 * writing and the standard input is a terminal.
162 * (b) The -i option is specified.
163 *
164 * the mv utility shall write a prompt to standard error and
165 * read a line from standard input. If the response is not
166 * affirmative, mv shall do nothing more with the current
167 * source file...
168 */
169 if (!fflg && !access(to, F_OK)) {
170 int ask = 1;
171 int ch;
172
173 if (iflg) {
174 if (access(from, F_OK)) {
175 warn("rename %s", from);
176 return (1);
177 }
178 (void)fprintf(stderr, "overwrite %s? ", to);
179 } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
180 if (access(from, F_OK)) {
181 warn("rename %s", from);
182 return (1);
183 }
184 strmode(sb.st_mode, modep);
185 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
186 modep + 1, modep[9] == ' ' ? "" : " ",
187 user_from_uid(sb.st_uid, 0),
188 group_from_gid(sb.st_gid, 0), to);
189 } else
190 ask = 0;
191 if (ask) {
192 if ((ch = getchar()) != EOF && ch != '\n')
193 while (getchar() != '\n');
194 if (ch != 'y' && ch != 'Y')
195 return (0);
196 }
197 }
198
199 /*
200 * (2) If rename() succeeds, mv shall do nothing more with the
201 * current source file. If it fails for any other reason than
202 * EXDEV, mv shall write a diagnostic message to the standard
203 * error and do nothing more with the current source file.
204 *
205 * (3) If the destination path exists, and it is a file of type
206 * directory and source_file is not a file of type directory,
207 * or it is a file not of type directory, and source file is
208 * a file of type directory, mv shall write a diagnostic
209 * message to standard error, and do nothing more with the
210 * current source file...
211 */
212 if (!rename(from, to)) {
213 if (vflg)
214 printf("%s -> %s\n", from, to);
215 return (0);
216 }
217
218 if (errno != EXDEV) {
219 warn("rename %s to %s", from, to);
220 return (1);
221 }
222
223 /*
224 * (4) If the destination path exists, mv shall attempt to remove it.
225 * If this fails for any reason, mv shall write a diagnostic
226 * message to the standard error and do nothing more with the
227 * current source file...
228 */
229 if (!lstat(to, &sb)) {
230 if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
231 warn("can't remove %s", to);
232 return (1);
233 }
234 }
235
236 /*
237 * (5) The file hierarchy rooted in source_file shall be duplicated
238 * as a file hierarchy rooted in the destination path...
239 */
240 if (lstat(from, &sb)) {
241 warn("%s", from);
242 return (1);
243 }
244
245 return (S_ISREG(sb.st_mode) ?
246 fastcopy(from, to, &sb) : copy(from, to));
247 }
248
249 int
250 fastcopy(char *from, char *to, struct stat *sbp)
251 {
252 struct timeval tval[2];
253 static u_int blen;
254 static char *bp;
255 int nread, from_fd, to_fd;
256
257 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
258 warn("%s", from);
259 return (1);
260 }
261 if ((to_fd =
262 open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
263 warn("%s", to);
264 (void)close(from_fd);
265 return (1);
266 }
267 if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
268 warn(NULL);
269 return (1);
270 }
271 while ((nread = read(from_fd, bp, blen)) > 0)
272 if (write(to_fd, bp, nread) != nread) {
273 warn("%s", to);
274 goto err;
275 }
276 if (nread < 0) {
277 warn("%s", from);
278 err: if (unlink(to))
279 warn("%s: remove", to);
280 (void)close(from_fd);
281 (void)close(to_fd);
282 return (1);
283 }
284 (void)close(from_fd);
285 #ifdef BSD4_4
286 TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
287 TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
288 #else
289 tval[0].tv_sec = sbp->st_atime;
290 tval[1].tv_sec = sbp->st_mtime;
291 tval[0].tv_usec = 0;
292 tval[1].tv_usec = 0;
293 #endif
294 #ifdef __SVR4
295 if (utimes(to, tval))
296 #else
297 if (futimes(to_fd, tval))
298 #endif
299 warn("%s: set times", to);
300 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
301 if (errno != EPERM)
302 warn("%s: set owner/group", to);
303 sbp->st_mode &= ~(S_ISUID | S_ISGID);
304 }
305 if (fchmod(to_fd, sbp->st_mode))
306 warn("%s: set mode", to);
307 if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP))
308 warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
309
310 if (close(to_fd)) {
311 warn("%s", to);
312 return (1);
313 }
314
315 if (unlink(from)) {
316 warn("%s: remove", from);
317 return (1);
318 }
319
320 if (vflg)
321 printf("%s -> %s\n", from, to);
322
323 return (0);
324 }
325
326 int
327 copy(char *from, char *to)
328 {
329 int pid, status;
330
331 if ((pid = vfork()) == 0) {
332 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", from, to, NULL);
333 warn("%s", _PATH_CP);
334 _exit(1);
335 }
336 if (waitpid(pid, &status, 0) == -1) {
337 warn("%s: waitpid", _PATH_CP);
338 return (1);
339 }
340 if (!WIFEXITED(status)) {
341 warnx("%s: did not terminate normally", _PATH_CP);
342 return (1);
343 }
344 if (WEXITSTATUS(status)) {
345 warnx("%s: terminated with %d (non-zero) status",
346 _PATH_CP, WEXITSTATUS(status));
347 return (1);
348 }
349 if (!(pid = vfork())) {
350 execl(_PATH_RM, "mv", "-rf", from, NULL);
351 warn("%s", _PATH_RM);
352 _exit(1);
353 }
354 if (waitpid(pid, &status, 0) == -1) {
355 warn("%s: waitpid", _PATH_RM);
356 return (1);
357 }
358 if (!WIFEXITED(status)) {
359 warnx("%s: did not terminate normally", _PATH_RM);
360 return (1);
361 }
362 if (WEXITSTATUS(status)) {
363 warnx("%s: terminated with %d (non-zero) status",
364 _PATH_RM, WEXITSTATUS(status));
365 return (1);
366 }
367 return (0);
368 }
369
370 void
371 usage(void)
372 {
373 (void)fprintf(stderr, "usage: %s [-fiv] source target\n"
374 " %s [-fiv] source ... directory\n", getprogname(),
375 getprogname());
376 exit(1);
377 /* NOTREACHED */
378 }
379