VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/mv.c@ 809

Last change on this file since 809 was 809, checked in by bird, 18 years ago

Solaris + cleanup.

File size: 12.2 KB
Line 
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if 0
34#ifndef lint
35static char const copyright[] =
36"@(#) Copyright (c) 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
42#endif /* not lint */
43#endif
44#if 0
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: src/bin/mv/mv.c,v 1.46 2005/09/05 04:36:08 csjp Exp $");
47#endif
48
49#include <sys/types.h>
50#ifndef _MSC_VER
51# ifndef __OS2__
52# include <sys/acl.h>
53# endif
54# include <sys/param.h>
55# include <sys/time.h>
56# include <sys/wait.h>
57# include <sys/mount.h>
58#endif
59#include <sys/stat.h>
60
61#include "err.h"
62#include <errno.h>
63#include <fcntl.h>
64#include <grp.h>
65#include <limits.h>
66#include <paths.h>
67#include <pwd.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <string.h>
71#include <sysexits.h>
72#include <unistd.h>
73
74#ifdef __sun__
75# include "solfakes.h"
76#endif
77#ifdef _MSC_VER
78# include "mscfakes.h"
79#endif
80
81#if !defined(__FreeBSD__) && !defined(__APPLE__)
82extern void strmode(mode_t mode, char *p);
83#endif
84
85static int fflg, iflg, nflg, vflg;
86
87static int do_move(char *, char *);
88#ifdef CROSS_DEVICE_MOVE
89static int fastcopy(char *, char *, struct stat *);
90static int copy(char *, char *);
91#endif
92static int usage(void);
93
94#if !defined(__FreeBSD__) && !defined(__APPLE__)
95# ifdef __OS2__
96static
97# endif
98const char *user_from_uid(uid_t id, int x)
99{
100 static char s_buf[64];
101 sprintf(s_buf, "%ld", id);
102 return s_buf;
103}
104# ifdef __OS2__
105static
106# endif
107const char *group_from_gid(gid_t id, int x)
108{
109 static char s_buf[64];
110 sprintf(s_buf, "%ld", id);
111 return s_buf;
112}
113#endif
114
115
116int
117kmk_builtin_mv(int argc, char *argv[])
118{
119 size_t baselen, len;
120 int rval;
121 char *p, *endp;
122 struct stat sb;
123 int ch;
124 char path[PATH_MAX];
125
126 /* kmk: reinitialize globals */
127 fflg = iflg = nflg = vflg = 0;
128
129 /* kmk: reset getopt and set progname */
130 g_progname = argv[0];
131 opterr = 1;
132 optarg = NULL;
133 optopt = 0;
134#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
135 optreset = 1;
136 optind = 1;
137#else
138 optind = 0; /* init */
139#endif
140
141 while ((ch = getopt(argc, argv, "finv")) != -1)
142 switch (ch) {
143 case 'i':
144 iflg = 1;
145 fflg = nflg = 0;
146 break;
147 case 'f':
148 fflg = 1;
149 iflg = nflg = 0;
150 break;
151 case 'n':
152 nflg = 1;
153 fflg = iflg = 0;
154 break;
155 case 'v':
156 vflg = 1;
157 break;
158 default:
159 return usage();
160 }
161 argc -= optind;
162 argv += optind;
163
164 if (argc < 2)
165 return usage();
166
167 /*
168 * If the stat on the target fails or the target isn't a directory,
169 * try the move. More than 2 arguments is an error in this case.
170 */
171 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
172 if (argc > 2)
173 return usage();
174 return do_move(argv[0], argv[1]);
175 }
176
177 /* It's a directory, move each file into it. */
178 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
179 return errx(1, "%s: destination pathname too long", *argv);
180 (void)strcpy(path, argv[argc - 1]);
181 baselen = strlen(path);
182 endp = &path[baselen];
183#if defined(_MSC_VER) || defined(__EMX__)
184 if (!baselen || (*(endp - 1) != '/' && *(endp - 1) != '\\' && *(endp - 1) != ':')) {
185#else
186 if (!baselen || *(endp - 1) != '/') {
187#endif
188 *endp++ = '/';
189 ++baselen;
190 }
191 for (rval = 0; --argc; ++argv) {
192 /*
193 * Find the last component of the source pathname. It
194 * may have trailing slashes.
195 */
196 p = *argv + strlen(*argv);
197#if defined(_MSC_VER) || defined(__EMX__)
198 while (p != *argv && (p[-1] == '/' || p[-1] == '\\'))
199 --p;
200 while (p != *argv && p[-1] != '/' && p[-1] != '/' && p[-1] != ':')
201 --p;
202#else
203 while (p != *argv && p[-1] == '/')
204 --p;
205 while (p != *argv && p[-1] != '/')
206 --p;
207#endif
208
209 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
210 warnx("%s: destination pathname too long", *argv);
211 rval = 1;
212 } else {
213 memmove(endp, p, (size_t)len + 1);
214 if (do_move(*argv, path))
215 rval = 1;
216 }
217 }
218 return rval;
219}
220
221static int
222do_move(char *from, char *to)
223{
224 struct stat sb;
225 int ask, ch, first;
226 char modep[15];
227
228 /*
229 * Check access. If interactive and file exists, ask user if it
230 * should be replaced. Otherwise if file exists but isn't writable
231 * make sure the user wants to clobber it.
232 */
233 if (!fflg && !access(to, F_OK)) {
234
235 /* prompt only if source exist */
236 if (lstat(from, &sb) == -1) {
237 warn("%s", from);
238 return (1);
239 }
240
241#define YESNO "(y/n [n]) "
242 ask = 0;
243 if (nflg) {
244 if (vflg)
245 printf("%s not overwritten\n", to);
246 return (0);
247 } else if (iflg) {
248 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
249 ask = 1;
250 } else if (access(to, W_OK) && !stat(to, &sb)) {
251 strmode(sb.st_mode, modep);
252 (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
253 modep + 1, modep[9] == ' ' ? "" : " ",
254 user_from_uid((unsigned long)sb.st_uid, 0),
255 group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
256 ask = 1;
257 }
258 if (ask) {
259 first = ch = getchar();
260 while (ch != '\n' && ch != EOF)
261 ch = getchar();
262 if (first != 'y' && first != 'Y') {
263 (void)fprintf(stderr, "not overwritten\n");
264 return (0);
265 }
266 }
267 }
268 if (!rename(from, to)) {
269 if (vflg)
270 printf("%s -> %s\n", from, to);
271 return (0);
272 }
273#ifdef _MSC_VER
274 if (errno == EEXIST) {
275 remove(to);
276 if (!rename(from, to)) {
277 if (vflg)
278 printf("%s -> %s\n", from, to);
279 return (0);
280 }
281 }
282#endif
283
284 if (errno == EXDEV) {
285#ifndef CROSS_DEVICE_MOVE
286 warnx("cannot move `%s' to a different device: `%s'", from, to);
287 return (1);
288#else
289 struct statfs sfs;
290 char path[PATH_MAX];
291
292 /*
293 * If the source is a symbolic link and is on another
294 * filesystem, it can be recreated at the destination.
295 */
296 if (lstat(from, &sb) == -1) {
297 warn("%s", from);
298 return (1);
299 }
300 if (!S_ISLNK(sb.st_mode)) {
301 /* Can't mv(1) a mount point. */
302 if (realpath(from, path) == NULL) {
303 warnx("cannot resolve %s: %s", from, path);
304 return (1);
305 }
306 if (!statfs(path, &sfs) &&
307 !strcmp(path, sfs.f_mntonname)) {
308 warnx("cannot rename a mount point");
309 return (1);
310 }
311 }
312#endif
313 } else {
314 warn("rename %s to %s", from, to);
315 return (1);
316 }
317
318#ifdef CROSS_DEVICE_MOVE
319 /*
320 * If rename fails because we're trying to cross devices, and
321 * it's a regular file, do the copy internally; otherwise, use
322 * cp and rm.
323 */
324 if (lstat(from, &sb)) {
325 warn("%s", from);
326 return (1);
327 }
328 return (S_ISREG(sb.st_mode) ?
329 fastcopy(from, to, &sb) : copy(from, to));
330#endif
331}
332
333#ifdef CROSS_DEVICE_MOVE
334int
335static fastcopy(char *from, char *to, struct stat *sbp)
336{
337 struct timeval tval[2];
338 static u_int blen;
339 static char *bp;
340 mode_t oldmode;
341 int nread, from_fd, to_fd;
342 acl_t acl;
343
344 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
345 warn("%s", from);
346 return (1);
347 }
348 if (blen < sbp->st_blksize) {
349 if (bp != NULL)
350 free(bp);
351 if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
352 blen = 0;
353 warnx("malloc failed");
354 return (1);
355 }
356 blen = sbp->st_blksize;
357 }
358 while ((to_fd =
359 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
360 if (errno == EEXIST && unlink(to) == 0)
361 continue;
362 warn("%s", to);
363 (void)close(from_fd);
364 return (1);
365 }
366 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
367 if (write(to_fd, bp, (size_t)nread) != nread) {
368 warn("%s", to);
369 goto err;
370 }
371 if (nread < 0) {
372 warn("%s", from);
373err: if (unlink(to))
374 warn("%s: remove", to);
375 (void)close(from_fd);
376 (void)close(to_fd);
377 return (1);
378 }
379
380 oldmode = sbp->st_mode & ALLPERMS;
381 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
382 warn("%s: set owner/group (was: %lu/%lu)", to,
383 (u_long)sbp->st_uid, (u_long)sbp->st_gid);
384 if (oldmode & (S_ISUID | S_ISGID)) {
385 warnx(
386"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
387 to, oldmode);
388 sbp->st_mode &= ~(S_ISUID | S_ISGID);
389 }
390 }
391 /*
392 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
393 * for dest_file, then it's ACLs shall reflect the ACLs of the
394 * source_file.
395 */
396 if (fpathconf(to_fd, _PC_ACL_EXTENDED) == 1 &&
397 fpathconf(from_fd, _PC_ACL_EXTENDED) == 1) {
398 acl = acl_get_fd(from_fd);
399 if (acl == NULL)
400 warn("failed to get acl entries while setting %s",
401 from);
402 else if (acl_set_fd(to_fd, acl) < 0)
403 warn("failed to set acl entries for %s", to);
404 }
405 (void)close(from_fd);
406 if (fchmod(to_fd, sbp->st_mode))
407 warn("%s: set mode (was: 0%03o)", to, oldmode);
408 /*
409 * XXX
410 * NFS doesn't support chflags; ignore errors unless there's reason
411 * to believe we're losing bits. (Note, this still won't be right
412 * if the server supports flags and we were trying to *remove* flags
413 * on a file that we copied, i.e., that we didn't create.)
414 */
415 errno = 0;
416 if (fchflags(to_fd, (u_long)sbp->st_flags))
417 if (errno != EOPNOTSUPP || sbp->st_flags != 0)
418 warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
419
420 tval[0].tv_sec = sbp->st_atime;
421 tval[1].tv_sec = sbp->st_mtime;
422 tval[0].tv_usec = tval[1].tv_usec = 0;
423 if (utimes(to, tval))
424 warn("%s: set times", to);
425
426 if (close(to_fd)) {
427 warn("%s", to);
428 return (1);
429 }
430
431 if (unlink(from)) {
432 warn("%s: remove", from);
433 return (1);
434 }
435 if (vflg)
436 printf("%s -> %s\n", from, to);
437 return (0);
438}
439
440int
441copy(char *from, char *to)
442{
443 int pid, status;
444
445 if ((pid = fork()) == 0) {
446 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
447 (char *)NULL);
448 warn("%s", _PATH_CP);
449 _exit(1);
450 }
451 if (waitpid(pid, &status, 0) == -1) {
452 warn("%s: waitpid", _PATH_CP);
453 return (1);
454 }
455 if (!WIFEXITED(status)) {
456 warnx("%s: did not terminate normally", _PATH_CP);
457 return (1);
458 }
459 if (WEXITSTATUS(status)) {
460 warnx("%s: terminated with %d (non-zero) status",
461 _PATH_CP, WEXITSTATUS(status));
462 return (1);
463 }
464 if (!(pid = vfork())) {
465 execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
466 warn("%s", _PATH_RM);
467 _exit(1);
468 }
469 if (waitpid(pid, &status, 0) == -1) {
470 warn("%s: waitpid", _PATH_RM);
471 return (1);
472 }
473 if (!WIFEXITED(status)) {
474 warnx("%s: did not terminate normally", _PATH_RM);
475 return (1);
476 }
477 if (WEXITSTATUS(status)) {
478 warnx("%s: terminated with %d (non-zero) status",
479 _PATH_RM, WEXITSTATUS(status));
480 return (1);
481 }
482 return (0);
483}
484#endif /* CROSS_DEVICE_MOVE */
485
486
487static int
488usage(void)
489{
490
491 (void)fprintf(stderr, "%s\n%s\n",
492 "usage: mv [-f | -i | -n] [-v] source target",
493 " mv [-f | -i | -n] [-v] source ... directory");
494 return EX_USAGE;
495}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette