file_cmds-45.tar.gz
[apple/file_cmds.git] / mv / mv.c
1 /* $NetBSD: mv.c,v 1.19 1998/07/28 11:41:49 mycroft 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.19 1998/07/28 11:41:49 mycroft 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 (void)fprintf(stderr, "overwrite %s? ", to);
179 } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
180 strmode(sb.st_mode, modep);
181 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
182 modep + 1, modep[9] == ' ' ? "" : " ",
183 user_from_uid(sb.st_uid, 0),
184 group_from_gid(sb.st_gid, 0), to);
185 } else
186 ask = 0;
187 if (ask) {
188 if ((ch = getchar()) != EOF && ch != '\n')
189 while (getchar() != '\n');
190 if (ch != 'y' && ch != 'Y')
191 return (0);
192 }
193 }
194
195 /*
196 * (2) If rename() succeeds, mv shall do nothing more with the
197 * current source file. If it fails for any other reason than
198 * EXDEV, mv shall write a diagnostic message to the standard
199 * error and do nothing more with the current source file.
200 *
201 * (3) If the destination path exists, and it is a file of type
202 * directory and source_file is not a file of type directory,
203 * or it is a file not of type directory, and source file is
204 * a file of type directory, mv shall write a diagnostic
205 * message to standard error, and do nothing more with the
206 * current source file...
207 */
208 if (!rename(from, to))
209 return (0);
210
211 if (errno != EXDEV) {
212 warn("rename %s to %s", from, to);
213 return (1);
214 }
215
216 /*
217 * (4) If the destination path exists, mv shall attempt to remove it.
218 * If this fails for any reason, mv shall write a diagnostic
219 * message to the standard error and do nothing more with the
220 * current source file...
221 */
222 if (!lstat(to, &sb)) {
223 if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
224 warn("can't remove %s", to);
225 return (1);
226 }
227 }
228
229 /*
230 * (5) The file hierarchy rooted in source_file shall be duplicated
231 * as a file hierarchy rooted in the destination path...
232 */
233 if (lstat(from, &sb)) {
234 warn("%s", from);
235 return (1);
236 }
237 return (S_ISREG(sb.st_mode) ?
238 fastcopy(from, to, &sb) : copy(from, to));
239 }
240
241 int
242 fastcopy(from, to, sbp)
243 char *from, *to;
244 struct stat *sbp;
245 {
246 struct timeval tval[2];
247 static u_int blen;
248 static char *bp;
249 int nread, from_fd, to_fd;
250
251 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
252 warn("%s", from);
253 return (1);
254 }
255 if ((to_fd =
256 open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
257 warn("%s", to);
258 (void)close(from_fd);
259 return (1);
260 }
261 if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
262 warn("%s", "");
263 return (1);
264 }
265 while ((nread = read(from_fd, bp, blen)) > 0)
266 if (write(to_fd, bp, nread) != nread) {
267 warn("%s", to);
268 goto err;
269 }
270 if (nread < 0) {
271 warn("%s", from);
272 err: if (unlink(to))
273 warn("%s: remove", to);
274 (void)close(from_fd);
275 (void)close(to_fd);
276 return (1);
277 }
278 (void)close(from_fd);
279
280 TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
281 TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
282 #ifndef __APPLE__
283 if (futimes(to_fd, tval))
284 #else
285 if (utimes(to, tval))
286 #endif
287 warn("%s: set times", to);
288 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
289 if (errno != EPERM)
290 warn("%s: set owner/group", to);
291 sbp->st_mode &= ~(S_ISUID | S_ISGID);
292 }
293 if (fchmod(to_fd, sbp->st_mode))
294 warn("%s: set mode", to);
295
296 if (close(to_fd)) {
297 warn("%s", to);
298 return (1);
299 }
300
301 if (unlink(from)) {
302 warn("%s: remove", from);
303 return (1);
304 }
305 return (0);
306 }
307
308 int
309 copy(from, to)
310 char *from, *to;
311 {
312 int pid, status;
313
314 if ((pid = vfork()) == 0) {
315 execl(_PATH_CP, "mv", "-PRp", from, to, NULL);
316 warn("%s", _PATH_CP);
317 _exit(1);
318 }
319 if (waitpid(pid, &status, 0) == -1) {
320 warn("%s: waitpid", _PATH_CP);
321 return (1);
322 }
323 if (!WIFEXITED(status)) {
324 warn("%s: did not terminate normally", _PATH_CP);
325 return (1);
326 }
327 if (WEXITSTATUS(status)) {
328 warn("%s: terminated with %d (non-zero) status",
329 _PATH_CP, WEXITSTATUS(status));
330 return (1);
331 }
332 if (!(pid = vfork())) {
333 execl(_PATH_RM, "mv", "-rf", from, NULL);
334 warn("%s", _PATH_RM);
335 _exit(1);
336 }
337 if (waitpid(pid, &status, 0) == -1) {
338 warn("%s: waitpid", _PATH_RM);
339 return (1);
340 }
341 if (!WIFEXITED(status)) {
342 warn("%s: did not terminate normally", _PATH_RM);
343 return (1);
344 }
345 if (WEXITSTATUS(status)) {
346 warn("%s: terminated with %d (non-zero) status",
347 _PATH_RM, WEXITSTATUS(status));
348 return (1);
349 }
350 return (0);
351 }
352
353 void
354 usage()
355 {
356
357 (void)fprintf(stderr, "usage: mv [-fi] source target\n");
358 (void)fprintf(stderr, " mv [-fi] source ... directory\n");
359 exit(1);
360 /* NOTREACHED */
361 }