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