mv.c revision 36049
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. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3820420Sstevestatic char const copyright[] = 391556Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\ 401556Srgrimes The Regents of the University of California. All rights reserved.\n"; 411556Srgrimes#endif /* not lint */ 421556Srgrimes 431556Srgrimes#ifndef lint 4436049Scharnier#if 0 4536049Scharnierstatic char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 4636049Scharnier#endif 4736049Scharnierstatic const char rcsid[] = 4836049Scharnier "$Id$"; 491556Srgrimes#endif /* not lint */ 501556Srgrimes 511556Srgrimes#include <sys/param.h> 521556Srgrimes#include <sys/time.h> 531556Srgrimes#include <sys/wait.h> 541556Srgrimes#include <sys/stat.h> 5531664Seivind#include <sys/mount.h> 561556Srgrimes 571556Srgrimes#include <err.h> 581556Srgrimes#include <errno.h> 591556Srgrimes#include <fcntl.h> 601556Srgrimes#include <stdio.h> 611556Srgrimes#include <stdlib.h> 621556Srgrimes#include <string.h> 631556Srgrimes#include <unistd.h> 641556Srgrimes 651556Srgrimes#include "pathnames.h" 661556Srgrimes 671556Srgrimesint fflg, iflg; 681556Srgrimes 691556Srgrimesint copy __P((char *, char *)); 701556Srgrimesint do_move __P((char *, char *)); 711556Srgrimesint fastcopy __P((char *, char *, struct stat *)); 721556Srgrimesvoid usage __P((void)); 731556Srgrimes 741556Srgrimesint 751556Srgrimesmain(argc, argv) 761556Srgrimes int argc; 771556Srgrimes char *argv[]; 781556Srgrimes{ 791556Srgrimes register int baselen, len, rval; 801556Srgrimes register char *p, *endp; 811556Srgrimes struct stat sb; 821556Srgrimes int ch; 831556Srgrimes char path[MAXPATHLEN + 1]; 841556Srgrimes 8524348Simp while ((ch = getopt(argc, argv, "fi")) != -1) 861556Srgrimes switch (ch) { 871556Srgrimes case 'i': 8814154Swosch iflg = 1; 8914166Swosch fflg = 0; 901556Srgrimes break; 911556Srgrimes case 'f': 921556Srgrimes fflg = 1; 9314166Swosch iflg = 0; 941556Srgrimes break; 951556Srgrimes default: 961556Srgrimes usage(); 971556Srgrimes } 9814305Swosch argc -= optind; 991556Srgrimes argv += optind; 1001556Srgrimes 1011556Srgrimes if (argc < 2) 1021556Srgrimes usage(); 1031556Srgrimes 1041556Srgrimes /* 1051556Srgrimes * If the stat on the target fails or the target isn't a directory, 1061556Srgrimes * try the move. More than 2 arguments is an error in this case. 1071556Srgrimes */ 1081556Srgrimes if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 1091556Srgrimes if (argc > 2) 1101556Srgrimes usage(); 1111556Srgrimes exit(do_move(argv[0], argv[1])); 1121556Srgrimes } 1131556Srgrimes 1141556Srgrimes /* It's a directory, move each file into it. */ 1151556Srgrimes (void)strcpy(path, argv[argc - 1]); 1161556Srgrimes baselen = strlen(path); 1171556Srgrimes endp = &path[baselen]; 1181556Srgrimes *endp++ = '/'; 1191556Srgrimes ++baselen; 1201556Srgrimes for (rval = 0; --argc; ++argv) { 12111298Sbde /* 12211298Sbde * Find the last component of the source pathname. It 12311298Sbde * may have trailing slashes. 12411298Sbde */ 12511298Sbde p = *argv + strlen(*argv); 12611298Sbde while (p != *argv && p[-1] == '/') 12711298Sbde --p; 12811298Sbde while (p != *argv && p[-1] != '/') 12911298Sbde --p; 13011298Sbde 1311556Srgrimes if ((baselen + (len = strlen(p))) >= MAXPATHLEN) { 1321556Srgrimes warnx("%s: destination pathname too long", *argv); 1331556Srgrimes rval = 1; 1341556Srgrimes } else { 1351556Srgrimes memmove(endp, p, len + 1); 1361556Srgrimes if (do_move(*argv, path)) 1371556Srgrimes rval = 1; 1381556Srgrimes } 1391556Srgrimes } 1401556Srgrimes exit(rval); 1411556Srgrimes} 1421556Srgrimes 1431556Srgrimesint 1441556Srgrimesdo_move(from, to) 1451556Srgrimes char *from, *to; 1461556Srgrimes{ 1471556Srgrimes struct stat sb; 14829933Swosch int ask, ch, first; 1491556Srgrimes char modep[15]; 1501556Srgrimes 1511556Srgrimes /* 1521556Srgrimes * Check access. If interactive and file exists, ask user if it 1531556Srgrimes * should be replaced. Otherwise if file exists but isn't writable 1541556Srgrimes * make sure the user wants to clobber it. 1551556Srgrimes */ 1561556Srgrimes if (!fflg && !access(to, F_OK)) { 15714166Swosch 15814166Swosch /* prompt only if source exist */ 15914166Swosch if (lstat(from, &sb) == -1) { 16014305Swosch warn("%s", from); 16114305Swosch return (1); 16214166Swosch } 16330106Swosch 16430106Swosch#define YESNO "(y/n [n]) " 1651556Srgrimes ask = 0; 1661556Srgrimes if (iflg) { 16730106Swosch (void)fprintf(stderr, "overwrite %s? %s", to, YESNO); 1681556Srgrimes ask = 1; 1691556Srgrimes } else if (access(to, W_OK) && !stat(to, &sb)) { 1701556Srgrimes strmode(sb.st_mode, modep); 17130106Swosch (void)fprintf(stderr, "override %s%s%s/%s for %s? %s", 1721556Srgrimes modep + 1, modep[9] == ' ' ? "" : " ", 1731556Srgrimes user_from_uid(sb.st_uid, 0), 17430106Swosch group_from_gid(sb.st_gid, 0), to, YESNO); 1751556Srgrimes ask = 1; 1761556Srgrimes } 1771556Srgrimes if (ask) { 17829933Swosch first = ch = getchar(); 17929933Swosch while (ch != '\n' && ch != EOF) 18029933Swosch ch = getchar(); 18130106Swosch if (first != 'y' && first != 'Y') { 18230106Swosch (void)fprintf(stderr, "not overwritten\n"); 1831556Srgrimes return (0); 18430106Swosch } 1851556Srgrimes } 1861556Srgrimes } 1871556Srgrimes if (!rename(from, to)) 1881556Srgrimes return (0); 1891556Srgrimes 19031664Seivind if (errno == EXDEV) { 19131664Seivind struct statfs sfs; 19231664Seivind char path[MAXPATHLEN]; 19331664Seivind 19431664Seivind /* Can't mv(1) a mount point. */ 19531664Seivind if (realpath(from, path) == NULL) { 19631664Seivind warnx("cannot resolve %s: %s", from, path); 19731664Seivind return (1); 19831664Seivind } 19931664Seivind if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) { 20031664Seivind warnx("cannot rename a mount point"); 20131664Seivind return (1); 20231664Seivind } 20331664Seivind } else { 2041556Srgrimes warn("rename %s to %s", from, to); 2051556Srgrimes return (1); 2061556Srgrimes } 2071556Srgrimes 2081556Srgrimes /* 2091556Srgrimes * If rename fails because we're trying to cross devices, and 2101556Srgrimes * it's a regular file, do the copy internally; otherwise, use 2111556Srgrimes * cp and rm. 2121556Srgrimes */ 2131556Srgrimes if (stat(from, &sb)) { 2141556Srgrimes warn("%s", from); 2151556Srgrimes return (1); 2161556Srgrimes } 2171556Srgrimes return (S_ISREG(sb.st_mode) ? 2181556Srgrimes fastcopy(from, to, &sb) : copy(from, to)); 2191556Srgrimes} 2201556Srgrimes 2211556Srgrimesint 2221556Srgrimesfastcopy(from, to, sbp) 2231556Srgrimes char *from, *to; 2241556Srgrimes struct stat *sbp; 2251556Srgrimes{ 2261556Srgrimes struct timeval tval[2]; 2271556Srgrimes static u_int blen; 2281556Srgrimes static char *bp; 22923525Sguido mode_t oldmode; 2301556Srgrimes register int nread, from_fd, to_fd; 2311556Srgrimes 2321556Srgrimes if ((from_fd = open(from, O_RDONLY, 0)) < 0) { 2331556Srgrimes warn("%s", from); 2341556Srgrimes return (1); 2351556Srgrimes } 23623525Sguido if (blen < sbp->st_blksize) { 23723525Sguido if (bp != NULL) 23823525Sguido free(bp); 23923525Sguido if ((bp = malloc(sbp->st_blksize)) == NULL) { 24023525Sguido blen = 0; 24123525Sguido warnx("malloc failed"); 24223525Sguido return (1); 24323525Sguido } 24423525Sguido blen = sbp->st_blksize; 24523525Sguido } 24623525Sguido while ((to_fd = 24723525Sguido open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) { 24823525Sguido if (errno == EEXIST && unlink(to) == 0) 24923525Sguido continue; 2501556Srgrimes warn("%s", to); 2511556Srgrimes (void)close(from_fd); 2521556Srgrimes return (1); 2531556Srgrimes } 2541556Srgrimes while ((nread = read(from_fd, bp, blen)) > 0) 2551556Srgrimes if (write(to_fd, bp, nread) != nread) { 2561556Srgrimes warn("%s", to); 2571556Srgrimes goto err; 2581556Srgrimes } 2591556Srgrimes if (nread < 0) { 2601556Srgrimes warn("%s", from); 2611556Srgrimeserr: if (unlink(to)) 2621556Srgrimes warn("%s: remove", to); 2631556Srgrimes (void)close(from_fd); 2641556Srgrimes (void)close(to_fd); 2651556Srgrimes return (1); 2661556Srgrimes } 2671556Srgrimes (void)close(from_fd); 2681556Srgrimes 26923525Sguido oldmode = sbp->st_mode & ALLPERMS; 27023525Sguido if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) { 27123525Sguido warn("%s: set owner/group (was: %u/%u)", to, sbp->st_uid, 27223525Sguido sbp->st_gid); 27323525Sguido if (oldmode & (S_ISUID | S_ISGID)) { 27423525Sguido warnx( 27523525Sguido"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)", 27623525Sguido to, oldmode); 27723525Sguido sbp->st_mode &= ~(S_ISUID | S_ISGID); 27823525Sguido } 27923525Sguido } 2801556Srgrimes if (fchmod(to_fd, sbp->st_mode)) 28123525Sguido warn("%s: set mode (was: 0%03o)", to, oldmode); 2821556Srgrimes 2831556Srgrimes tval[0].tv_sec = sbp->st_atime; 2841556Srgrimes tval[1].tv_sec = sbp->st_mtime; 2851556Srgrimes tval[0].tv_usec = tval[1].tv_usec = 0; 2861556Srgrimes if (utimes(to, tval)) 2871556Srgrimes warn("%s: set times", to); 2881556Srgrimes 2891556Srgrimes if (close(to_fd)) { 2901556Srgrimes warn("%s", to); 2911556Srgrimes return (1); 2921556Srgrimes } 2931556Srgrimes 2941556Srgrimes if (unlink(from)) { 2951556Srgrimes warn("%s: remove", from); 2961556Srgrimes return (1); 2971556Srgrimes } 2981556Srgrimes return (0); 2991556Srgrimes} 3001556Srgrimes 3011556Srgrimesint 3021556Srgrimescopy(from, to) 3031556Srgrimes char *from, *to; 3041556Srgrimes{ 3051556Srgrimes int pid, status; 3061556Srgrimes 3071556Srgrimes if ((pid = vfork()) == 0) { 3081556Srgrimes execl(_PATH_CP, "mv", "-PRp", from, to, NULL); 3091556Srgrimes warn("%s", _PATH_CP); 3101556Srgrimes _exit(1); 3111556Srgrimes } 3121556Srgrimes if (waitpid(pid, &status, 0) == -1) { 3131556Srgrimes warn("%s: waitpid", _PATH_CP); 3141556Srgrimes return (1); 3151556Srgrimes } 3161556Srgrimes if (!WIFEXITED(status)) { 3171556Srgrimes warn("%s: did not terminate normally", _PATH_CP); 3181556Srgrimes return (1); 3191556Srgrimes } 3201556Srgrimes if (WEXITSTATUS(status)) { 3211556Srgrimes warn("%s: terminated with %d (non-zero) status", 3221556Srgrimes _PATH_CP, WEXITSTATUS(status)); 3231556Srgrimes return (1); 3241556Srgrimes } 3251556Srgrimes if (!(pid = vfork())) { 3261556Srgrimes execl(_PATH_RM, "mv", "-rf", from, NULL); 3271556Srgrimes warn("%s", _PATH_RM); 3281556Srgrimes _exit(1); 3291556Srgrimes } 3301556Srgrimes if (waitpid(pid, &status, 0) == -1) { 3311556Srgrimes warn("%s: waitpid", _PATH_RM); 3321556Srgrimes return (1); 3331556Srgrimes } 3341556Srgrimes if (!WIFEXITED(status)) { 3351556Srgrimes warn("%s: did not terminate normally", _PATH_RM); 3361556Srgrimes return (1); 3371556Srgrimes } 3381556Srgrimes if (WEXITSTATUS(status)) { 3391556Srgrimes warn("%s: terminated with %d (non-zero) status", 3401556Srgrimes _PATH_RM, WEXITSTATUS(status)); 3411556Srgrimes return (1); 3421556Srgrimes } 3431556Srgrimes return (0); 3441556Srgrimes} 3451556Srgrimes 3461556Srgrimesvoid 3471556Srgrimesusage() 3481556Srgrimes{ 34914305Swosch (void)fprintf(stderr, "%s\n%s\n", 35030727Shelbig "usage: mv [-f | -i] source target", 35130727Shelbig " mv [-f | -i] source ... directory"); 3521556Srgrimes exit(1); 3531556Srgrimes} 354