VirtualBox

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

最後變更 在這個檔案從774是 714,由 bird 提交於 18 年 前

made it build on OS/2.

檔案大小: 11.7 KB
 
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#ifndef _MSC_VER
65#include <grp.h>
66#endif
67#include <limits.h>
68#ifndef _MSC_VER
69#include <paths.h>
70#include <pwd.h>
71#endif
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#ifndef _MSC_VER
76#include <sysexits.h>
77#include <unistd.h>
78#else
79#include "mscfakes.h"
80#endif
81
82#if !defined(__FreeBSD__) && !defined(__APPLE__)
83extern void strmode(mode_t mode, char *p);
84#endif
85
86static int fflg, iflg, nflg, vflg;
87
88static int do_move(char *, char *);
89#ifdef CROSS_DEVICE_MOVE
90static int fastcopy(char *, char *, struct stat *);
91static int copy(char *, char *);
92#endif
93static int usage(void);
94
95#if !defined(__FreeBSD__) && !defined(__APPLE__)
96# ifdef __OS2__
97static
98# endif
99const char *user_from_uid(uid_t id, int x)
100{
101 static char s_buf[64];
102 sprintf(s_buf, "%ld", id);
103 return s_buf;
104}
105# ifdef __OS2__
106static
107# endif
108const char *group_from_gid(gid_t id, int x)
109{
110 static char s_buf[64];
111 sprintf(s_buf, "%ld", id);
112 return s_buf;
113}
114#endif
115
116
117int
118kmk_builtin_mv(int argc, char *argv[])
119{
120 size_t baselen, len;
121 int rval;
122 char *p, *endp;
123 struct stat sb;
124 int ch;
125 char path[PATH_MAX];
126
127 /* kmk: reinitialize globals */
128 fflg = iflg = nflg = vflg = 0;
129
130 /* kmk: reset getopt and set progname */
131 g_progname = argv[0];
132 opterr = 1;
133 optarg = NULL;
134 optopt = 0;
135#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
136 optreset = 1;
137 optind = 1;
138#else
139 optind = 0; /* init */
140#endif
141
142 while ((ch = getopt(argc, argv, "finv")) != -1)
143 switch (ch) {
144 case 'i':
145 iflg = 1;
146 fflg = nflg = 0;
147 break;
148 case 'f':
149 fflg = 1;
150 iflg = nflg = 0;
151 break;
152 case 'n':
153 nflg = 1;
154 fflg = iflg = 0;
155 break;
156 case 'v':
157 vflg = 1;
158 break;
159 default:
160 return usage();
161 }
162 argc -= optind;
163 argv += optind;
164
165 if (argc < 2)
166 return usage();
167
168 /*
169 * If the stat on the target fails or the target isn't a directory,
170 * try the move. More than 2 arguments is an error in this case.
171 */
172 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
173 if (argc > 2)
174 return usage();
175 return do_move(argv[0], argv[1]);
176 }
177
178 /* It's a directory, move each file into it. */
179 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
180 return errx(1, "%s: destination pathname too long", *argv);
181 (void)strcpy(path, argv[argc - 1]);
182 baselen = strlen(path);
183 endp = &path[baselen];
184#if defined(_MSC_VER) || defined(__EMX__)
185 if (!baselen || (*(endp - 1) != '/' && *(endp - 1) != '\\' && *(endp - 1) != ':')) {
186#else
187 if (!baselen || *(endp - 1) != '/') {
188#endif
189 *endp++ = '/';
190 ++baselen;
191 }
192 for (rval = 0; --argc; ++argv) {
193 /*
194 * Find the last component of the source pathname. It
195 * may have trailing slashes.
196 */
197 p = *argv + strlen(*argv);
198#if defined(_MSC_VER) || defined(__EMX__)
199 while (p != *argv && (p[-1] == '/' || p[-1] == '\\'))
200 --p;
201 while (p != *argv && p[-1] != '/' && p[-1] != '/' && p[-1] != ':')
202 --p;
203#else
204 while (p != *argv && p[-1] == '/')
205 --p;
206 while (p != *argv && p[-1] != '/')
207 --p;
208#endif
209
210 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
211 warnx("%s: destination pathname too long", *argv);
212 rval = 1;
213 } else {
214 memmove(endp, p, (size_t)len + 1);
215 if (do_move(*argv, path))
216 rval = 1;
217 }
218 }
219 return rval;
220}
221
222static int
223do_move(char *from, char *to)
224{
225 struct stat sb;
226 int ask, ch, first;
227 char modep[15];
228
229 /*
230 * Check access. If interactive and file exists, ask user if it
231 * should be replaced. Otherwise if file exists but isn't writable
232 * make sure the user wants to clobber it.
233 */
234 if (!fflg && !access(to, F_OK)) {
235
236 /* prompt only if source exist */
237 if (lstat(from, &sb) == -1) {
238 warn("%s", from);
239 return (1);
240 }
241
242#define YESNO "(y/n [n]) "
243 ask = 0;
244 if (nflg) {
245 if (vflg)
246 printf("%s not overwritten\n", to);
247 return (0);
248 } else if (iflg) {
249 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
250 ask = 1;
251 } else if (access(to, W_OK) && !stat(to, &sb)) {
252 strmode(sb.st_mode, modep);
253 (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
254 modep + 1, modep[9] == ' ' ? "" : " ",
255 user_from_uid((unsigned long)sb.st_uid, 0),
256 group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
257 ask = 1;
258 }
259 if (ask) {
260 first = ch = getchar();
261 while (ch != '\n' && ch != EOF)
262 ch = getchar();
263 if (first != 'y' && first != 'Y') {
264 (void)fprintf(stderr, "not overwritten\n");
265 return (0);
266 }
267 }
268 }
269 if (!rename(from, to)) {
270 if (vflg)
271 printf("%s -> %s\n", from, to);
272 return (0);
273 }
274#ifdef _MSC_VER
275 if (errno == EEXIST) {
276 remove(to);
277 if (!rename(from, to)) {
278 if (vflg)
279 printf("%s -> %s\n", from, to);
280 return (0);
281 }
282 }
283#endif
284
285 if (errno == EXDEV) {
286#ifndef CROSS_DEVICE_MOVE
287 warnx("cannot move `%s' to a different device: `%s'", from, to);
288 return (1);
289#else
290 struct statfs sfs;
291 char path[PATH_MAX];
292
293 /*
294 * If the source is a symbolic link and is on another
295 * filesystem, it can be recreated at the destination.
296 */
297 if (lstat(from, &sb) == -1) {
298 warn("%s", from);
299 return (1);
300 }
301 if (!S_ISLNK(sb.st_mode)) {
302 /* Can't mv(1) a mount point. */
303 if (realpath(from, path) == NULL) {
304 warnx("cannot resolve %s: %s", from, path);
305 return (1);
306 }
307 if (!statfs(path, &sfs) &&
308 !strcmp(path, sfs.f_mntonname)) {
309 warnx("cannot rename a mount point");
310 return (1);
311 }
312 }
313#endif
314 } else {
315 warn("rename %s to %s", from, to);
316 return (1);
317 }
318
319#ifdef CROSS_DEVICE_MOVE
320 /*
321 * If rename fails because we're trying to cross devices, and
322 * it's a regular file, do the copy internally; otherwise, use
323 * cp and rm.
324 */
325 if (lstat(from, &sb)) {
326 warn("%s", from);
327 return (1);
328 }
329 return (S_ISREG(sb.st_mode) ?
330 fastcopy(from, to, &sb) : copy(from, to));
331#endif
332}
333
334#ifdef CROSS_DEVICE_MOVE
335int
336static fastcopy(char *from, char *to, struct stat *sbp)
337{
338 struct timeval tval[2];
339 static u_int blen;
340 static char *bp;
341 mode_t oldmode;
342 int nread, from_fd, to_fd;
343 acl_t acl;
344
345 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
346 warn("%s", from);
347 return (1);
348 }
349 if (blen < sbp->st_blksize) {
350 if (bp != NULL)
351 free(bp);
352 if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
353 blen = 0;
354 warnx("malloc failed");
355 return (1);
356 }
357 blen = sbp->st_blksize;
358 }
359 while ((to_fd =
360 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
361 if (errno == EEXIST && unlink(to) == 0)
362 continue;
363 warn("%s", to);
364 (void)close(from_fd);
365 return (1);
366 }
367 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
368 if (write(to_fd, bp, (size_t)nread) != nread) {
369 warn("%s", to);
370 goto err;
371 }
372 if (nread < 0) {
373 warn("%s", from);
374err: if (unlink(to))
375 warn("%s: remove", to);
376 (void)close(from_fd);
377 (void)close(to_fd);
378 return (1);
379 }
380
381 oldmode = sbp->st_mode & ALLPERMS;
382 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
383 warn("%s: set owner/group (was: %lu/%lu)", to,
384 (u_long)sbp->st_uid, (u_long)sbp->st_gid);
385 if (oldmode & (S_ISUID | S_ISGID)) {
386 warnx(
387"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
388 to, oldmode);
389 sbp->st_mode &= ~(S_ISUID | S_ISGID);
390 }
391 }
392 /*
393 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
394 * for dest_file, then it's ACLs shall reflect the ACLs of the
395 * source_file.
396 */
397 if (fpathconf(to_fd, _PC_ACL_EXTENDED) == 1 &&
398 fpathconf(from_fd, _PC_ACL_EXTENDED) == 1) {
399 acl = acl_get_fd(from_fd);
400 if (acl == NULL)
401 warn("failed to get acl entries while setting %s",
402 from);
403 else if (acl_set_fd(to_fd, acl) < 0)
404 warn("failed to set acl entries for %s", to);
405 }
406 (void)close(from_fd);
407 if (fchmod(to_fd, sbp->st_mode))
408 warn("%s: set mode (was: 0%03o)", to, oldmode);
409 /*
410 * XXX
411 * NFS doesn't support chflags; ignore errors unless there's reason
412 * to believe we're losing bits. (Note, this still won't be right
413 * if the server supports flags and we were trying to *remove* flags
414 * on a file that we copied, i.e., that we didn't create.)
415 */
416 errno = 0;
417 if (fchflags(to_fd, (u_long)sbp->st_flags))
418 if (errno != EOPNOTSUPP || sbp->st_flags != 0)
419 warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
420
421 tval[0].tv_sec = sbp->st_atime;
422 tval[1].tv_sec = sbp->st_mtime;
423 tval[0].tv_usec = tval[1].tv_usec = 0;
424 if (utimes(to, tval))
425 warn("%s: set times", to);
426
427 if (close(to_fd)) {
428 warn("%s", to);
429 return (1);
430 }
431
432 if (unlink(from)) {
433 warn("%s: remove", from);
434 return (1);
435 }
436 if (vflg)
437 printf("%s -> %s\n", from, to);
438 return (0);
439}
440
441int
442copy(char *from, char *to)
443{
444 int pid, status;
445
446 if ((pid = fork()) == 0) {
447 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
448 (char *)NULL);
449 warn("%s", _PATH_CP);
450 _exit(1);
451 }
452 if (waitpid(pid, &status, 0) == -1) {
453 warn("%s: waitpid", _PATH_CP);
454 return (1);
455 }
456 if (!WIFEXITED(status)) {
457 warnx("%s: did not terminate normally", _PATH_CP);
458 return (1);
459 }
460 if (WEXITSTATUS(status)) {
461 warnx("%s: terminated with %d (non-zero) status",
462 _PATH_CP, WEXITSTATUS(status));
463 return (1);
464 }
465 if (!(pid = vfork())) {
466 execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
467 warn("%s", _PATH_RM);
468 _exit(1);
469 }
470 if (waitpid(pid, &status, 0) == -1) {
471 warn("%s: waitpid", _PATH_RM);
472 return (1);
473 }
474 if (!WIFEXITED(status)) {
475 warnx("%s: did not terminate normally", _PATH_RM);
476 return (1);
477 }
478 if (WEXITSTATUS(status)) {
479 warnx("%s: terminated with %d (non-zero) status",
480 _PATH_RM, WEXITSTATUS(status));
481 return (1);
482 }
483 return (0);
484}
485#endif /* CROSS_DEVICE_MOVE */
486
487
488static int
489usage(void)
490{
491
492 (void)fprintf(stderr, "%s\n%s\n",
493 "usage: mv [-f | -i | -n] [-v] source target",
494 " mv [-f | -i | -n] [-v] source ... directory");
495 return EX_USAGE;
496}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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