mv.c revision 1.23 1 /* $NetBSD: mv.c,v 1.23 1999/11/09 15:06:31 drochner 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
42 The Regents of the University of California. All rights reserved.\n");
43 #endif /* not lint */
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
48 #else
49 __RCSID("$NetBSD: mv.c,v 1.23 1999/11/09 15:06:31 drochner Exp $");
50 #endif
51 #endif /* not lint */
52
53 #include <sys/param.h>
54 #include <sys/time.h>
55 #include <sys/wait.h>
56 #include <sys/stat.h>
57
58 #include <err.h>
59 #include <errno.h>
60 #include <fcntl.h>
61 #include <locale.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include <pwd.h>
67 #include <grp.h>
68
69 #include "pathnames.h"
70
71 int fflg, iflg;
72 int stdin_ok;
73
74 int copy __P((char *, char *));
75 int do_move __P((char *, char *));
76 int fastcopy __P((char *, char *, struct stat *));
77 void usage __P((void));
78 int main __P((int, char *[]));
79
80 int
81 main(argc, argv)
82 int argc;
83 char *argv[];
84 {
85 int baselen, len, rval;
86 char *p, *endp;
87 struct stat sb;
88 int ch;
89 char path[MAXPATHLEN + 1];
90
91 (void)setlocale(LC_ALL, "");
92
93 while ((ch = getopt(argc, argv, "if")) != -1)
94 switch (ch) {
95 case 'i':
96 fflg = 0;
97 iflg = 1;
98 break;
99 case 'f':
100 iflg = 0;
101 fflg = 1;
102 break;
103 case '?':
104 default:
105 usage();
106 }
107 argc -= optind;
108 argv += optind;
109
110 if (argc < 2)
111 usage();
112
113 stdin_ok = isatty(STDIN_FILENO);
114
115 /*
116 * If the stat on the target fails or the target isn't a directory,
117 * try the move. More than 2 arguments is an error in this case.
118 */
119 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
120 if (argc > 2)
121 usage();
122 exit(do_move(argv[0], argv[1]));
123 }
124
125 /* It's a directory, move each file into it. */
126 (void)strcpy(path, argv[argc - 1]);
127 baselen = strlen(path);
128 endp = &path[baselen];
129 *endp++ = '/';
130 ++baselen;
131 for (rval = 0; --argc; ++argv) {
132 p = *argv + strlen(*argv) - 1;
133 while (*p == '/' && p != *argv)
134 *p-- = '\0';
135 if ((p = strrchr(*argv, '/')) == NULL)
136 p = *argv;
137 else
138 ++p;
139
140 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
141 warnx("%s: destination pathname too long", *argv);
142 rval = 1;
143 } else {
144 memmove(endp, p, len + 1);
145 if (do_move(*argv, path))
146 rval = 1;
147 }
148 }
149 exit(rval);
150 /* NOTREACHED */
151 }
152
153 int
154 do_move(from, to)
155 char *from, *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 perimissions 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 while (getchar() != '\n');
198 if (ch != 'y' && ch != 'Y')
199 return (0);
200 }
201 }
202
203 /*
204 * (2) If rename() succeeds, mv shall do nothing more with the
205 * current source file. If it fails for any other reason than
206 * EXDEV, mv shall write a diagnostic message to the standard
207 * error and do nothing more with the current source file.
208 *
209 * (3) If the destination path exists, and it is a file of type
210 * directory and source_file is not a file of type directory,
211 * or it is a file not of type directory, and source file is
212 * a file of type directory, mv shall write a diagnostic
213 * message to standard error, and do nothing more with the
214 * current source file...
215 */
216 if (!rename(from, to))
217 return (0);
218
219 if (errno != EXDEV) {
220 warn("rename %s to %s", from, to);
221 return (1);
222 }
223
224 /*
225 * (4) If the destination path exists, mv shall attempt to remove it.
226 * If this fails for any reason, mv shall write a diagnostic
227 * message to the standard error and do nothing more with the
228 * current source file...
229 */
230 if (!lstat(to, &sb)) {
231 if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
232 warn("can't remove %s", to);
233 return (1);
234 }
235 }
236
237 /*
238 * (5) The file hierarchy rooted in source_file shall be duplicated
239 * as a file hierarchy rooted in the destination path...
240 */
241 if (lstat(from, &sb)) {
242 warn("%s", from);
243 return (1);
244 }
245 return (S_ISREG(sb.st_mode) ?
246 fastcopy(from, to, &sb) : copy(from, to));
247 }
248
249 int
250 fastcopy(from, to, sbp)
251 char *from, *to;
252 struct stat *sbp;
253 {
254 struct timeval tval[2];
255 static u_int blen;
256 static char *bp;
257 int nread, from_fd, to_fd;
258
259 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
260 warn("%s", from);
261 return (1);
262 }
263 if ((to_fd =
264 open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
265 warn("%s", to);
266 (void)close(from_fd);
267 return (1);
268 }
269 if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
270 warn(NULL);
271 return (1);
272 }
273 while ((nread = read(from_fd, bp, blen)) > 0)
274 if (write(to_fd, bp, nread) != nread) {
275 warn("%s", to);
276 goto err;
277 }
278 if (nread < 0) {
279 warn("%s", from);
280 err: if (unlink(to))
281 warn("%s: remove", to);
282 (void)close(from_fd);
283 (void)close(to_fd);
284 return (1);
285 }
286 (void)close(from_fd);
287 #ifdef BSD4_4
288 TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
289 TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
290 #else
291 tval[0].tv_sec = sbp->st_atime;
292 tval[1].tv_sec = sbp->st_mtime;
293 tval[0].tv_usec = 0;
294 tval[1].tv_usec = 0;
295 #endif
296 #ifdef __SVR4
297 if (utimes(to, tval))
298 #else
299 if (futimes(to_fd, tval))
300 #endif
301 warn("%s: set times", to);
302 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
303 if (errno != EPERM)
304 warn("%s: set owner/group", to);
305 sbp->st_mode &= ~(S_ISUID | S_ISGID);
306 }
307 if (fchmod(to_fd, sbp->st_mode))
308 warn("%s: set mode", to);
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 return (0);
320 }
321
322 int
323 copy(from, to)
324 char *from, *to;
325 {
326 int pid, status;
327
328 if ((pid = vfork()) == 0) {
329 execl(_PATH_CP, "mv", "-PRp", from, to, NULL);
330 warn("%s", _PATH_CP);
331 _exit(1);
332 }
333 if (waitpid(pid, &status, 0) == -1) {
334 warn("%s: waitpid", _PATH_CP);
335 return (1);
336 }
337 if (!WIFEXITED(status)) {
338 warn("%s: did not terminate normally", _PATH_CP);
339 return (1);
340 }
341 if (WEXITSTATUS(status)) {
342 warn("%s: terminated with %d (non-zero) status",
343 _PATH_CP, WEXITSTATUS(status));
344 return (1);
345 }
346 if (!(pid = vfork())) {
347 execl(_PATH_RM, "mv", "-rf", from, NULL);
348 warn("%s", _PATH_RM);
349 _exit(1);
350 }
351 if (waitpid(pid, &status, 0) == -1) {
352 warn("%s: waitpid", _PATH_RM);
353 return (1);
354 }
355 if (!WIFEXITED(status)) {
356 warn("%s: did not terminate normally", _PATH_RM);
357 return (1);
358 }
359 if (WEXITSTATUS(status)) {
360 warn("%s: terminated with %d (non-zero) status",
361 _PATH_RM, WEXITSTATUS(status));
362 return (1);
363 }
364 return (0);
365 }
366
367 void
368 usage()
369 {
370
371 (void)fprintf(stderr, "usage: mv [-fi] source target\n");
372 (void)fprintf(stderr, " mv [-fi] source ... directory\n");
373 exit(1);
374 /* NOTREACHED */
375 }
376