2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Ken Smith of The State University of New York at Buffalo.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 static char const copyright[] =
36 "@(#) Copyright (c) 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/types.h>
49 #include <sys/param.h>
53 #include <sys/mount.h>
68 /* Exit code for a failed exec. */
69 #define EXEC_FAILED 127
71 int fflg, iflg, nflg, vflg;
73 static int copy(const char *, const char *);
74 static int do_move(const char *, const char *);
75 static int fastcopy(const char *, const char *, struct stat *);
76 static void usage(void);
79 main(int argc, char *argv[])
88 while ((ch = getopt(argc, argv, "finv")) != -1)
115 * If the stat on the target fails or the target isn't a directory,
116 * try the move. More than 2 arguments is an error in this case.
118 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
121 exit(do_move(argv[0], argv[1]));
124 /* It's a directory, move each file into it. */
125 if (strlen(argv[argc - 1]) > sizeof(path) - 1)
126 errx(1, "%s: destination pathname too long", *argv);
127 (void)strcpy(path, argv[argc - 1]);
128 baselen = strlen(path);
129 endp = &path[baselen];
130 if (!baselen || *(endp - 1) != '/') {
134 for (rval = 0; --argc; ++argv) {
136 * Find the last component of the source pathname. It
137 * may have trailing slashes.
139 p = *argv + strlen(*argv);
140 while (p != *argv && p[-1] == '/')
142 while (p != *argv && p[-1] != '/')
145 if ((baselen + (len = strlen(p))) >= PATH_MAX) {
146 warnx("%s: destination pathname too long", *argv);
149 memmove(endp, p, (size_t)len + 1);
150 if (do_move(*argv, path))
158 do_move(const char *from, const char *to)
165 * Check access. If interactive and file exists, ask user if it
166 * should be replaced. Otherwise if file exists but isn't writable
167 * make sure the user wants to clobber it.
169 if (!fflg && !access(to, F_OK)) {
171 /* prompt only if source exist */
172 if (lstat(from, &sb) == -1) {
177 #define YESNO "(y/n [n]) "
181 printf("%s not overwritten\n", to);
184 (void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
186 } else if (access(to, W_OK) && !stat(to, &sb)) {
187 strmode(sb.st_mode, modep);
188 (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
189 modep + 1, modep[9] == ' ' ? "" : " ",
190 user_from_uid((unsigned long)sb.st_uid, 0),
191 group_from_gid((unsigned long)sb.st_gid, 0), to, YESNO);
195 first = ch = getchar();
196 while (ch != '\n' && ch != EOF)
198 if (first != 'y' && first != 'Y') {
199 (void)fprintf(stderr, "not overwritten\n");
205 * Rename on FreeBSD will fail with EISDIR and ENOTDIR, before failing
206 * with EXDEV. Therefore, copy() doesn't have to perform the checks
207 * specified in the Step 3 of the POSIX mv specification.
209 if (!rename(from, to)) {
211 printf("%s -> %s\n", from, to);
215 if (errno == EXDEV) {
220 * If the source is a symbolic link and is on another
221 * filesystem, it can be recreated at the destination.
223 if (lstat(from, &sb) == -1) {
227 if (!S_ISLNK(sb.st_mode)) {
228 /* Can't mv(1) a mount point. */
229 if (realpath(from, path) == NULL) {
230 warn("cannot resolve %s: %s", from, path);
233 if (!statfs(path, &sfs) &&
234 !strcmp(path, sfs.f_mntonname)) {
235 warnx("cannot rename a mount point");
240 warn("rename %s to %s", from, to);
245 * If rename fails because we're trying to cross devices, and
246 * it's a regular file, do the copy internally; otherwise, use
249 if (lstat(from, &sb)) {
253 return (S_ISREG(sb.st_mode) ?
254 fastcopy(from, to, &sb) : copy(from, to));
258 fastcopy(const char *from, const char *to, struct stat *sbp)
260 struct timeval tval[2];
265 int nread, from_fd, to_fd;
267 if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
271 if (blen < sbp->st_blksize) {
274 if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
276 warnx("malloc failed");
279 blen = sbp->st_blksize;
282 open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
283 if (errno == EEXIST && unlink(to) == 0)
286 (void)close(from_fd);
289 while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
290 if (write(to_fd, bp, (size_t)nread) != nread) {
297 warn("%s: remove", to);
298 (void)close(from_fd);
303 oldmode = sbp->st_mode & ALLPERMS;
304 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
305 warn("%s: set owner/group (was: %lu/%lu)", to,
306 (u_long)sbp->st_uid, (u_long)sbp->st_gid);
307 if (oldmode & (S_ISUID | S_ISGID)) {
309 "%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
311 sbp->st_mode &= ~(S_ISUID | S_ISGID);
315 * POSIX 1003.2c states that if _POSIX_ACL_EXTENDED is in effect
316 * for dest_file, then its ACLs shall reflect the ACLs of the
319 if (fpathconf(to_fd, _PC_ACL_EXTENDED) == 1 &&
320 fpathconf(from_fd, _PC_ACL_EXTENDED) == 1) {
321 acl = acl_get_fd(from_fd);
323 warn("failed to get acl entries while setting %s",
325 else if (acl_set_fd(to_fd, acl) < 0)
326 warn("failed to set acl entries for %s", to);
328 (void)close(from_fd);
329 if (fchmod(to_fd, sbp->st_mode))
330 warn("%s: set mode (was: 0%03o)", to, oldmode);
333 * NFS doesn't support chflags; ignore errors unless there's reason
334 * to believe we're losing bits. (Note, this still won't be right
335 * if the server supports flags and we were trying to *remove* flags
336 * on a file that we copied, i.e., that we didn't create.)
339 if (fchflags(to_fd, (u_long)sbp->st_flags))
340 if (errno != EOPNOTSUPP || sbp->st_flags != 0)
341 warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
343 tval[0].tv_sec = sbp->st_atime;
344 tval[1].tv_sec = sbp->st_mtime;
345 tval[0].tv_usec = tval[1].tv_usec = 0;
346 if (utimes(to, tval))
347 warn("%s: set times", to);
355 warn("%s: remove", from);
359 printf("%s -> %s\n", from, to);
364 copy(const char *from, const char *to)
369 if (lstat(to, &sb) == 0) {
370 /* Destination path exists. */
371 if (S_ISDIR(sb.st_mode)) {
372 if (rmdir(to) != 0) {
373 warn("rmdir %s", to);
377 if (unlink(to) != 0) {
378 warn("unlink %s", to);
382 } else if (errno != ENOENT) {
387 /* Copy source to destination. */
388 if (!(pid = vfork())) {
389 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to,
393 if (waitpid(pid, &status, 0) == -1) {
394 warn("%s %s %s: waitpid", _PATH_CP, from, to);
397 if (!WIFEXITED(status)) {
398 warnx("%s %s %s: did not terminate normally",
402 switch (WEXITSTATUS(status)) {
406 warnx("%s %s %s: exec failed", _PATH_CP, from, to);
409 warnx("%s %s %s: terminated with %d (non-zero) status",
410 _PATH_CP, from, to, WEXITSTATUS(status));
414 /* Delete the source. */
415 if (!(pid = vfork())) {
416 execl(_PATH_RM, "mv", "-rf", "--", from, (char *)NULL);
419 if (waitpid(pid, &status, 0) == -1) {
420 warn("%s %s: waitpid", _PATH_RM, from);
423 if (!WIFEXITED(status)) {
424 warnx("%s %s: did not terminate normally", _PATH_RM, from);
427 switch (WEXITSTATUS(status)) {
431 warnx("%s %s: exec failed", _PATH_RM, from);
434 warnx("%s %s: terminated with %d (non-zero) status",
435 _PATH_RM, from, WEXITSTATUS(status));
445 (void)fprintf(stderr, "%s\n%s\n",
446 "usage: mv [-f | -i | -n] [-v] source target",
447 " mv [-f | -i | -n] [-v] source ... directory");