Deleted Added
full compact
mv.c (29933) mv.c (30106)
1/*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ken Smith of The State University of New York at Buffalo.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
1/*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ken Smith of The State University of New York at Buffalo.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * $Id: mv.c,v 1.13 1997/03/28 15:24:26 imp Exp $
36 * $Id: mv.c,v 1.14 1997/09/28 10:41:40 wosch Exp $
37 */
38
39#ifndef lint
40static char const 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
46static char const sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
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 <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62#include "pathnames.h"
63
64int fflg, iflg;
65
66int copy __P((char *, char *));
67int do_move __P((char *, char *));
68int fastcopy __P((char *, char *, struct stat *));
69void usage __P((void));
70
71int
72main(argc, argv)
73 int argc;
74 char *argv[];
75{
76 register int baselen, len, rval;
77 register char *p, *endp;
78 struct stat sb;
79 int ch;
80 char path[MAXPATHLEN + 1];
81
82 while ((ch = getopt(argc, argv, "fi")) != -1)
83 switch (ch) {
84 case 'i':
85 iflg = 1;
86 fflg = 0;
87 break;
88 case 'f':
89 fflg = 1;
90 iflg = 0;
91 break;
92 default:
93 usage();
94 }
95 argc -= optind;
96 argv += optind;
97
98 if (argc < 2)
99 usage();
100
101 /*
102 * If the stat on the target fails or the target isn't a directory,
103 * try the move. More than 2 arguments is an error in this case.
104 */
105 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
106 if (argc > 2)
107 usage();
108 exit(do_move(argv[0], argv[1]));
109 }
110
111 /* It's a directory, move each file into it. */
112 (void)strcpy(path, argv[argc - 1]);
113 baselen = strlen(path);
114 endp = &path[baselen];
115 *endp++ = '/';
116 ++baselen;
117 for (rval = 0; --argc; ++argv) {
118 /*
119 * Find the last component of the source pathname. It
120 * may have trailing slashes.
121 */
122 p = *argv + strlen(*argv);
123 while (p != *argv && p[-1] == '/')
124 --p;
125 while (p != *argv && p[-1] != '/')
126 --p;
127
128 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
129 warnx("%s: destination pathname too long", *argv);
130 rval = 1;
131 } else {
132 memmove(endp, p, len + 1);
133 if (do_move(*argv, path))
134 rval = 1;
135 }
136 }
137 exit(rval);
138}
139
140int
141do_move(from, to)
142 char *from, *to;
143{
144 struct stat sb;
145 int ask, ch, first;
146 char modep[15];
147
148 /*
149 * Check access. If interactive and file exists, ask user if it
150 * should be replaced. Otherwise if file exists but isn't writable
151 * make sure the user wants to clobber it.
152 */
153 if (!fflg && !access(to, F_OK)) {
154
155 /* prompt only if source exist */
156 if (lstat(from, &sb) == -1) {
157 warn("%s", from);
158 return (1);
159 }
37 */
38
39#ifndef lint
40static char const 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
46static char const sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
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 <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62#include "pathnames.h"
63
64int fflg, iflg;
65
66int copy __P((char *, char *));
67int do_move __P((char *, char *));
68int fastcopy __P((char *, char *, struct stat *));
69void usage __P((void));
70
71int
72main(argc, argv)
73 int argc;
74 char *argv[];
75{
76 register int baselen, len, rval;
77 register char *p, *endp;
78 struct stat sb;
79 int ch;
80 char path[MAXPATHLEN + 1];
81
82 while ((ch = getopt(argc, argv, "fi")) != -1)
83 switch (ch) {
84 case 'i':
85 iflg = 1;
86 fflg = 0;
87 break;
88 case 'f':
89 fflg = 1;
90 iflg = 0;
91 break;
92 default:
93 usage();
94 }
95 argc -= optind;
96 argv += optind;
97
98 if (argc < 2)
99 usage();
100
101 /*
102 * If the stat on the target fails or the target isn't a directory,
103 * try the move. More than 2 arguments is an error in this case.
104 */
105 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
106 if (argc > 2)
107 usage();
108 exit(do_move(argv[0], argv[1]));
109 }
110
111 /* It's a directory, move each file into it. */
112 (void)strcpy(path, argv[argc - 1]);
113 baselen = strlen(path);
114 endp = &path[baselen];
115 *endp++ = '/';
116 ++baselen;
117 for (rval = 0; --argc; ++argv) {
118 /*
119 * Find the last component of the source pathname. It
120 * may have trailing slashes.
121 */
122 p = *argv + strlen(*argv);
123 while (p != *argv && p[-1] == '/')
124 --p;
125 while (p != *argv && p[-1] != '/')
126 --p;
127
128 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
129 warnx("%s: destination pathname too long", *argv);
130 rval = 1;
131 } else {
132 memmove(endp, p, len + 1);
133 if (do_move(*argv, path))
134 rval = 1;
135 }
136 }
137 exit(rval);
138}
139
140int
141do_move(from, to)
142 char *from, *to;
143{
144 struct stat sb;
145 int ask, ch, first;
146 char modep[15];
147
148 /*
149 * Check access. If interactive and file exists, ask user if it
150 * should be replaced. Otherwise if file exists but isn't writable
151 * make sure the user wants to clobber it.
152 */
153 if (!fflg && !access(to, F_OK)) {
154
155 /* prompt only if source exist */
156 if (lstat(from, &sb) == -1) {
157 warn("%s", from);
158 return (1);
159 }
160
160
161#define YESNO "(y/n [n]) "
161 ask = 0;
162 if (iflg) {
162 ask = 0;
163 if (iflg) {
163 (void)fprintf(stderr, "overwrite %s? ", to);
164 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
164 ask = 1;
165 } else if (access(to, W_OK) && !stat(to, &sb)) {
166 strmode(sb.st_mode, modep);
165 ask = 1;
166 } else if (access(to, W_OK) && !stat(to, &sb)) {
167 strmode(sb.st_mode, modep);
167 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
168 (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
168 modep + 1, modep[9] == ' ' ? "" : " ",
169 user_from_uid(sb.st_uid, 0),
169 modep + 1, modep[9] == ' ' ? "" : " ",
170 user_from_uid(sb.st_uid, 0),
170 group_from_gid(sb.st_gid, 0), to);
171 group_from_gid(sb.st_gid, 0), to, YESNO);
171 ask = 1;
172 }
173 if (ask) {
174 first = ch = getchar();
175 while (ch != '\n' && ch != EOF)
176 ch = getchar();
172 ask = 1;
173 }
174 if (ask) {
175 first = ch = getchar();
176 while (ch != '\n' && ch != EOF)
177 ch = getchar();
177 if (first != 'y' && first != 'Y')
178 if (first != 'y' && first != 'Y') {
179 (void)fprintf(stderr, "not overwritten\n");
178 return (0);
180 return (0);
181 }
179 }
180 }
181 if (!rename(from, to))
182 return (0);
183
184 if (errno != EXDEV) {
185 warn("rename %s to %s", from, to);
186 return (1);
187 }
188
189 /*
190 * If rename fails because we're trying to cross devices, and
191 * it's a regular file, do the copy internally; otherwise, use
192 * cp and rm.
193 */
194 if (stat(from, &sb)) {
195 warn("%s", from);
196 return (1);
197 }
198 return (S_ISREG(sb.st_mode) ?
199 fastcopy(from, to, &sb) : copy(from, to));
200}
201
202int
203fastcopy(from, to, sbp)
204 char *from, *to;
205 struct stat *sbp;
206{
207 struct timeval tval[2];
208 static u_int blen;
209 static char *bp;
210 mode_t oldmode;
211 register int nread, from_fd, to_fd;
212
213 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
214 warn("%s", from);
215 return (1);
216 }
217 if (blen < sbp->st_blksize) {
218 if (bp != NULL)
219 free(bp);
220 if ((bp = malloc(sbp->st_blksize)) == NULL) {
221 blen = 0;
222 warnx("malloc failed");
223 return (1);
224 }
225 blen = sbp->st_blksize;
226 }
227 while ((to_fd =
228 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
229 if (errno == EEXIST && unlink(to) == 0)
230 continue;
231 warn("%s", to);
232 (void)close(from_fd);
233 return (1);
234 }
235 while ((nread = read(from_fd, bp, blen)) > 0)
236 if (write(to_fd, bp, nread) != nread) {
237 warn("%s", to);
238 goto err;
239 }
240 if (nread < 0) {
241 warn("%s", from);
242err: if (unlink(to))
243 warn("%s: remove", to);
244 (void)close(from_fd);
245 (void)close(to_fd);
246 return (1);
247 }
248 (void)close(from_fd);
249
250 oldmode = sbp->st_mode & ALLPERMS;
251 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
252 warn("%s: set owner/group (was: %u/%u)", to, sbp->st_uid,
253 sbp->st_gid);
254 if (oldmode & (S_ISUID | S_ISGID)) {
255 warnx(
256"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
257 to, oldmode);
258 sbp->st_mode &= ~(S_ISUID | S_ISGID);
259 }
260 }
261 if (fchmod(to_fd, sbp->st_mode))
262 warn("%s: set mode (was: 0%03o)", to, oldmode);
263
264 tval[0].tv_sec = sbp->st_atime;
265 tval[1].tv_sec = sbp->st_mtime;
266 tval[0].tv_usec = tval[1].tv_usec = 0;
267 if (utimes(to, tval))
268 warn("%s: set times", to);
269
270 if (close(to_fd)) {
271 warn("%s", to);
272 return (1);
273 }
274
275 if (unlink(from)) {
276 warn("%s: remove", from);
277 return (1);
278 }
279 return (0);
280}
281
282int
283copy(from, to)
284 char *from, *to;
285{
286 int pid, status;
287
288 if ((pid = vfork()) == 0) {
289 execl(_PATH_CP, "mv", "-PRp", from, to, NULL);
290 warn("%s", _PATH_CP);
291 _exit(1);
292 }
293 if (waitpid(pid, &status, 0) == -1) {
294 warn("%s: waitpid", _PATH_CP);
295 return (1);
296 }
297 if (!WIFEXITED(status)) {
298 warn("%s: did not terminate normally", _PATH_CP);
299 return (1);
300 }
301 if (WEXITSTATUS(status)) {
302 warn("%s: terminated with %d (non-zero) status",
303 _PATH_CP, WEXITSTATUS(status));
304 return (1);
305 }
306 if (!(pid = vfork())) {
307 execl(_PATH_RM, "mv", "-rf", from, NULL);
308 warn("%s", _PATH_RM);
309 _exit(1);
310 }
311 if (waitpid(pid, &status, 0) == -1) {
312 warn("%s: waitpid", _PATH_RM);
313 return (1);
314 }
315 if (!WIFEXITED(status)) {
316 warn("%s: did not terminate normally", _PATH_RM);
317 return (1);
318 }
319 if (WEXITSTATUS(status)) {
320 warn("%s: terminated with %d (non-zero) status",
321 _PATH_RM, WEXITSTATUS(status));
322 return (1);
323 }
324 return (0);
325}
326
327void
328usage()
329{
330 (void)fprintf(stderr, "%s\n%s\n",
331 "usage: mv [-f | -i] src target",
332 " mv [-f | -i] src1 ... srcN directory");
333 exit(1);
334}
182 }
183 }
184 if (!rename(from, to))
185 return (0);
186
187 if (errno != EXDEV) {
188 warn("rename %s to %s", from, to);
189 return (1);
190 }
191
192 /*
193 * If rename fails because we're trying to cross devices, and
194 * it's a regular file, do the copy internally; otherwise, use
195 * cp and rm.
196 */
197 if (stat(from, &sb)) {
198 warn("%s", from);
199 return (1);
200 }
201 return (S_ISREG(sb.st_mode) ?
202 fastcopy(from, to, &sb) : copy(from, to));
203}
204
205int
206fastcopy(from, to, sbp)
207 char *from, *to;
208 struct stat *sbp;
209{
210 struct timeval tval[2];
211 static u_int blen;
212 static char *bp;
213 mode_t oldmode;
214 register int nread, from_fd, to_fd;
215
216 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
217 warn("%s", from);
218 return (1);
219 }
220 if (blen < sbp->st_blksize) {
221 if (bp != NULL)
222 free(bp);
223 if ((bp = malloc(sbp->st_blksize)) == NULL) {
224 blen = 0;
225 warnx("malloc failed");
226 return (1);
227 }
228 blen = sbp->st_blksize;
229 }
230 while ((to_fd =
231 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
232 if (errno == EEXIST && unlink(to) == 0)
233 continue;
234 warn("%s", to);
235 (void)close(from_fd);
236 return (1);
237 }
238 while ((nread = read(from_fd, bp, blen)) > 0)
239 if (write(to_fd, bp, nread) != nread) {
240 warn("%s", to);
241 goto err;
242 }
243 if (nread < 0) {
244 warn("%s", from);
245err: if (unlink(to))
246 warn("%s: remove", to);
247 (void)close(from_fd);
248 (void)close(to_fd);
249 return (1);
250 }
251 (void)close(from_fd);
252
253 oldmode = sbp->st_mode & ALLPERMS;
254 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
255 warn("%s: set owner/group (was: %u/%u)", to, sbp->st_uid,
256 sbp->st_gid);
257 if (oldmode & (S_ISUID | S_ISGID)) {
258 warnx(
259"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
260 to, oldmode);
261 sbp->st_mode &= ~(S_ISUID | S_ISGID);
262 }
263 }
264 if (fchmod(to_fd, sbp->st_mode))
265 warn("%s: set mode (was: 0%03o)", to, oldmode);
266
267 tval[0].tv_sec = sbp->st_atime;
268 tval[1].tv_sec = sbp->st_mtime;
269 tval[0].tv_usec = tval[1].tv_usec = 0;
270 if (utimes(to, tval))
271 warn("%s: set times", to);
272
273 if (close(to_fd)) {
274 warn("%s", to);
275 return (1);
276 }
277
278 if (unlink(from)) {
279 warn("%s: remove", from);
280 return (1);
281 }
282 return (0);
283}
284
285int
286copy(from, to)
287 char *from, *to;
288{
289 int pid, status;
290
291 if ((pid = vfork()) == 0) {
292 execl(_PATH_CP, "mv", "-PRp", from, to, NULL);
293 warn("%s", _PATH_CP);
294 _exit(1);
295 }
296 if (waitpid(pid, &status, 0) == -1) {
297 warn("%s: waitpid", _PATH_CP);
298 return (1);
299 }
300 if (!WIFEXITED(status)) {
301 warn("%s: did not terminate normally", _PATH_CP);
302 return (1);
303 }
304 if (WEXITSTATUS(status)) {
305 warn("%s: terminated with %d (non-zero) status",
306 _PATH_CP, WEXITSTATUS(status));
307 return (1);
308 }
309 if (!(pid = vfork())) {
310 execl(_PATH_RM, "mv", "-rf", from, NULL);
311 warn("%s", _PATH_RM);
312 _exit(1);
313 }
314 if (waitpid(pid, &status, 0) == -1) {
315 warn("%s: waitpid", _PATH_RM);
316 return (1);
317 }
318 if (!WIFEXITED(status)) {
319 warn("%s: did not terminate normally", _PATH_RM);
320 return (1);
321 }
322 if (WEXITSTATUS(status)) {
323 warn("%s: terminated with %d (non-zero) status",
324 _PATH_RM, WEXITSTATUS(status));
325 return (1);
326 }
327 return (0);
328}
329
330void
331usage()
332{
333 (void)fprintf(stderr, "%s\n%s\n",
334 "usage: mv [-f | -i] src target",
335 " mv [-f | -i] src1 ... srcN directory");
336 exit(1);
337}