Home | History | Annotate | Line # | Download | only in mv
mv.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1989 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * This code is derived from software contributed to Berkeley by
      6  1.1      cgd  * Ken Smith of The State University of New York at Buffalo.
      7  1.1      cgd  *
      8  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1      cgd  * modification, are permitted provided that the following conditions
     10  1.1      cgd  * are met:
     11  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1      cgd  *    must display the following acknowledgement:
     18  1.1      cgd  *	This product includes software developed by the University of
     19  1.1      cgd  *	California, Berkeley and its contributors.
     20  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1      cgd  *    may be used to endorse or promote products derived from this software
     22  1.1      cgd  *    without specific prior written permission.
     23  1.1      cgd  *
     24  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1      cgd  * SUCH DAMAGE.
     35  1.1      cgd  */
     36  1.1      cgd 
     37  1.1      cgd #ifndef lint
     38  1.1      cgd char copyright[] =
     39  1.1      cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
     40  1.1      cgd  All rights reserved.\n";
     41  1.1      cgd #endif /* not lint */
     42  1.1      cgd 
     43  1.1      cgd #ifndef lint
     44  1.4  mycroft /*static char sccsid[] = "from: @(#)mv.c	5.11 (Berkeley) 4/3/91";*/
     45  1.4  mycroft static char rcsid[] = "$Id: mv.c,v 1.4 1993/08/01 18:59:17 mycroft Exp $";
     46  1.1      cgd #endif /* not lint */
     47  1.1      cgd 
     48  1.1      cgd #include <sys/param.h>
     49  1.1      cgd #include <sys/time.h>
     50  1.1      cgd #include <sys/wait.h>
     51  1.1      cgd #include <sys/stat.h>
     52  1.1      cgd #include <fcntl.h>
     53  1.1      cgd #include <errno.h>
     54  1.1      cgd #include <unistd.h>
     55  1.1      cgd #include <stdio.h>
     56  1.1      cgd #include <stdlib.h>
     57  1.1      cgd #include <string.h>
     58  1.1      cgd #include "pathnames.h"
     59  1.1      cgd 
     60  1.1      cgd int fflg, iflg;
     61  1.1      cgd 
     62  1.1      cgd main(argc, argv)
     63  1.1      cgd 	int argc;
     64  1.1      cgd 	char **argv;
     65  1.1      cgd {
     66  1.1      cgd 	extern char *optarg;
     67  1.1      cgd 	extern int optind;
     68  1.1      cgd 	register int baselen, exitval, len;
     69  1.1      cgd 	register char *p, *endp;
     70  1.1      cgd 	struct stat sb;
     71  1.1      cgd 	int ch;
     72  1.1      cgd 	char path[MAXPATHLEN + 1];
     73  1.1      cgd 
     74  1.1      cgd 	while (((ch = getopt(argc, argv, "-if")) != EOF))
     75  1.1      cgd 		switch((char)ch) {
     76  1.1      cgd 		case 'i':
     77  1.1      cgd 			iflg = 1;
     78  1.1      cgd 			break;
     79  1.1      cgd 		case 'f':
     80  1.1      cgd 			fflg = 1;
     81  1.1      cgd 			break;
     82  1.1      cgd 		case '-':		/* undocumented; for compatibility */
     83  1.1      cgd 			goto endarg;
     84  1.1      cgd 		case '?':
     85  1.1      cgd 		default:
     86  1.1      cgd 			usage();
     87  1.1      cgd 		}
     88  1.1      cgd endarg:	argc -= optind;
     89  1.1      cgd 	argv += optind;
     90  1.1      cgd 
     91  1.1      cgd 	if (argc < 2)
     92  1.1      cgd 		usage();
     93  1.1      cgd 
     94  1.1      cgd 	/*
     95  1.1      cgd 	 * If the stat on the target fails or the target isn't a directory,
     96  1.1      cgd 	 * try the move.  More than 2 arguments is an error in this case.
     97  1.1      cgd 	 */
     98  1.1      cgd 	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
     99  1.1      cgd 		if (argc > 2)
    100  1.1      cgd 			usage();
    101  1.1      cgd 		exit(do_move(argv[0], argv[1]));
    102  1.1      cgd 	}
    103  1.1      cgd 
    104  1.1      cgd 	/* It's a directory, move each file into it. */
    105  1.1      cgd 	(void)strcpy(path, argv[argc - 1]);
    106  1.1      cgd 	baselen = strlen(path);
    107  1.1      cgd 	endp = &path[baselen];
    108  1.1      cgd 	*endp++ = '/';
    109  1.1      cgd 	++baselen;
    110  1.1      cgd 	for (exitval = 0; --argc; ++argv) {
    111  1.1      cgd 		if ((p = rindex(*argv, '/')) == NULL)
    112  1.1      cgd 			p = *argv;
    113  1.1      cgd 		else
    114  1.1      cgd 			++p;
    115  1.1      cgd 		if ((baselen + (len = strlen(p))) >= MAXPATHLEN)
    116  1.1      cgd 			(void)fprintf(stderr,
    117  1.1      cgd 			    "mv: %s: destination pathname too long\n", *argv);
    118  1.1      cgd 		else {
    119  1.1      cgd 			bcopy(p, endp, len + 1);
    120  1.1      cgd 			exitval |= do_move(*argv, path);
    121  1.1      cgd 		}
    122  1.1      cgd 	}
    123  1.1      cgd 	exit(exitval);
    124  1.1      cgd }
    125  1.1      cgd 
    126  1.1      cgd do_move(from, to)
    127  1.1      cgd 	char *from, *to;
    128  1.1      cgd {
    129  1.1      cgd 	struct stat sb;
    130  1.1      cgd 	int ask, ch;
    131  1.1      cgd 
    132  1.1      cgd 	/*
    133  1.1      cgd 	 * Check access.  If interactive and file exists, ask user if it
    134  1.1      cgd 	 * should be replaced.  Otherwise if file exists but isn't writable
    135  1.1      cgd 	 * make sure the user wants to clobber it.
    136  1.1      cgd 	 */
    137  1.1      cgd 	if (!fflg && !access(to, F_OK)) {
    138  1.1      cgd 		ask = 0;
    139  1.1      cgd 		if (iflg) {
    140  1.1      cgd 			(void)fprintf(stderr, "overwrite %s? ", to);
    141  1.1      cgd 			ask = 1;
    142  1.1      cgd 		}
    143  1.1      cgd 		else if (access(to, W_OK) && !stat(to, &sb)) {
    144  1.1      cgd 			(void)fprintf(stderr, "override mode %o on %s? ",
    145  1.1      cgd 			    sb.st_mode & 07777, to);
    146  1.1      cgd 			ask = 1;
    147  1.1      cgd 		}
    148  1.1      cgd 		if (ask) {
    149  1.1      cgd 			if ((ch = getchar()) != EOF && ch != '\n')
    150  1.1      cgd 				while (getchar() != '\n');
    151  1.1      cgd 			if (ch != 'y')
    152  1.1      cgd 				return(0);
    153  1.1      cgd 		}
    154  1.1      cgd 	}
    155  1.1      cgd 	if (!rename(from, to))
    156  1.1      cgd 		return(0);
    157  1.1      cgd 
    158  1.1      cgd 	if (errno != EXDEV) {
    159  1.1      cgd 		(void)fprintf(stderr,
    160  1.1      cgd 		    "mv: rename %s to %s: %s\n", from, to, strerror(errno));
    161  1.1      cgd 		return(1);
    162  1.1      cgd 	}
    163  1.1      cgd 
    164  1.1      cgd 	/*
    165  1.1      cgd 	 * If rename fails, and it's a regular file, do the copy internally;
    166  1.1      cgd 	 * otherwise, use cp and rm.
    167  1.1      cgd 	 */
    168  1.1      cgd 	if (stat(from, &sb)) {
    169  1.1      cgd 		(void)fprintf(stderr, "mv: %s: %s\n", from, strerror(errno));
    170  1.1      cgd 		return(1);
    171  1.1      cgd 	}
    172  1.1      cgd 	return(S_ISREG(sb.st_mode) ?
    173  1.1      cgd 	    fastcopy(from, to, &sb) : copy(from, to));
    174  1.1      cgd }
    175  1.1      cgd 
    176  1.1      cgd fastcopy(from, to, sbp)
    177  1.1      cgd 	char *from, *to;
    178  1.1      cgd 	struct stat *sbp;
    179  1.1      cgd {
    180  1.1      cgd 	struct timeval tval[2];
    181  1.1      cgd 	static u_int blen;
    182  1.1      cgd 	static char *bp;
    183  1.1      cgd 	register int nread, from_fd, to_fd;
    184  1.1      cgd 
    185  1.1      cgd 	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
    186  1.1      cgd 		error(from);
    187  1.1      cgd 		return(1);
    188  1.1      cgd 	}
    189  1.1      cgd 	if ((to_fd = open(to, O_CREAT|O_TRUNC|O_WRONLY, sbp->st_mode)) < 0) {
    190  1.1      cgd 		error(to);
    191  1.1      cgd 		(void)close(from_fd);
    192  1.1      cgd 		return(1);
    193  1.1      cgd 	}
    194  1.1      cgd 	if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
    195  1.1      cgd 		error(NULL);
    196  1.1      cgd 		return(1);
    197  1.1      cgd 	}
    198  1.1      cgd 	while ((nread = read(from_fd, bp, blen)) > 0)
    199  1.1      cgd 		if (write(to_fd, bp, nread) != nread) {
    200  1.1      cgd 			error(to);
    201  1.1      cgd 			goto err;
    202  1.1      cgd 		}
    203  1.1      cgd 	if (nread < 0) {
    204  1.1      cgd 		error(from);
    205  1.1      cgd err:		(void)unlink(to);
    206  1.1      cgd 		(void)close(from_fd);
    207  1.1      cgd 		(void)close(to_fd);
    208  1.1      cgd 		return(1);
    209  1.1      cgd 	}
    210  1.1      cgd 	(void)fchown(to_fd, sbp->st_uid, sbp->st_gid);
    211  1.1      cgd 	(void)fchmod(to_fd, sbp->st_mode);
    212  1.1      cgd 
    213  1.1      cgd 	(void)close(from_fd);
    214  1.1      cgd 	(void)close(to_fd);
    215  1.1      cgd 
    216  1.1      cgd 	tval[0].tv_sec = sbp->st_atime;
    217  1.1      cgd 	tval[1].tv_sec = sbp->st_mtime;
    218  1.1      cgd 	tval[0].tv_usec = tval[1].tv_usec = 0;
    219  1.1      cgd 	(void)utimes(to, tval);
    220  1.1      cgd 	(void)unlink(from);
    221  1.1      cgd 	return(0);
    222  1.1      cgd }
    223  1.1      cgd 
    224  1.1      cgd copy(from, to)
    225  1.1      cgd 	char *from, *to;
    226  1.1      cgd {
    227  1.1      cgd 	int pid, status;
    228  1.1      cgd 
    229  1.1      cgd 	if (!(pid = vfork())) {
    230  1.1      cgd 		execl(_PATH_CP, "mv", "-pr", from, to, NULL);
    231  1.1      cgd 		error(_PATH_CP);
    232  1.1      cgd 		_exit(1);
    233  1.1      cgd 	}
    234  1.1      cgd 	(void)waitpid(pid, &status, 0);
    235  1.1      cgd 	if (!WIFEXITED(status) || WEXITSTATUS(status))
    236  1.1      cgd 		return(1);
    237  1.1      cgd 	if (!(pid = vfork())) {
    238  1.1      cgd 		execl(_PATH_RM, "mv", "-rf", from, NULL);
    239  1.1      cgd 		error(_PATH_RM);
    240  1.1      cgd 		_exit(1);
    241  1.1      cgd 	}
    242  1.1      cgd 	(void)waitpid(pid, &status, 0);
    243  1.1      cgd 	return(!WIFEXITED(status) || WEXITSTATUS(status));
    244  1.1      cgd }
    245  1.1      cgd 
    246  1.1      cgd error(s)
    247  1.1      cgd 	char *s;
    248  1.1      cgd {
    249  1.1      cgd 	if (s)
    250  1.1      cgd 		(void)fprintf(stderr, "mv: %s: %s\n", s, strerror(errno));
    251  1.1      cgd 	else
    252  1.1      cgd 		(void)fprintf(stderr, "mv: %s\n", strerror(errno));
    253  1.1      cgd }
    254  1.1      cgd 
    255  1.1      cgd usage()
    256  1.1      cgd {
    257  1.1      cgd 	(void)fprintf(stderr,
    258  1.1      cgd "usage: mv [-if] src target;\n   or: mv [-if] src1 ... srcN directory\n");
    259  1.1      cgd 	exit(1);
    260  1.1      cgd }
    261