Home | History | Annotate | Line # | Download | only in mv
mv.c revision 1.10
      1  1.10      tls /*	$NetBSD: mv.c,v 1.10 1997/01/09 16:44:06 tls Exp $	*/
      2   1.9      cgd 
      3   1.1      cgd /*
      4   1.8  mycroft  * Copyright (c) 1989, 1993, 1994
      5   1.8  mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * This code is derived from software contributed to Berkeley by
      8   1.1      cgd  * Ken Smith of The State University of New York at Buffalo.
      9   1.1      cgd  *
     10   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1      cgd  * modification, are permitted provided that the following conditions
     12   1.1      cgd  * are met:
     13   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1      cgd  *    must display the following acknowledgement:
     20   1.1      cgd  *	This product includes software developed by the University of
     21   1.1      cgd  *	California, Berkeley and its contributors.
     22   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1      cgd  *    may be used to endorse or promote products derived from this software
     24   1.1      cgd  *    without specific prior written permission.
     25   1.1      cgd  *
     26   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1      cgd  * SUCH DAMAGE.
     37   1.1      cgd  */
     38   1.1      cgd 
     39   1.1      cgd #ifndef lint
     40   1.8  mycroft static char copyright[] =
     41   1.8  mycroft "@(#) Copyright (c) 1989, 1993, 1994\n\
     42   1.8  mycroft 	The Regents of the University of California.  All rights reserved.\n";
     43   1.1      cgd #endif /* not lint */
     44   1.1      cgd 
     45   1.1      cgd #ifndef lint
     46   1.9      cgd #if 0
     47   1.9      cgd static char sccsid[] = "@(#)mv.c	8.2 (Berkeley) 4/2/94";
     48   1.9      cgd #else
     49  1.10      tls static char rcsid[] = "$NetBSD: mv.c,v 1.10 1997/01/09 16:44:06 tls Exp $";
     50   1.9      cgd #endif
     51   1.1      cgd #endif /* not lint */
     52   1.1      cgd 
     53   1.1      cgd #include <sys/param.h>
     54   1.1      cgd #include <sys/time.h>
     55   1.1      cgd #include <sys/wait.h>
     56   1.1      cgd #include <sys/stat.h>
     57   1.8  mycroft 
     58   1.8  mycroft #include <err.h>
     59   1.8  mycroft #include <errno.h>
     60   1.1      cgd #include <fcntl.h>
     61   1.1      cgd #include <stdio.h>
     62   1.1      cgd #include <stdlib.h>
     63   1.1      cgd #include <string.h>
     64   1.8  mycroft #include <unistd.h>
     65   1.8  mycroft 
     66   1.1      cgd #include "pathnames.h"
     67   1.1      cgd 
     68   1.1      cgd int fflg, iflg;
     69   1.6      jtc int stdin_ok;
     70   1.1      cgd 
     71   1.8  mycroft int	copy __P((char *, char *));
     72   1.8  mycroft int	do_move __P((char *, char *));
     73   1.8  mycroft int	fastcopy __P((char *, char *, struct stat *));
     74   1.8  mycroft void	usage __P((void));
     75   1.8  mycroft 
     76   1.8  mycroft int
     77   1.1      cgd main(argc, argv)
     78   1.1      cgd 	int argc;
     79   1.8  mycroft 	char *argv[];
     80   1.1      cgd {
     81  1.10      tls 	int baselen, len, rval;
     82  1.10      tls 	char *p, *endp;
     83   1.1      cgd 	struct stat sb;
     84   1.1      cgd 	int ch;
     85   1.1      cgd 	char path[MAXPATHLEN + 1];
     86   1.1      cgd 
     87   1.8  mycroft 	while ((ch = getopt(argc, argv, "if")) != -1)
     88   1.8  mycroft 		switch (ch) {
     89   1.1      cgd 		case 'i':
     90   1.5      jtc 			fflg = 0;
     91   1.1      cgd 			iflg = 1;
     92   1.1      cgd 			break;
     93   1.1      cgd 		case 'f':
     94   1.5      jtc 			iflg = 0;
     95   1.1      cgd 			fflg = 1;
     96   1.1      cgd 			break;
     97   1.1      cgd 		case '?':
     98   1.1      cgd 		default:
     99   1.1      cgd 			usage();
    100   1.1      cgd 		}
    101   1.5      jtc 	argc -= optind;
    102   1.1      cgd 	argv += optind;
    103   1.1      cgd 
    104   1.1      cgd 	if (argc < 2)
    105   1.1      cgd 		usage();
    106   1.1      cgd 
    107   1.6      jtc 	stdin_ok = isatty(STDIN_FILENO);
    108   1.6      jtc 
    109   1.1      cgd 	/*
    110   1.1      cgd 	 * If the stat on the target fails or the target isn't a directory,
    111   1.1      cgd 	 * try the move.  More than 2 arguments is an error in this case.
    112   1.1      cgd 	 */
    113   1.1      cgd 	if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
    114   1.1      cgd 		if (argc > 2)
    115   1.1      cgd 			usage();
    116   1.1      cgd 		exit(do_move(argv[0], argv[1]));
    117   1.1      cgd 	}
    118   1.1      cgd 
    119   1.1      cgd 	/* It's a directory, move each file into it. */
    120   1.1      cgd 	(void)strcpy(path, argv[argc - 1]);
    121   1.1      cgd 	baselen = strlen(path);
    122   1.1      cgd 	endp = &path[baselen];
    123   1.1      cgd 	*endp++ = '/';
    124   1.1      cgd 	++baselen;
    125   1.8  mycroft 	for (rval = 0; --argc; ++argv) {
    126   1.8  mycroft 		if ((p = strrchr(*argv, '/')) == NULL)
    127   1.1      cgd 			p = *argv;
    128   1.1      cgd 		else
    129   1.1      cgd 			++p;
    130   1.8  mycroft 		if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
    131   1.8  mycroft 			warnx("%s: destination pathname too long", *argv);
    132   1.8  mycroft 			rval = 1;
    133   1.8  mycroft 		} else {
    134   1.8  mycroft 			memmove(endp, p, len + 1);
    135   1.8  mycroft 			if (do_move(*argv, path))
    136   1.8  mycroft 				rval = 1;
    137   1.1      cgd 		}
    138   1.1      cgd 	}
    139   1.8  mycroft 	exit(rval);
    140   1.1      cgd }
    141   1.1      cgd 
    142   1.8  mycroft int
    143   1.1      cgd do_move(from, to)
    144   1.1      cgd 	char *from, *to;
    145   1.1      cgd {
    146   1.1      cgd 	struct stat sb;
    147   1.8  mycroft 	char modep[15];
    148   1.1      cgd 
    149   1.8  mycroft 	/*
    150   1.8  mycroft 	 * (1)	If the destination path exists, the -f option is not specified
    151   1.6      jtc 	 *	and either of the following conditions are true:
    152   1.6      jtc 	 *
    153   1.6      jtc 	 *	(a) The perimissions of the destination path do not permit
    154   1.6      jtc 	 *	    writing and the standard input is a terminal.
    155   1.6      jtc 	 *	(b) The -i option is specified.
    156   1.6      jtc 	 *
    157   1.8  mycroft 	 *	the mv utility shall write a prompt to standard error and
    158   1.6      jtc 	 *	read a line from standard input.  If the response is not
    159   1.6      jtc 	 *	affirmative, mv shall do nothing more with the current
    160   1.6      jtc 	 *	source file...
    161   1.1      cgd 	 */
    162   1.1      cgd 	if (!fflg && !access(to, F_OK)) {
    163   1.8  mycroft 		int ask = 1;
    164   1.6      jtc 		int ch;
    165   1.6      jtc 
    166   1.1      cgd 		if (iflg) {
    167   1.1      cgd 			(void)fprintf(stderr, "overwrite %s? ", to);
    168   1.6      jtc 		} else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
    169   1.8  mycroft 			strmode(sb.st_mode, modep);
    170   1.8  mycroft 			(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
    171   1.8  mycroft 			    modep + 1, modep[9] == ' ' ? "" : " ",
    172   1.8  mycroft 			    user_from_uid(sb.st_uid, 0),
    173   1.8  mycroft 			    group_from_gid(sb.st_gid, 0), to);
    174   1.8  mycroft 		} else
    175   1.8  mycroft 			ask = 0;
    176   1.1      cgd 		if (ask) {
    177   1.1      cgd 			if ((ch = getchar()) != EOF && ch != '\n')
    178   1.1      cgd 				while (getchar() != '\n');
    179   1.7      jtc 			if (ch != 'y' && ch != 'Y')
    180   1.8  mycroft 				return (0);
    181   1.1      cgd 		}
    182   1.1      cgd 	}
    183   1.6      jtc 
    184   1.8  mycroft 	/*
    185   1.8  mycroft 	 * (2)	If rename() succeeds, mv shall do nothing more with the
    186   1.6      jtc 	 *	current source file.  If it fails for any other reason than
    187   1.6      jtc 	 *	EXDEV, mv shall write a diagnostic message to the standard
    188   1.6      jtc 	 *	error and do nothing more with the current source file.
    189   1.6      jtc 	 *
    190   1.6      jtc 	 * (3)	If the destination path exists, and it is a file of type
    191   1.6      jtc 	 *	directory and source_file is not a file of type directory,
    192   1.6      jtc 	 *	or it is a file not of type directory, and source file is
    193   1.8  mycroft 	 *	a file of type directory, mv shall write a diagnostic
    194   1.6      jtc 	 *	message to standard error, and do nothing more with the
    195   1.6      jtc 	 *	current source file...
    196   1.6      jtc 	 */
    197   1.1      cgd 	if (!rename(from, to))
    198   1.8  mycroft 		return (0);
    199   1.1      cgd 
    200   1.1      cgd 	if (errno != EXDEV) {
    201   1.8  mycroft 		warn("rename %s to %s", from, to);
    202   1.8  mycroft 		return (1);
    203   1.1      cgd 	}
    204   1.1      cgd 
    205   1.8  mycroft 	/*
    206   1.8  mycroft 	 * (4)	If the destination path exists, mv shall attempt to remove it.
    207   1.8  mycroft 	 *	If this fails for any reason, mv shall write a diagnostic
    208   1.6      jtc 	 *	message to the standard error and do nothing more with the
    209   1.6      jtc 	 *	current source file...
    210   1.6      jtc 	 */
    211   1.6      jtc 	if (!stat(to, &sb)) {
    212   1.6      jtc 		if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
    213   1.8  mycroft 			warn("can't remove %s", to);
    214   1.6      jtc 			return (1);
    215   1.6      jtc 		}
    216   1.6      jtc 	}
    217   1.6      jtc 
    218   1.8  mycroft 	/*
    219   1.8  mycroft 	 * (5)	The file hierarchy rooted in source_file shall be duplicated
    220   1.6      jtc 	 *	as a file hiearchy rooted in the destination path...
    221   1.1      cgd 	 */
    222   1.1      cgd 	if (stat(from, &sb)) {
    223   1.8  mycroft 		warn("%s", from);
    224   1.8  mycroft 		return (1);
    225   1.1      cgd 	}
    226   1.8  mycroft 	return (S_ISREG(sb.st_mode) ?
    227   1.1      cgd 	    fastcopy(from, to, &sb) : copy(from, to));
    228   1.1      cgd }
    229   1.1      cgd 
    230   1.8  mycroft int
    231   1.1      cgd fastcopy(from, to, sbp)
    232   1.1      cgd 	char *from, *to;
    233   1.1      cgd 	struct stat *sbp;
    234   1.1      cgd {
    235   1.1      cgd 	struct timeval tval[2];
    236   1.1      cgd 	static u_int blen;
    237   1.1      cgd 	static char *bp;
    238  1.10      tls 	int nread, from_fd, to_fd;
    239   1.1      cgd 
    240   1.1      cgd 	if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
    241   1.8  mycroft 		warn("%s", from);
    242   1.8  mycroft 		return (1);
    243   1.1      cgd 	}
    244   1.8  mycroft 	if ((to_fd =
    245   1.8  mycroft 	    open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
    246   1.8  mycroft 		warn("%s", to);
    247   1.1      cgd 		(void)close(from_fd);
    248   1.8  mycroft 		return (1);
    249   1.1      cgd 	}
    250   1.1      cgd 	if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
    251   1.8  mycroft 		warn(NULL);
    252   1.8  mycroft 		return (1);
    253   1.1      cgd 	}
    254   1.1      cgd 	while ((nread = read(from_fd, bp, blen)) > 0)
    255   1.1      cgd 		if (write(to_fd, bp, nread) != nread) {
    256   1.8  mycroft 			warn("%s", to);
    257   1.1      cgd 			goto err;
    258   1.1      cgd 		}
    259   1.1      cgd 	if (nread < 0) {
    260   1.8  mycroft 		warn("%s", from);
    261   1.8  mycroft err:		if (unlink(to))
    262   1.8  mycroft 			warn("%s: remove", to);
    263   1.1      cgd 		(void)close(from_fd);
    264   1.1      cgd 		(void)close(to_fd);
    265   1.8  mycroft 		return (1);
    266   1.1      cgd 	}
    267   1.8  mycroft 	(void)close(from_fd);
    268   1.1      cgd 
    269   1.8  mycroft 	if (fchown(to_fd, sbp->st_uid, sbp->st_gid))
    270   1.8  mycroft 		warn("%s: set owner/group", to);
    271   1.8  mycroft 	if (fchmod(to_fd, sbp->st_mode))
    272   1.8  mycroft 		warn("%s: set mode", to);
    273   1.1      cgd 
    274   1.1      cgd 	tval[0].tv_sec = sbp->st_atime;
    275   1.1      cgd 	tval[1].tv_sec = sbp->st_mtime;
    276   1.1      cgd 	tval[0].tv_usec = tval[1].tv_usec = 0;
    277   1.8  mycroft 	if (utimes(to, tval))
    278   1.8  mycroft 		warn("%s: set times", to);
    279   1.8  mycroft 
    280   1.8  mycroft 	if (close(to_fd)) {
    281   1.8  mycroft 		warn("%s", to);
    282   1.8  mycroft 		return (1);
    283   1.8  mycroft 	}
    284   1.8  mycroft 
    285   1.8  mycroft 	if (unlink(from)) {
    286   1.8  mycroft 		warn("%s: remove", from);
    287   1.8  mycroft 		return (1);
    288   1.8  mycroft 	}
    289   1.8  mycroft 	return (0);
    290   1.1      cgd }
    291   1.1      cgd 
    292   1.8  mycroft int
    293   1.1      cgd copy(from, to)
    294   1.1      cgd 	char *from, *to;
    295   1.1      cgd {
    296   1.1      cgd 	int pid, status;
    297   1.1      cgd 
    298   1.8  mycroft 	if ((pid = vfork()) == 0) {
    299   1.8  mycroft 		execl(_PATH_CP, "mv", "-PRp", from, to, NULL);
    300   1.8  mycroft 		warn("%s", _PATH_CP);
    301   1.1      cgd 		_exit(1);
    302   1.1      cgd 	}
    303   1.8  mycroft 	if (waitpid(pid, &status, 0) == -1) {
    304   1.8  mycroft 		warn("%s: waitpid", _PATH_CP);
    305   1.8  mycroft 		return (1);
    306   1.8  mycroft 	}
    307   1.8  mycroft 	if (!WIFEXITED(status)) {
    308   1.8  mycroft 		warn("%s: did not terminate normally", _PATH_CP);
    309   1.8  mycroft 		return (1);
    310   1.8  mycroft 	}
    311   1.8  mycroft 	if (WEXITSTATUS(status)) {
    312   1.8  mycroft 		warn("%s: terminated with %d (non-zero) status",
    313   1.8  mycroft 		    _PATH_CP, WEXITSTATUS(status));
    314   1.8  mycroft 		return (1);
    315   1.8  mycroft 	}
    316   1.1      cgd 	if (!(pid = vfork())) {
    317   1.1      cgd 		execl(_PATH_RM, "mv", "-rf", from, NULL);
    318   1.8  mycroft 		warn("%s", _PATH_RM);
    319   1.1      cgd 		_exit(1);
    320   1.1      cgd 	}
    321   1.8  mycroft 	if (waitpid(pid, &status, 0) == -1) {
    322   1.8  mycroft 		warn("%s: waitpid", _PATH_RM);
    323   1.8  mycroft 		return (1);
    324   1.8  mycroft 	}
    325   1.8  mycroft 	if (!WIFEXITED(status)) {
    326   1.8  mycroft 		warn("%s: did not terminate normally", _PATH_RM);
    327   1.8  mycroft 		return (1);
    328   1.8  mycroft 	}
    329   1.8  mycroft 	if (WEXITSTATUS(status)) {
    330   1.8  mycroft 		warn("%s: terminated with %d (non-zero) status",
    331   1.8  mycroft 		    _PATH_RM, WEXITSTATUS(status));
    332   1.8  mycroft 		return (1);
    333   1.8  mycroft 	}
    334   1.8  mycroft 	return (0);
    335   1.1      cgd }
    336   1.1      cgd 
    337   1.8  mycroft void
    338   1.8  mycroft usage()
    339   1.1      cgd {
    340   1.1      cgd 
    341   1.8  mycroft 	(void)fprintf(stderr, "usage: mv [-fi] source target\n");
    342   1.8  mycroft 	(void)fprintf(stderr, "       mv [-fi] source ... directory\n");
    343   1.1      cgd 	exit(1);
    344   1.1      cgd }
    345