mv.c revision 30106
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.
353044Sdg *
3630106Swosch *	$Id: mv.c,v 1.14 1997/09/28 10:41:40 wosch Exp $
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
4020420Sstevestatic char const copyright[] =
411556Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\
421556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
431556Srgrimes#endif /* not lint */
441556Srgrimes
451556Srgrimes#ifndef lint
4620420Sstevestatic char const sccsid[] = "@(#)mv.c	8.2 (Berkeley) 4/2/94";
471556Srgrimes#endif /* not lint */
481556Srgrimes
491556Srgrimes#include <sys/param.h>
501556Srgrimes#include <sys/time.h>
511556Srgrimes#include <sys/wait.h>
521556Srgrimes#include <sys/stat.h>
531556Srgrimes
541556Srgrimes#include <err.h>
551556Srgrimes#include <errno.h>
561556Srgrimes#include <fcntl.h>
571556Srgrimes#include <stdio.h>
581556Srgrimes#include <stdlib.h>
591556Srgrimes#include <string.h>
601556Srgrimes#include <unistd.h>
611556Srgrimes
621556Srgrimes#include "pathnames.h"
631556Srgrimes
641556Srgrimesint fflg, iflg;
651556Srgrimes
661556Srgrimesint	copy __P((char *, char *));
671556Srgrimesint	do_move __P((char *, char *));
681556Srgrimesint	fastcopy __P((char *, char *, struct stat *));
691556Srgrimesvoid	usage __P((void));
701556Srgrimes
711556Srgrimesint
721556Srgrimesmain(argc, argv)
731556Srgrimes	int argc;
741556Srgrimes	char *argv[];
751556Srgrimes{
761556Srgrimes	register int baselen, len, rval;
771556Srgrimes	register char *p, *endp;
781556Srgrimes	struct stat sb;
791556Srgrimes	int ch;
801556Srgrimes	char path[MAXPATHLEN + 1];
811556Srgrimes
8224348Simp	while ((ch = getopt(argc, argv, "fi")) != -1)
831556Srgrimes		switch (ch) {
841556Srgrimes		case 'i':
8514154Swosch			iflg = 1;
8614166Swosch			fflg = 0;
871556Srgrimes			break;
881556Srgrimes		case 'f':
891556Srgrimes			fflg = 1;
9014166Swosch			iflg = 0;
911556Srgrimes			break;
921556Srgrimes		default:
931556Srgrimes			usage();
941556Srgrimes		}
9514305Swosch	argc -= optind;
961556Srgrimes	argv += optind;
971556Srgrimes
981556Srgrimes	if (argc < 2)
991556Srgrimes		usage();
1001556Srgrimes
1011556Srgrimes	/*
1021556Srgrimes	 * If the stat on the target fails or the target isn't a directory,
1031556Srgrimes	 * try the move.  More than 2 arguments is an error in this case.
1041556Srgrimes	 */
1051556Srgrimes	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
1061556Srgrimes		if (argc > 2)
1071556Srgrimes			usage();
1081556Srgrimes		exit(do_move(argv[0], argv[1]));
1091556Srgrimes	}
1101556Srgrimes
1111556Srgrimes	/* It's a directory, move each file into it. */
1121556Srgrimes	(void)strcpy(path, argv[argc - 1]);
1131556Srgrimes	baselen = strlen(path);
1141556Srgrimes	endp = &path[baselen];
1151556Srgrimes	*endp++ = '/';
1161556Srgrimes	++baselen;
1171556Srgrimes	for (rval = 0; --argc; ++argv) {
11811298Sbde		/*
11911298Sbde		 * Find the last component of the source pathname.  It
12011298Sbde		 * may have trailing slashes.
12111298Sbde		 */
12211298Sbde		p = *argv + strlen(*argv);
12311298Sbde		while (p != *argv && p[-1] == '/')
12411298Sbde			--p;
12511298Sbde		while (p != *argv && p[-1] != '/')
12611298Sbde			--p;
12711298Sbde
1281556Srgrimes		if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
1291556Srgrimes			warnx("%s: destination pathname too long", *argv);
1301556Srgrimes			rval = 1;
1311556Srgrimes		} else {
1321556Srgrimes			memmove(endp, p, len + 1);
1331556Srgrimes			if (do_move(*argv, path))
1341556Srgrimes				rval = 1;
1351556Srgrimes		}
1361556Srgrimes	}
1371556Srgrimes	exit(rval);
1381556Srgrimes}
1391556Srgrimes
1401556Srgrimesint
1411556Srgrimesdo_move(from, to)
1421556Srgrimes	char *from, *to;
1431556Srgrimes{
1441556Srgrimes	struct stat sb;
14529933Swosch	int ask, ch, first;
1461556Srgrimes	char modep[15];
1471556Srgrimes
1481556Srgrimes	/*
1491556Srgrimes	 * Check access.  If interactive and file exists, ask user if it
1501556Srgrimes	 * should be replaced.  Otherwise if file exists but isn't writable
1511556Srgrimes	 * make sure the user wants to clobber it.
1521556Srgrimes	 */
1531556Srgrimes	if (!fflg && !access(to, F_OK)) {
15414166Swosch
15514166Swosch		/* prompt only if source exist */
15614166Swosch	        if (lstat(from, &sb) == -1) {
15714305Swosch			warn("%s", from);
15814305Swosch			return (1);
15914166Swosch		}
16030106Swosch
16130106Swosch#define YESNO "(y/n [n]) "
1621556Srgrimes		ask = 0;
1631556Srgrimes		if (iflg) {
16430106Swosch			(void)fprintf(stderr, "overwrite %s? %s", to, YESNO);
1651556Srgrimes			ask = 1;
1661556Srgrimes		} else if (access(to, W_OK) && !stat(to, &sb)) {
1671556Srgrimes			strmode(sb.st_mode, modep);
16830106Swosch			(void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
1691556Srgrimes			    modep + 1, modep[9] == ' ' ? "" : " ",
1701556Srgrimes			    user_from_uid(sb.st_uid, 0),
17130106Swosch			    group_from_gid(sb.st_gid, 0), to, YESNO);
1721556Srgrimes			ask = 1;
1731556Srgrimes		}
1741556Srgrimes		if (ask) {
17529933Swosch			first = ch = getchar();
17629933Swosch			while (ch != '\n' && ch != EOF)
17729933Swosch				ch = getchar();
17830106Swosch			if (first != 'y' && first != 'Y') {
17930106Swosch				(void)fprintf(stderr, "not overwritten\n");
1801556Srgrimes				return (0);
18130106Swosch			}
1821556Srgrimes		}
1831556Srgrimes	}
1841556Srgrimes	if (!rename(from, to))
1851556Srgrimes		return (0);
1861556Srgrimes
1871556Srgrimes	if (errno != EXDEV) {
1881556Srgrimes		warn("rename %s to %s", from, to);
1891556Srgrimes		return (1);
1901556Srgrimes	}
1911556Srgrimes
1921556Srgrimes	/*
1931556Srgrimes	 * If rename fails because we're trying to cross devices, and
1941556Srgrimes	 * it's a regular file, do the copy internally; otherwise, use
1951556Srgrimes	 * cp and rm.
1961556Srgrimes	 */
1971556Srgrimes	if (stat(from, &sb)) {
1981556Srgrimes		warn("%s", from);
1991556Srgrimes		return (1);
2001556Srgrimes	}
2011556Srgrimes	return (S_ISREG(sb.st_mode) ?
2021556Srgrimes	    fastcopy(from, to, &sb) : copy(from, to));
2031556Srgrimes}
2041556Srgrimes
2051556Srgrimesint
2061556Srgrimesfastcopy(from, to, sbp)
2071556Srgrimes	char *from, *to;
2081556Srgrimes	struct stat *sbp;
2091556Srgrimes{
2101556Srgrimes	struct timeval tval[2];
2111556Srgrimes	static u_int blen;
2121556Srgrimes	static char *bp;
21323525Sguido	mode_t oldmode;
2141556Srgrimes	register int nread, from_fd, to_fd;
2151556Srgrimes
2161556Srgrimes	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
2171556Srgrimes		warn("%s", from);
2181556Srgrimes		return (1);
2191556Srgrimes	}
22023525Sguido	if (blen < sbp->st_blksize) {
22123525Sguido		if (bp != NULL)
22223525Sguido			free(bp);
22323525Sguido		if ((bp = malloc(sbp->st_blksize)) == NULL) {
22423525Sguido			blen = 0;
22523525Sguido			warnx("malloc failed");
22623525Sguido			return (1);
22723525Sguido		}
22823525Sguido		blen = sbp->st_blksize;
22923525Sguido	}
23023525Sguido	while ((to_fd =
23123525Sguido	    open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
23223525Sguido		if (errno == EEXIST && unlink(to) == 0)
23323525Sguido			continue;
2341556Srgrimes		warn("%s", to);
2351556Srgrimes		(void)close(from_fd);
2361556Srgrimes		return (1);
2371556Srgrimes	}
2381556Srgrimes	while ((nread = read(from_fd, bp, blen)) > 0)
2391556Srgrimes		if (write(to_fd, bp, nread) != nread) {
2401556Srgrimes			warn("%s", to);
2411556Srgrimes			goto err;
2421556Srgrimes		}
2431556Srgrimes	if (nread < 0) {
2441556Srgrimes		warn("%s", from);
2451556Srgrimeserr:		if (unlink(to))
2461556Srgrimes			warn("%s: remove", to);
2471556Srgrimes		(void)close(from_fd);
2481556Srgrimes		(void)close(to_fd);
2491556Srgrimes		return (1);
2501556Srgrimes	}
2511556Srgrimes	(void)close(from_fd);
2521556Srgrimes
25323525Sguido	oldmode = sbp->st_mode & ALLPERMS;
25423525Sguido	if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
25523525Sguido		warn("%s: set owner/group (was: %u/%u)", to, sbp->st_uid,
25623525Sguido		    sbp->st_gid);
25723525Sguido		if (oldmode & (S_ISUID | S_ISGID)) {
25823525Sguido			warnx(
25923525Sguido"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)",
26023525Sguido			    to, oldmode);
26123525Sguido			sbp->st_mode &= ~(S_ISUID | S_ISGID);
26223525Sguido		}
26323525Sguido	}
2641556Srgrimes	if (fchmod(to_fd, sbp->st_mode))
26523525Sguido		warn("%s: set mode (was: 0%03o)", to, oldmode);
2661556Srgrimes
2671556Srgrimes	tval[0].tv_sec = sbp->st_atime;
2681556Srgrimes	tval[1].tv_sec = sbp->st_mtime;
2691556Srgrimes	tval[0].tv_usec = tval[1].tv_usec = 0;
2701556Srgrimes	if (utimes(to, tval))
2711556Srgrimes		warn("%s: set times", to);
2721556Srgrimes
2731556Srgrimes	if (close(to_fd)) {
2741556Srgrimes		warn("%s", to);
2751556Srgrimes		return (1);
2761556Srgrimes	}
2771556Srgrimes
2781556Srgrimes	if (unlink(from)) {
2791556Srgrimes		warn("%s: remove", from);
2801556Srgrimes		return (1);
2811556Srgrimes	}
2821556Srgrimes	return (0);
2831556Srgrimes}
2841556Srgrimes
2851556Srgrimesint
2861556Srgrimescopy(from, to)
2871556Srgrimes	char *from, *to;
2881556Srgrimes{
2891556Srgrimes	int pid, status;
2901556Srgrimes
2911556Srgrimes	if ((pid = vfork()) == 0) {
2921556Srgrimes		execl(_PATH_CP, "mv", "-PRp", from, to, NULL);
2931556Srgrimes		warn("%s", _PATH_CP);
2941556Srgrimes		_exit(1);
2951556Srgrimes	}
2961556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
2971556Srgrimes		warn("%s: waitpid", _PATH_CP);
2981556Srgrimes		return (1);
2991556Srgrimes	}
3001556Srgrimes	if (!WIFEXITED(status)) {
3011556Srgrimes		warn("%s: did not terminate normally", _PATH_CP);
3021556Srgrimes		return (1);
3031556Srgrimes	}
3041556Srgrimes	if (WEXITSTATUS(status)) {
3051556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3061556Srgrimes		    _PATH_CP, WEXITSTATUS(status));
3071556Srgrimes		return (1);
3081556Srgrimes	}
3091556Srgrimes	if (!(pid = vfork())) {
3101556Srgrimes		execl(_PATH_RM, "mv", "-rf", from, NULL);
3111556Srgrimes		warn("%s", _PATH_RM);
3121556Srgrimes		_exit(1);
3131556Srgrimes	}
3141556Srgrimes	if (waitpid(pid, &status, 0) == -1) {
3151556Srgrimes		warn("%s: waitpid", _PATH_RM);
3161556Srgrimes		return (1);
3171556Srgrimes	}
3181556Srgrimes	if (!WIFEXITED(status)) {
3191556Srgrimes		warn("%s: did not terminate normally", _PATH_RM);
3201556Srgrimes		return (1);
3211556Srgrimes	}
3221556Srgrimes	if (WEXITSTATUS(status)) {
3231556Srgrimes		warn("%s: terminated with %d (non-zero) status",
3241556Srgrimes		    _PATH_RM, WEXITSTATUS(status));
3251556Srgrimes		return (1);
3261556Srgrimes	}
3271556Srgrimes	return (0);
3281556Srgrimes}
3291556Srgrimes
3301556Srgrimesvoid
3311556Srgrimesusage()
3321556Srgrimes{
33314305Swosch	(void)fprintf(stderr, "%s\n%s\n",
33414305Swosch		      "usage: mv [-f | -i] src target",
33514305Swosch		      "       mv [-f | -i] src1 ... srcN directory");
3361556Srgrimes	exit(1);
3371556Srgrimes}
338