mv.c revision 1.9 1 /* $NetBSD: mv.c,v 1.9 1995/03/21 09:06:52 cgd 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 #ifndef lint
40 static char copyright[] =
41 "@(#) 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 static char rcsid[] = "$NetBSD: mv.c,v 1.9 1995/03/21 09:06:52 cgd 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 <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #include "pathnames.h"
67
68 int fflg, iflg;
69 int stdin_ok;
70
71 int copy __P((char *, char *));
72 int do_move __P((char *, char *));
73 int fastcopy __P((char *, char *, struct stat *));
74 void usage __P((void));
75
76 int
77 main(argc, argv)
78 int argc;
79 char *argv[];
80 {
81 register int baselen, len, rval;
82 register char *p, *endp;
83 struct stat sb;
84 int ch;
85 char path[MAXPATHLEN + 1];
86
87 while ((ch = getopt(argc, argv, "if")) != -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 '?':
98 default:
99 usage();
100 }
101 argc -= optind;
102 argv += optind;
103
104 if (argc < 2)
105 usage();
106
107 stdin_ok = isatty(STDIN_FILENO);
108
109 /*
110 * If the stat on the target fails or the target isn't a directory,
111 * try the move. More than 2 arguments is an error in this case.
112 */
113 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
114 if (argc > 2)
115 usage();
116 exit(do_move(argv[0], argv[1]));
117 }
118
119 /* It's a directory, move each file into it. */
120 (void)strcpy(path, argv[argc - 1]);
121 baselen = strlen(path);
122 endp = &path[baselen];
123 *endp++ = '/';
124 ++baselen;
125 for (rval = 0; --argc; ++argv) {
126 if ((p = strrchr(*argv, '/')) == NULL)
127 p = *argv;
128 else
129 ++p;
130 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
131 warnx("%s: destination pathname too long", *argv);
132 rval = 1;
133 } else {
134 memmove(endp, p, len + 1);
135 if (do_move(*argv, path))
136 rval = 1;
137 }
138 }
139 exit(rval);
140 }
141
142 int
143 do_move(from, to)
144 char *from, *to;
145 {
146 struct stat sb;
147 char modep[15];
148
149 /*
150 * (1) If the destination path exists, the -f option is not specified
151 * and either of the following conditions are true:
152 *
153 * (a) The perimissions of the destination path do not permit
154 * writing and the standard input is a terminal.
155 * (b) The -i option is specified.
156 *
157 * the mv utility shall write a prompt to standard error and
158 * read a line from standard input. If the response is not
159 * affirmative, mv shall do nothing more with the current
160 * source file...
161 */
162 if (!fflg && !access(to, F_OK)) {
163 int ask = 1;
164 int ch;
165
166 if (iflg) {
167 (void)fprintf(stderr, "overwrite %s? ", to);
168 } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
169 strmode(sb.st_mode, modep);
170 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
171 modep + 1, modep[9] == ' ' ? "" : " ",
172 user_from_uid(sb.st_uid, 0),
173 group_from_gid(sb.st_gid, 0), to);
174 } else
175 ask = 0;
176 if (ask) {
177 if ((ch = getchar()) != EOF && ch != '\n')
178 while (getchar() != '\n');
179 if (ch != 'y' && ch != 'Y')
180 return (0);
181 }
182 }
183
184 /*
185 * (2) If rename() succeeds, mv shall do nothing more with the
186 * current source file. If it fails for any other reason than
187 * EXDEV, mv shall write a diagnostic message to the standard
188 * error and do nothing more with the current source file.
189 *
190 * (3) If the destination path exists, and it is a file of type
191 * directory and source_file is not a file of type directory,
192 * or it is a file not of type directory, and source file is
193 * a file of type directory, mv shall write a diagnostic
194 * message to standard error, and do nothing more with the
195 * current source file...
196 */
197 if (!rename(from, to))
198 return (0);
199
200 if (errno != EXDEV) {
201 warn("rename %s to %s", from, to);
202 return (1);
203 }
204
205 /*
206 * (4) If the destination path exists, mv shall attempt to remove it.
207 * If this fails for any reason, mv shall write a diagnostic
208 * message to the standard error and do nothing more with the
209 * current source file...
210 */
211 if (!stat(to, &sb)) {
212 if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
213 warn("can't remove %s", to);
214 return (1);
215 }
216 }
217
218 /*
219 * (5) The file hierarchy rooted in source_file shall be duplicated
220 * as a file hiearchy rooted in the destination path...
221 */
222 if (stat(from, &sb)) {
223 warn("%s", from);
224 return (1);
225 }
226 return (S_ISREG(sb.st_mode) ?
227 fastcopy(from, to, &sb) : copy(from, to));
228 }
229
230 int
231 fastcopy(from, to, sbp)
232 char *from, *to;
233 struct stat *sbp;
234 {
235 struct timeval tval[2];
236 static u_int blen;
237 static char *bp;
238 register int nread, from_fd, to_fd;
239
240 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
241 warn("%s", from);
242 return (1);
243 }
244 if ((to_fd =
245 open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
246 warn("%s", to);
247 (void)close(from_fd);
248 return (1);
249 }
250 if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
251 warn(NULL);
252 return (1);
253 }
254 while ((nread = read(from_fd, bp, blen)) > 0)
255 if (write(to_fd, bp, nread) != nread) {
256 warn("%s", to);
257 goto err;
258 }
259 if (nread < 0) {
260 warn("%s", from);
261 err: if (unlink(to))
262 warn("%s: remove", to);
263 (void)close(from_fd);
264 (void)close(to_fd);
265 return (1);
266 }
267 (void)close(from_fd);
268
269 if (fchown(to_fd, sbp->st_uid, sbp->st_gid))
270 warn("%s: set owner/group", to);
271 if (fchmod(to_fd, sbp->st_mode))
272 warn("%s: set mode", to);
273
274 tval[0].tv_sec = sbp->st_atime;
275 tval[1].tv_sec = sbp->st_mtime;
276 tval[0].tv_usec = tval[1].tv_usec = 0;
277 if (utimes(to, tval))
278 warn("%s: set times", to);
279
280 if (close(to_fd)) {
281 warn("%s", to);
282 return (1);
283 }
284
285 if (unlink(from)) {
286 warn("%s: remove", from);
287 return (1);
288 }
289 return (0);
290 }
291
292 int
293 copy(from, to)
294 char *from, *to;
295 {
296 int pid, status;
297
298 if ((pid = vfork()) == 0) {
299 execl(_PATH_CP, "mv", "-PRp", from, to, NULL);
300 warn("%s", _PATH_CP);
301 _exit(1);
302 }
303 if (waitpid(pid, &status, 0) == -1) {
304 warn("%s: waitpid", _PATH_CP);
305 return (1);
306 }
307 if (!WIFEXITED(status)) {
308 warn("%s: did not terminate normally", _PATH_CP);
309 return (1);
310 }
311 if (WEXITSTATUS(status)) {
312 warn("%s: terminated with %d (non-zero) status",
313 _PATH_CP, WEXITSTATUS(status));
314 return (1);
315 }
316 if (!(pid = vfork())) {
317 execl(_PATH_RM, "mv", "-rf", from, NULL);
318 warn("%s", _PATH_RM);
319 _exit(1);
320 }
321 if (waitpid(pid, &status, 0) == -1) {
322 warn("%s: waitpid", _PATH_RM);
323 return (1);
324 }
325 if (!WIFEXITED(status)) {
326 warn("%s: did not terminate normally", _PATH_RM);
327 return (1);
328 }
329 if (WEXITSTATUS(status)) {
330 warn("%s: terminated with %d (non-zero) status",
331 _PATH_RM, WEXITSTATUS(status));
332 return (1);
333 }
334 return (0);
335 }
336
337 void
338 usage()
339 {
340
341 (void)fprintf(stderr, "usage: mv [-fi] source target\n");
342 (void)fprintf(stderr, " mv [-fi] source ... directory\n");
343 exit(1);
344 }
345