xref: /csrg-svn/bin/mv/mv.c (revision 66627)
119845Sdist /*
2*66627Sbostic  * Copyright (c) 1989, 1993, 1994
360673Sbostic  *	The Regents of the University of California.  All rights reserved.
439246Sbostic  *
539246Sbostic  * This code is derived from software contributed to Berkeley by
639246Sbostic  * Ken Smith of The State University of New York at Buffalo.
739246Sbostic  *
842536Sbostic  * %sccs.include.redist.c%
919845Sdist  */
1019845Sdist 
1110047Ssam #ifndef lint
1260673Sbostic static char copyright[] =
13*66627Sbostic "@(#) Copyright (c) 1989, 1993, 1994\n\
1460673Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1534045Sbostic #endif /* not lint */
168839Smckusick 
1719845Sdist #ifndef lint
18*66627Sbostic static char sccsid[] = "@(#)mv.c	8.2 (Berkeley) 04/02/94";
1934045Sbostic #endif /* not lint */
2019845Sdist 
2110047Ssam #include <sys/param.h>
2239246Sbostic #include <sys/time.h>
2339246Sbostic #include <sys/wait.h>
2410047Ssam #include <sys/stat.h>
2556905Sbostic 
2659503Sbostic #include <err.h>
2756905Sbostic #include <errno.h>
2847751Sbostic #include <fcntl.h>
291216Sbill #include <stdio.h>
3047751Sbostic #include <stdlib.h>
3139246Sbostic #include <string.h>
3256905Sbostic #include <unistd.h>
3356905Sbostic 
3439246Sbostic #include "pathnames.h"
351216Sbill 
3639246Sbostic int fflg, iflg;
371216Sbill 
3856905Sbostic int	copy __P((char *, char *));
3956905Sbostic int	do_move __P((char *, char *));
4056905Sbostic int	fastcopy __P((char *, char *, struct stat *));
4156905Sbostic void	usage __P((void));
4256905Sbostic 
4356905Sbostic int
main(argc,argv)441216Sbill main(argc, argv)
4539246Sbostic 	int argc;
4656905Sbostic 	char *argv[];
471216Sbill {
48*66627Sbostic 	register int baselen, len, rval;
4939246Sbostic 	register char *p, *endp;
5047751Sbostic 	struct stat sb;
5139246Sbostic 	int ch;
5239246Sbostic 	char path[MAXPATHLEN + 1];
531216Sbill 
54*66627Sbostic 	while ((ch = getopt(argc, argv, "-if")) != EOF)
55*66627Sbostic 		switch (ch) {
5639246Sbostic 		case 'i':
5747751Sbostic 			iflg = 1;
5839246Sbostic 			break;
5934045Sbostic 		case 'f':
6047751Sbostic 			fflg = 1;
6110047Ssam 			break;
6256905Sbostic 		case '-':		/* Undocumented; for compatibility. */
6339246Sbostic 			goto endarg;
6434045Sbostic 		case '?':
6510047Ssam 		default:
6634045Sbostic 			usage();
6710047Ssam 		}
6839246Sbostic endarg:	argc -= optind;
6939246Sbostic 	argv += optind;
7034045Sbostic 
7134045Sbostic 	if (argc < 2)
7234045Sbostic 		usage();
7339246Sbostic 
7439246Sbostic 	/*
7547751Sbostic 	 * If the stat on the target fails or the target isn't a directory,
7647751Sbostic 	 * try the move.  More than 2 arguments is an error in this case.
7739246Sbostic 	 */
7847751Sbostic 	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
7939246Sbostic 		if (argc > 2)
8039246Sbostic 			usage();
8139246Sbostic 		exit(do_move(argv[0], argv[1]));
821216Sbill 	}
831216Sbill 
8447751Sbostic 	/* It's a directory, move each file into it. */
8539246Sbostic 	(void)strcpy(path, argv[argc - 1]);
8639246Sbostic 	baselen = strlen(path);
8739246Sbostic 	endp = &path[baselen];
8839246Sbostic 	*endp++ = '/';
8939246Sbostic 	++baselen;
90*66627Sbostic 	for (rval = 0; --argc; ++argv) {
9159503Sbostic 		if ((p = strrchr(*argv, '/')) == NULL)
9239246Sbostic 			p = *argv;
9339246Sbostic 		else
9439246Sbostic 			++p;
9556905Sbostic 		if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
9659503Sbostic 			warnx("%s: destination pathname too long", *argv);
97*66627Sbostic 			rval = 1;
9856905Sbostic 		} else {
9959503Sbostic 			memmove(endp, p, len + 1);
100*66627Sbostic 			if (do_move(*argv, path))
101*66627Sbostic 				rval = 1;
10239246Sbostic 		}
10310047Ssam 	}
104*66627Sbostic 	exit(rval);
10510047Ssam }
10610047Ssam 
10756905Sbostic int
do_move(from,to)10839246Sbostic do_move(from, to)
10939246Sbostic 	char *from, *to;
1101216Sbill {
11147751Sbostic 	struct stat sb;
11239246Sbostic 	int ask, ch;
113*66627Sbostic 	char modep[15];
1141216Sbill 
11510047Ssam 	/*
11647751Sbostic 	 * Check access.  If interactive and file exists, ask user if it
11739246Sbostic 	 * should be replaced.  Otherwise if file exists but isn't writable
11839246Sbostic 	 * make sure the user wants to clobber it.
11910047Ssam 	 */
12039246Sbostic 	if (!fflg && !access(to, F_OK)) {
12139246Sbostic 		ask = 0;
12239246Sbostic 		if (iflg) {
12339246Sbostic 			(void)fprintf(stderr, "overwrite %s? ", to);
12439246Sbostic 			ask = 1;
125*66627Sbostic 		} else if (access(to, W_OK) && !stat(to, &sb)) {
126*66627Sbostic 			strmode(sb.st_mode, modep);
127*66627Sbostic 			(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
128*66627Sbostic 			    modep + 1, modep[9] == ' ' ? "" : " ",
129*66627Sbostic 			    user_from_uid(sb.st_uid, 0),
130*66627Sbostic 			    group_from_gid(sb.st_gid, 0), to);
13139246Sbostic 			ask = 1;
13239246Sbostic 		}
13339246Sbostic 		if (ask) {
13439246Sbostic 			if ((ch = getchar()) != EOF && ch != '\n')
13539246Sbostic 				while (getchar() != '\n');
13639246Sbostic 			if (ch != 'y')
13756905Sbostic 				return (0);
13839246Sbostic 		}
1391216Sbill 	}
14039246Sbostic 	if (!rename(from, to))
14156905Sbostic 		return (0);
14247751Sbostic 
14311642Ssam 	if (errno != EXDEV) {
14459503Sbostic 		warn("rename %s to %s", from, to);
14556905Sbostic 		return (1);
14611642Ssam 	}
14747751Sbostic 
14810047Ssam 	/*
149*66627Sbostic 	 * If rename fails because we're trying to cross devices, and
150*66627Sbostic 	 * it's a regular file, do the copy internally; otherwise, use
151*66627Sbostic 	 * cp and rm.
15210047Ssam 	 */
15347751Sbostic 	if (stat(from, &sb)) {
15459503Sbostic 		warn("%s", from);
15556905Sbostic 		return (1);
15610047Ssam 	}
15756905Sbostic 	return (S_ISREG(sb.st_mode) ?
15847751Sbostic 	    fastcopy(from, to, &sb) : copy(from, to));
15939246Sbostic }
16010166Ssam 
16156905Sbostic int
fastcopy(from,to,sbp)16239246Sbostic fastcopy(from, to, sbp)
16339246Sbostic 	char *from, *to;
16439246Sbostic 	struct stat *sbp;
16539246Sbostic {
16639246Sbostic 	struct timeval tval[2];
16739246Sbostic 	static u_int blen;
16839246Sbostic 	static char *bp;
16939246Sbostic 	register int nread, from_fd, to_fd;
17022601Sserge 
17139246Sbostic 	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
17259503Sbostic 		warn("%s", from);
17356905Sbostic 		return (1);
17410047Ssam 	}
175*66627Sbostic 	if ((to_fd =
176*66627Sbostic 	    open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
17759503Sbostic 		warn("%s", to);
17839246Sbostic 		(void)close(from_fd);
17956905Sbostic 		return (1);
18039246Sbostic 	}
18139246Sbostic 	if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
18259503Sbostic 		warn(NULL);
18356905Sbostic 		return (1);
18439246Sbostic 	}
18539246Sbostic 	while ((nread = read(from_fd, bp, blen)) > 0)
18639246Sbostic 		if (write(to_fd, bp, nread) != nread) {
18759503Sbostic 			warn("%s", to);
18839246Sbostic 			goto err;
1891216Sbill 		}
19039246Sbostic 	if (nread < 0) {
19159503Sbostic 		warn("%s", from);
19259503Sbostic err:		if (unlink(to))
19359503Sbostic 			warn("%s: remove", to);
19439246Sbostic 		(void)close(from_fd);
19539246Sbostic 		(void)close(to_fd);
19656905Sbostic 		return (1);
1971216Sbill 	}
19839246Sbostic 	(void)close(from_fd);
1991216Sbill 
20059503Sbostic 	if (fchown(to_fd, sbp->st_uid, sbp->st_gid))
20159503Sbostic 		warn("%s: set owner/group", to);
20259503Sbostic 	if (fchmod(to_fd, sbp->st_mode))
20359503Sbostic 		warn("%s: set mode", to);
20459503Sbostic 
20539246Sbostic 	tval[0].tv_sec = sbp->st_atime;
20639246Sbostic 	tval[1].tv_sec = sbp->st_mtime;
20739246Sbostic 	tval[0].tv_usec = tval[1].tv_usec = 0;
20859503Sbostic 	if (utimes(to, tval))
20959503Sbostic 		warn("%s: set times", to);
21059503Sbostic 
21159503Sbostic 	if (close(to_fd)) {
21259503Sbostic 		warn("%s", to);
21359503Sbostic 		return (1);
21459503Sbostic 	}
21559503Sbostic 
21659503Sbostic 	if (unlink(from)) {
21759503Sbostic 		warn("%s: remove", from);
21859503Sbostic 		return (1);
21959503Sbostic 	}
22056905Sbostic 	return (0);
2211216Sbill }
2221216Sbill 
22356905Sbostic int
copy(from,to)22439246Sbostic copy(from, to)
22539246Sbostic 	char *from, *to;
2261216Sbill {
22739246Sbostic 	int pid, status;
2281216Sbill 
229*66627Sbostic 	if ((pid = vfork()) == 0) {
230*66627Sbostic 		execl(_PATH_CP, "mv", "-PRp", from, to, NULL);
23159503Sbostic 		warn("%s", _PATH_CP);
23239246Sbostic 		_exit(1);
23339246Sbostic 	}
23459503Sbostic 	if (waitpid(pid, &status, 0) == -1) {
23559503Sbostic 		warn("%s: waitpid", _PATH_CP);
23656905Sbostic 		return (1);
23759503Sbostic 	}
23859503Sbostic 	if (!WIFEXITED(status)) {
23959503Sbostic 		warn("%s: did not terminate normally", _PATH_CP);
24059503Sbostic 		return (1);
24159503Sbostic 	}
24259503Sbostic 	if (WEXITSTATUS(status)) {
24359503Sbostic 		warn("%s: terminated with %d (non-zero) status",
24459503Sbostic 		    _PATH_CP, WEXITSTATUS(status));
24559503Sbostic 		return (1);
24659503Sbostic 	}
24739246Sbostic 	if (!(pid = vfork())) {
24847754Sbostic 		execl(_PATH_RM, "mv", "-rf", from, NULL);
24959503Sbostic 		warn("%s", _PATH_RM);
25039246Sbostic 		_exit(1);
25139246Sbostic 	}
25259503Sbostic 	if (waitpid(pid, &status, 0) == -1) {
25359503Sbostic 		warn("%s: waitpid", _PATH_RM);
25459503Sbostic 		return (1);
25559503Sbostic 	}
25659503Sbostic 	if (!WIFEXITED(status)) {
25759503Sbostic 		warn("%s: did not terminate normally", _PATH_RM);
25859503Sbostic 		return (1);
25959503Sbostic 	}
26059503Sbostic 	if (WEXITSTATUS(status)) {
26159503Sbostic 		warn("%s: terminated with %d (non-zero) status",
26259503Sbostic 		    _PATH_RM, WEXITSTATUS(status));
26359503Sbostic 		return (1);
26459503Sbostic 	}
26559503Sbostic 	return (0);
2661216Sbill }
2671216Sbill 
26856905Sbostic void
usage()26934045Sbostic usage()
27034045Sbostic {
27139246Sbostic 	(void)fprintf(stderr,
27239246Sbostic "usage: mv [-if] src target;\n   or: mv [-if] src1 ... srcN directory\n");
27334045Sbostic 	exit(1);
27434045Sbostic }
275