mv.c revision 50544
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1989, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Ken Smith of The State University of New York at Buffalo.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3820420Sstevestatic char const copyright[] =
391556Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\
401556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411556Srgrimes#endif /* not lint */
421556Srgrimes
431556Srgrimes#ifndef lint
4436049Scharnier#if 0
4536049Scharnierstatic char sccsid[] = "@(#)mv.c	8.2 (Berkeley) 4/2/94";
4636049Scharnier#endif
4736049Scharnierstatic const char rcsid[] =
4850471Speter  "$FreeBSD: head/bin/mv/mv.c 50544 1999-08-29 08:21:16Z mharo $";
491556Srgrimes#endif /* not lint */
501556Srgrimes
511556Srgrimes#include <sys/param.h>
521556Srgrimes#include <sys/time.h>
531556Srgrimes#include <sys/wait.h>
541556Srgrimes#include <sys/stat.h>
5531664Seivind#include <sys/mount.h>
561556Srgrimes
571556Srgrimes#include <err.h>
581556Srgrimes#include <errno.h>
591556Srgrimes#include <fcntl.h>
601556Srgrimes#include <stdio.h>
611556Srgrimes#include <stdlib.h>
621556Srgrimes#include <string.h>
6350544Smharo#include <sysexits.h>
641556Srgrimes#include <unistd.h>
651556Srgrimes
661556Srgrimes#include "pathnames.h"
671556Srgrimes
6850544Smharoint fflg, iflg, vflg;
691556Srgrimes
701556Srgrimesint	copy __P((char *, char *));
711556Srgrimesint	do_move __P((char *, char *));
721556Srgrimesint	fastcopy __P((char *, char *, struct stat *));
731556Srgrimesvoid	usage __P((void));
741556Srgrimes
751556Srgrimesint
761556Srgrimesmain(argc, argv)
771556Srgrimes	int argc;
781556Srgrimes	char *argv[];
791556Srgrimes{
801556Srgrimes	register int baselen, len, rval;
811556Srgrimes	register char *p, *endp;
821556Srgrimes	struct stat sb;
831556Srgrimes	int ch;
8436785Simp	char path[MAXPATHLEN];
851556Srgrimes
8650544Smharo	while ((ch = getopt(argc, argv, "fiv")) != -1)
871556Srgrimes		switch (ch) {
881556Srgrimes		case 'i':
8914154Swosch			iflg = 1;
9014166Swosch			fflg = 0;
911556Srgrimes			break;
921556Srgrimes		case 'f':
931556Srgrimes			fflg = 1;
9414166Swosch			iflg = 0;
951556Srgrimes			break;
9650544Smharo		case 'v':
9750544Smharo			vflg = 1;
9850544Smharo			break;
991556Srgrimes		default:
1001556Srgrimes			usage();
1011556Srgrimes		}
10214305Swosch	argc -= optind;
1031556Srgrimes	argv += optind;
1041556Srgrimes
1051556Srgrimes	if (argc < 2)
1061556Srgrimes		usage();
1071556Srgrimes
1081556Srgrimes	/*
1091556Srgrimes	 * If the stat on the target fails or the target isn't a directory,
1101556Srgrimes	 * try the move.  More than 2 arguments is an error in this case.
1111556Srgrimes	 */
1121556Srgrimes	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
1131556Srgrimes		if (argc > 2)
1141556Srgrimes			usage();
1151556Srgrimes		exit(do_move(argv[0], argv[1]));
1161556Srgrimes	}
1171556Srgrimes
1181556Srgrimes	/* It's a directory, move each file into it. */
11936785Simp	if (strlen(argv[argc - 1]) > sizeof(path) - 1)
12036785Simp		errx(1, "%s: destination pathname too long", *argv);
1211556Srgrimes	(void)strcpy(path, argv[argc - 1]);
1221556Srgrimes	baselen = strlen(path);
1231556Srgrimes	endp = &path[baselen];
12436383Ssteve	if (!baselen || *(endp - 1) != '/') {
12536383Ssteve		*endp++ = '/';
12636383Ssteve		++baselen;
12736383Ssteve	}
1281556Srgrimes	for (rval = 0; --argc; ++argv) {
12911298Sbde		/*
13011298Sbde		 * Find the last component of the source pathname.  It
13111298Sbde		 * may have trailing slashes.
13211298Sbde		 */
13311298Sbde		p = *argv + strlen(*argv);
13411298Sbde		while (p != *argv && p[-1] == '/')
13511298Sbde			--p;
13611298Sbde		while (p != *argv && p[-1] != '/')
13711298Sbde			--p;
13811298Sbde
1391556Srgrimes		if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
1401556Srgrimes			warnx("%s: destination pathname too long", *argv);
1411556Srgrimes			rval = 1;
1421556Srgrimes		} else {
1431556Srgrimes			memmove(endp, p, len + 1);
1441556Srgrimes			if (do_move(*argv, path))
1451556Srgrimes				rval = 1;
1461556Srgrimes		}
1471556Srgrimes	}
1481556Srgrimes	exit(rval);
1491556Srgrimes}
1501556Srgrimes
1511556Srgrimesint
1521556Srgrimesdo_move(from, to)
1531556Srgrimes	char *from, *to;
1541556Srgrimes{
1551556Srgrimes	struct stat sb;
15629933Swosch	int ask, ch, first;
1571556Srgrimes	char modep[15];
1581556Srgrimes
1591556Srgrimes	/*
1601556Srgrimes	 * Check access.  If interactive and file exists, ask user if it
1611556Srgrimes	 * should be replaced.  Otherwise if file exists but isn't writable
1621556Srgrimes	 * make sure the user wants to clobber it.
1631556Srgrimes	 */
1641556Srgrimes	if (!fflg && !access(to, F_OK)) {
16514166Swosch
16614166Swosch		/* prompt only if source exist */
16714166Swosch	        if (lstat(from, &sb) == -1) {
16814305Swosch			warn("%s", from);
16914305Swosch			return (1);
17014166Swosch		}
17130106Swosch
17230106Swosch#define YESNO "(y/n [n]) "
1731556Srgrimes		ask = 0;
1741556Srgrimes		if (iflg) {
17530106Swosch			(void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
1761556Srgrimes			ask = 1;
1771556Srgrimes		} else if (access(to, W_OK) && !stat(to, &sb)) {
1781556Srgrimes			strmode(sb.st_mode, modep);
17930106Swosch			(void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
1801556Srgrimes			    modep + 1, modep[9] == ' ' ? "" : " ",
1811556Srgrimes			    user_from_uid(sb.st_uid, 0),
18230106Swosch			    group_from_gid(sb.st_gid, 0), to, YESNO);
1831556Srgrimes			ask = 1;
1841556Srgrimes		}
1851556Srgrimes		if (ask) {
18629933Swosch			first = ch = getchar();
18729933Swosch			while (ch != '\n' && ch != EOF)
18829933Swosch				ch = getchar();
18930106Swosch			if (first != 'y' && first != 'Y') {
19030106Swosch				(void)fprintf(stderr, "not overwritten\n");
1911556Srgrimes				return (0);
19230106Swosch			}
1931556Srgrimes		}
1941556Srgrimes	}
19550544Smharo	if (!rename(from, to)) {
19650544Smharo		if (vflg)
19750544Smharo			printf("%s -> %s\n", from, to);
1981556Srgrimes		return (0);
19950544Smharo	}
2001556Srgrimes
20131664Seivind	if (errno == EXDEV) {
20231664Seivind		struct statfs sfs;
20331664Seivind		char path[MAXPATHLEN];
20431664Seivind
20531664Seivind		/* Can't mv(1) a mount point. */
20631664Seivind		if (realpath(from, path) == NULL) {
20731664Seivind			warnx("cannot resolve %s: %s", from, path);
20831664Seivind			return (1);
20931664Seivind		}
21031664Seivind		if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) {
21131664Seivind			warnx("cannot rename a mount point");
21231664Seivind			return (1);
21331664Seivind		}
21431664Seivind	} else {
2151556Srgrimes		warn("rename %s to %s", from, to);
2161556Srgrimes		return (1);
2171556Srgrimes	}
2181556Srgrimes
2191556Srgrimes	/*
2201556Srgrimes	 * If rename fails because we're trying to cross devices, and
2211556Srgrimes	 * it's a regular file, do the copy internally; otherwise, use
2221556Srgrimes	 * cp and rm.
2231556Srgrimes	 */
2241556Srgrimes	if (stat(from, &sb)) {
2251556Srgrimes		warn("%s", from);
2261556Srgrimes		return (1);
2271556Srgrimes	}
2281556Srgrimes	return (S_ISREG(sb.st_mode) ?
2291556Srgrimes	    fastcopy(from, to, &sb) : copy(from, to));
2301556Srgrimes}
2311556Srgrimes
2321556Srgrimesint
2331556Srgrimesfastcopy(from, to, sbp)
2341556Srgrimes	char *from, *to;
2351556Srgrimes	struct stat *sbp;
2361556Srgrimes{
2371556Srgrimes	struct timeval tval[2];
2381556Srgrimes	static u_int blen;
2391556Srgrimes	static char *bp;
24023525Sguido	mode_t oldmode;
2411556Srgrimes	register int nread, from_fd, to_fd;
2421556Srgrimes
2431556Srgrimes	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
2441556Srgrimes		warn("%s", from);
2451556Srgrimes		return (1);
2461556Srgrimes	}
24723525Sguido	if (blen < sbp->st_blksize) {
24823525Sguido		if (bp != NULL)
24923525Sguido			free(bp);
25023525Sguido		if ((bp = malloc(sbp->st_blksize)) == NULL) {
25123525Sguido			blen = 0;
25223525Sguido			warnx("malloc failed");
25323525Sguido			return (1);
25423525Sguido		}
25523525Sguido		blen = sbp->st_blksize;
25623525Sguido	}
25723525Sguido	while ((to_fd =
25823525Sguido	    open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
25923525Sguido		if (errno == EEXIST && unlink(to) == 0)
26023525Sguido			continue;
2611556Srgrimes		warn("%s", to);
2621556Srgrimes		(void)close(from_fd);
2631556Srgrimes		return (1);
2641556Srgrimes	}
2651556Srgrimes	while ((nread = read(from_fd, bp, blen)) > 0)
2661556Srgrimes		if (write(to_fd, bp, nread) != nread) {
2671556Srgrimes			warn("%s", to);
2681556Srgrimes			goto err;
2691556Srgrimes		}
2701556Srgrimes	if (nread < 0) {
2711556Srgrimes		warn("%s", from);
2721556Srgrimeserr:		if (unlink(to))
2731556Srgrimes			warn("%s: remove", to);
2741556Srgrimes		(void)close(from_fd);
2751556Srgrimes		(void)close(to_fd);
2761556Srgrimes		return (1);
2771556Srgrimes	}
2781556Srgrimes	(void)close(from_fd);
2791556Srgrimes
28023525Sguido	oldmode = sbp->st_mode & ALLPERMS;
28123525Sguido	if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
28237245Sbde		warn("%s: set owner/group (was: %lu/%lu)", to,
28337245Sbde		    (u_long)sbp->st_uid, (u_long)sbp->st_gid);
28423525Sguido		if (oldmode & (S_ISUID | S_ISGID)) {
28523525Sguido			warnx(
28623525Sguido"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
28723525Sguido			    to, oldmode);
28823525Sguido			sbp->st_mode &= ~(S_ISUID | S_ISGID);
28923525Sguido		}
29023525Sguido	}
2911556Srgrimes	if (fchmod(to_fd, sbp->st_mode))
29223525Sguido		warn("%s: set mode (was: 0%03o)", to, oldmode);
2931556Srgrimes
2941556Srgrimes	tval[0].tv_sec = sbp->st_atime;
2951556Srgrimes	tval[1].tv_sec = sbp->st_mtime;
2961556Srgrimes	tval[0].tv_usec = tval[1].tv_usec = 0;
2971556Srgrimes	if (utimes(to, tval))
2981556Srgrimes		warn("%s: set times", to);
2991556Srgrimes
3001556Srgrimes	if (close(to_fd)) {
3011556Srgrimes		warn("%s", to);
3021556Srgrimes		return (1);
3031556Srgrimes	}
3041556Srgrimes
3051556Srgrimes	if (unlink(from)) {
3061556Srgrimes		warn("%s: remove", from);
3071556Srgrimes		return (1);
3081556Srgrimes	}
30950544Smharo	if (vflg)
31050544Smharo		printf("%s -> %s\n", from, to);
3111556Srgrimes	return (0);
3121556Srgrimes}
3131556Srgrimes
3141556Srgrimesint
3151556Srgrimescopy(from, to)
3161556Srgrimes	char *from, *to;
3171556Srgrimes{
3181556Srgrimes	int pid, status;
3191556Srgrimes
32040301Sdes	if ((pid = fork()) == 0) {
32150544Smharo		execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", from, to, NULL);
3221556Srgrimes		warn("%s", _PATH_CP);
3231556Srgrimes		_exit(1);
3241556Srgrimes	}
3251556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
3261556Srgrimes		warn("%s: waitpid", _PATH_CP);
3271556Srgrimes		return (1);
3281556Srgrimes	}
3291556Srgrimes	if (!WIFEXITED(status)) {
3301556Srgrimes		warn("%s: did not terminate normally", _PATH_CP);
3311556Srgrimes		return (1);
3321556Srgrimes	}
3331556Srgrimes	if (WEXITSTATUS(status)) {
3341556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3351556Srgrimes		    _PATH_CP, WEXITSTATUS(status));
3361556Srgrimes		return (1);
3371556Srgrimes	}
3381556Srgrimes	if (!(pid = vfork())) {
3391556Srgrimes		execl(_PATH_RM, "mv", "-rf", from, NULL);
3401556Srgrimes		warn("%s", _PATH_RM);
3411556Srgrimes		_exit(1);
3421556Srgrimes	}
3431556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
3441556Srgrimes		warn("%s: waitpid", _PATH_RM);
3451556Srgrimes		return (1);
3461556Srgrimes	}
3471556Srgrimes	if (!WIFEXITED(status)) {
3481556Srgrimes		warn("%s: did not terminate normally", _PATH_RM);
3491556Srgrimes		return (1);
3501556Srgrimes	}
3511556Srgrimes	if (WEXITSTATUS(status)) {
3521556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3531556Srgrimes		    _PATH_RM, WEXITSTATUS(status));
3541556Srgrimes		return (1);
3551556Srgrimes	}
3561556Srgrimes	return (0);
3571556Srgrimes}
3581556Srgrimes
3591556Srgrimesvoid
3601556Srgrimesusage()
3611556Srgrimes{
36250544Smharo
36314305Swosch	(void)fprintf(stderr, "%s\n%s\n",
36450544Smharo		      "usage: mv [-f | -i] [-v] source target",
36550544Smharo		      "       mv [-f | -i] [-v] source ... directory");
36650544Smharo	exit(EX_USAGE);
3671556Srgrimes}
368