xref: /csrg-svn/bin/mv/mv.c (revision 10641)
110047Ssam #ifndef lint
2*10641Smckusick static	char *sccsid = "@(#)mv.c	4.10 (Berkeley) 83/01/31";
310047Ssam #endif
48839Smckusick 
51216Sbill /*
61216Sbill  * mv file1 file2
71216Sbill  */
810047Ssam #include <sys/param.h>
910047Ssam #include <sys/stat.h>
101216Sbill 
111216Sbill #include <stdio.h>
1210047Ssam #include <dir.h>
1310047Ssam #include <errno.h>
141216Sbill #include <signal.h>
151216Sbill 
161216Sbill #define	DELIM	'/'
171216Sbill #define MODEBITS 07777
181216Sbill 
1910047Ssam #define	ISDIR(st)	(((st).st_mode&S_IFMT) == S_IFDIR)
2010047Ssam #define	ISLNK(st)	(((st).st_mode&S_IFMT) == S_IFLNK)
2110047Ssam #define	ISREG(st)	(((st).st_mode&S_IFMT) == S_IFREG)
2210047Ssam #define	ISDEV(st) \
2310047Ssam 	(((st).st_mode&S_IFMT) == S_IFCHR || ((st).st_mode&S_IFMT) == S_IFBLK)
2410047Ssam 
251216Sbill char	*sprintf();
261216Sbill char	*dname();
271216Sbill struct	stat s1, s2;
2810047Ssam int	iflag = 0;	/* interactive mode */
2910047Ssam int	fflag = 0;	/* force overwriting */
3010047Ssam extern	unsigned errno;
311216Sbill 
321216Sbill main(argc, argv)
3310047Ssam 	register char *argv[];
341216Sbill {
351216Sbill 	register i, r;
361931Sroot 	register char *arg;
37*10641Smckusick 	char *dest;
381216Sbill 
391216Sbill 	if (argc < 2)
401216Sbill 		goto usage;
4110047Ssam 	while (argc > 1 && *argv[1] == '-') {
421216Sbill 		argc--;
431931Sroot 		arg = *++argv;
441216Sbill 
451931Sroot 		/*
4610047Ssam 		 * all files following a null option
4710047Ssam 		 * are considered file names
481931Sroot 		 */
4910047Ssam 		if (*(arg+1) == '\0')
5010047Ssam 			break;
5110047Ssam 		while (*++arg != '\0') switch (*arg) {
521931Sroot 
5310047Ssam 		case 'i':
5410047Ssam 			iflag++;
5510047Ssam 			break;
561931Sroot 
5710047Ssam 		case 'f':
5810047Ssam 			fflag++;
5910047Ssam 			break;
601216Sbill 
6110047Ssam 		default:
6210047Ssam 			goto usage;
6310047Ssam 		}
641216Sbill 	}
651216Sbill 	if (argc < 3)
661216Sbill 		goto usage;
67*10641Smckusick 	dest = argv[argc-1];
68*10641Smckusick 	if (stat(dest, &s2) < 0)
69*10641Smckusick 		goto usage;
70*10641Smckusick 	if (ISDIR(s2)) {
7110114Ssam 		r = 0;
7210047Ssam 		for (i = 1; i < argc-1; i++)
7310047Ssam 			r |= movewithshortname(argv[i], dest);
7410047Ssam 		exit(r);
751216Sbill 	}
76*10641Smckusick 	if (argc > 3)
77*10641Smckusick 		goto usage;
78*10641Smckusick 	r = move(argv[1], argv[2]);
7910047Ssam 	exit(r);
8010047Ssam 	/*NOTREACHED*/
811216Sbill usage:
8210047Ssam 	fprintf(stderr,
8310047Ssam "usage: mv [-if] f1 ... fn d1 (where `fn' is a file or directory)\n");
8410047Ssam 	return (1);
851216Sbill }
861216Sbill 
8710047Ssam movewithshortname(src, dest)
8810047Ssam 	char *src, *dest;
8910047Ssam {
9010047Ssam 	register char *shortname;
9110047Ssam 	char target[MAXPATHLEN + 1];
9210047Ssam 
9310047Ssam 	shortname = dname(src);
9410047Ssam 	if (strlen(dest) + strlen(shortname) > MAXPATHLEN - 1) {
9510047Ssam 		error("%s/%s: pathname too long", dest,
9610047Ssam 			shortname);
9710047Ssam 		return (1);
9810047Ssam 	}
9910047Ssam 	sprintf(target, "%s/%s", dest, shortname);
10010047Ssam 	return (move(src, target));
10110047Ssam }
10210047Ssam 
1031216Sbill move(source, target)
10410047Ssam 	char *source, *target;
1051216Sbill {
1061216Sbill 
1078839Smckusick 	if (lstat(source, &s1) < 0) {
10810047Ssam 		error("cannot access %s", source);
10910047Ssam 		return (1);
1101216Sbill 	}
11110047Ssam 	/*
11210047Ssam 	 * First, try to rename source to destination.
11310047Ssam 	 * The only reason we continue on failure is if
11410047Ssam 	 * the move is on a nondirectory and not across
11510047Ssam 	 * file systems.
11610047Ssam 	 */
11710047Ssam 	if (lstat(target, &s2) >= 0) {
11810047Ssam 		if (iflag && !fflag && query("remove %s? ", target) == 0)
11910047Ssam 			return (1);
12010047Ssam 		if (s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino) {
12110047Ssam 			error("%s and %s are identical", source, target);
12210047Ssam 			return (1);
1231216Sbill 		}
12410047Ssam 		if (access(target, 2) < 0 && !fflag && isatty(fileno(stdin))) {
12510047Ssam 			if (query("override protection %o for %s? ",
12610047Ssam 			  s2.st_mode & MODEBITS, target) == 0)
12710047Ssam 				return (1);
1281216Sbill 		}
12910047Ssam 		if (rename(source, target) >= 0)
13010047Ssam 			return (0);
13110047Ssam 		if (errno != EXDEV) {
13210047Ssam 			Perror2(source, "rename");
13310047Ssam 			return (1);
13410047Ssam 		}
13510047Ssam 		if (ISDIR(s1)) {
13610047Ssam 			error("can't mv directories across file systems");
13710047Ssam 			return (1);
13810047Ssam 		}
13910047Ssam 		if (unlink(target) < 0) {
14010047Ssam 			error("cannot unlink %s", target);
14110047Ssam 			return (1);
14210047Ssam 		}
14310047Ssam 	} else {
14410047Ssam 		if (rename(source, target) >= 0)
14510047Ssam 			return (0);
14610047Ssam 		if (ISDIR(s1)) {
14710047Ssam 			Perror2(source, "rename");
14810047Ssam 			return (1);
14910047Ssam 		}
1501216Sbill 	}
15110047Ssam 	/*
15210047Ssam 	 * File can't be renamed, try to recreate the symbolic
15310047Ssam 	 * link or special device, or copy the file wholesale
15410047Ssam 	 * between file systems.
15510047Ssam 	 */
15610047Ssam 	if (ISLNK(s1)) {
1578839Smckusick 		register m;
15810047Ssam 		char symln[MAXPATHLEN];
1598839Smckusick 
1608839Smckusick 		if (readlink(source, symln, sizeof (symln)) < 0) {
16110047Ssam 			Perror(source);
1628839Smckusick 			return (1);
1638839Smckusick 		}
1648839Smckusick 		m = umask(~(s1.st_mode & MODEBITS));
1658839Smckusick 		if (symlink(symln, target) < 0) {
16610047Ssam 			Perror(target);
1678839Smckusick 			return (1);
1688839Smckusick 		}
16910047Ssam 		(void) umask(m);
17010047Ssam 		goto cleanup;
17110047Ssam 	}
17210047Ssam 	if (ISDEV(s1)) {
17310166Ssam 		time_t tv[2];
17410166Ssam 
17510047Ssam 		if (mknod(target, s1.st_mode, s1.st_rdev) < 0) {
17610047Ssam 			Perror(target);
17710047Ssam 			return (1);
17810047Ssam 		}
17910166Ssam 		/* kludge prior to utimes */
18010166Ssam 		tv[0] = s1.st_atime;
18110166Ssam 		tv[1] = s1.st_mtime;
18210166Ssam 		(void) utime(target, tv);
18310047Ssam 		goto cleanup;
18410047Ssam 	}
18510047Ssam 	if (ISREG(s1)) {
18610047Ssam 		int i, c, status;
18710166Ssam 		time_t tv[2];
18810047Ssam 
1891216Sbill 		i = fork();
1901216Sbill 		if (i == -1) {
19110047Ssam 			error("try again");
19210047Ssam 			return (1);
1931216Sbill 		}
1941216Sbill 		if (i == 0) {
1951216Sbill 			execl("/bin/cp", "cp", source, target, 0);
19610047Ssam 			error("cannot exec /bin/cp");
1971216Sbill 			exit(1);
1981216Sbill 		}
1991216Sbill 		while ((c = wait(&status)) != i && c != -1)
2001216Sbill 			;
2011216Sbill 		if (status != 0)
20210047Ssam 			return (1);
20310166Ssam 		/* kludge prior to utimes */
20410166Ssam 		tv[0] = s1.st_atime;
20510166Ssam 		tv[1] = s1.st_mtime;
20610166Ssam 		(void) utime(target, tv);
20710047Ssam 		goto cleanup;
2081216Sbill 	}
20910047Ssam 	error("%s: unknown file type %o", source, s1.st_mode);
21010047Ssam 	return (1);
2111216Sbill 
21210047Ssam cleanup:
2131216Sbill 	if (unlink(source) < 0) {
21410047Ssam 		error("cannot unlink %s", source);
21510047Ssam 		return (1);
2161216Sbill 	}
21710047Ssam 	return (0);
2181216Sbill }
2191216Sbill 
22010047Ssam /*VARARGS*/
22110047Ssam query(prompt, a1, a2)
22210047Ssam 	char *a1;
2231216Sbill {
22410047Ssam 	register char i, c;
2251216Sbill 
22610047Ssam 	fprintf(stderr, prompt, a1, a2);
22710047Ssam 	i = c = getchar();
22810047Ssam 	while (c != '\n' && c != EOF)
22910047Ssam 		c = getchar();
23010047Ssam 	return (i == 'y');
2311216Sbill }
2321216Sbill 
2331216Sbill char *
2341216Sbill dname(name)
23510047Ssam 	register char *name;
2361216Sbill {
2371216Sbill 	register char *p;
2381216Sbill 
2391216Sbill 	p = name;
2401216Sbill 	while (*p)
2411216Sbill 		if (*p++ == DELIM && *p)
2421216Sbill 			name = p;
2431216Sbill 	return name;
2441216Sbill }
2451216Sbill 
24610047Ssam /*VARARGS*/
24710047Ssam error(fmt, a1, a2)
24810047Ssam 	char *fmt;
2491216Sbill {
2501216Sbill 
25110047Ssam 	fprintf(stderr, "mv: ");
25210047Ssam 	fprintf(stderr, fmt, a1, a2);
25310047Ssam 	fprintf(stderr, "\n");
25410047Ssam }
2551216Sbill 
25610047Ssam Perror(s)
25710047Ssam 	char *s;
25810047Ssam {
25910047Ssam 	char buf[MAXPATHLEN + 10];
26010047Ssam 
26110047Ssam 	sprintf(buf, "mv: %s", s);
26210047Ssam 	perror(buf);
2631216Sbill }
2641216Sbill 
26510047Ssam Perror2(s1, s2)
26610047Ssam 	char *s1, *s2;
2671216Sbill {
26810047Ssam 	char buf[MAXPATHLEN + 20];
26910047Ssam 
27010047Ssam 	sprintf(buf, "mv: %s: %s", s1, s2);
27110047Ssam 	perror(buf);
2721216Sbill }
273