mv.c revision 29933
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. 353044Sdg * 3629933Swosch * $Id: mv.c,v 1.13 1997/03/28 15:24:26 imp Exp $ 371556Srgrimes */ 381556Srgrimes 391556Srgrimes#ifndef lint 4020420Sstevestatic char const copyright[] = 411556Srgrimes"@(#) Copyright (c) 1989, 1993, 1994\n\ 421556Srgrimes The Regents of the University of California. All rights reserved.\n"; 431556Srgrimes#endif /* not lint */ 441556Srgrimes 451556Srgrimes#ifndef lint 4620420Sstevestatic char const sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 471556Srgrimes#endif /* not lint */ 481556Srgrimes 491556Srgrimes#include <sys/param.h> 501556Srgrimes#include <sys/time.h> 511556Srgrimes#include <sys/wait.h> 521556Srgrimes#include <sys/stat.h> 531556Srgrimes 541556Srgrimes#include <err.h> 551556Srgrimes#include <errno.h> 561556Srgrimes#include <fcntl.h> 571556Srgrimes#include <stdio.h> 581556Srgrimes#include <stdlib.h> 591556Srgrimes#include <string.h> 601556Srgrimes#include <unistd.h> 611556Srgrimes 621556Srgrimes#include "pathnames.h" 631556Srgrimes 641556Srgrimesint fflg, iflg; 651556Srgrimes 661556Srgrimesint copy __P((char *, char *)); 671556Srgrimesint do_move __P((char *, char *)); 681556Srgrimesint fastcopy __P((char *, char *, struct stat *)); 691556Srgrimesvoid usage __P((void)); 701556Srgrimes 711556Srgrimesint 721556Srgrimesmain(argc, argv) 731556Srgrimes int argc; 741556Srgrimes char *argv[]; 751556Srgrimes{ 761556Srgrimes register int baselen, len, rval; 771556Srgrimes register char *p, *endp; 781556Srgrimes struct stat sb; 791556Srgrimes int ch; 801556Srgrimes char path[MAXPATHLEN + 1]; 811556Srgrimes 8224348Simp while ((ch = getopt(argc, argv, "fi")) != -1) 831556Srgrimes switch (ch) { 841556Srgrimes case 'i': 8514154Swosch iflg = 1; 8614166Swosch fflg = 0; 871556Srgrimes break; 881556Srgrimes case 'f': 891556Srgrimes fflg = 1; 9014166Swosch iflg = 0; 911556Srgrimes break; 921556Srgrimes default: 931556Srgrimes usage(); 941556Srgrimes } 9514305Swosch argc -= optind; 961556Srgrimes argv += optind; 971556Srgrimes 981556Srgrimes if (argc < 2) 991556Srgrimes usage(); 1001556Srgrimes 1011556Srgrimes /* 1021556Srgrimes * If the stat on the target fails or the target isn't a directory, 1031556Srgrimes * try the move. More than 2 arguments is an error in this case. 1041556Srgrimes */ 1051556Srgrimes if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 1061556Srgrimes if (argc > 2) 1071556Srgrimes usage(); 1081556Srgrimes exit(do_move(argv[0], argv[1])); 1091556Srgrimes } 1101556Srgrimes 1111556Srgrimes /* It's a directory, move each file into it. */ 1121556Srgrimes (void)strcpy(path, argv[argc - 1]); 1131556Srgrimes baselen = strlen(path); 1141556Srgrimes endp = &path[baselen]; 1151556Srgrimes *endp++ = '/'; 1161556Srgrimes ++baselen; 1171556Srgrimes for (rval = 0; --argc; ++argv) { 11811298Sbde /* 11911298Sbde * Find the last component of the source pathname. It 12011298Sbde * may have trailing slashes. 12111298Sbde */ 12211298Sbde p = *argv + strlen(*argv); 12311298Sbde while (p != *argv && p[-1] == '/') 12411298Sbde --p; 12511298Sbde while (p != *argv && p[-1] != '/') 12611298Sbde --p; 12711298Sbde 1281556Srgrimes if ((baselen + (len = strlen(p))) >= MAXPATHLEN) { 1291556Srgrimes warnx("%s: destination pathname too long", *argv); 1301556Srgrimes rval = 1; 1311556Srgrimes } else { 1321556Srgrimes memmove(endp, p, len + 1); 1331556Srgrimes if (do_move(*argv, path)) 1341556Srgrimes rval = 1; 1351556Srgrimes } 1361556Srgrimes } 1371556Srgrimes exit(rval); 1381556Srgrimes} 1391556Srgrimes 1401556Srgrimesint 1411556Srgrimesdo_move(from, to) 1421556Srgrimes char *from, *to; 1431556Srgrimes{ 1441556Srgrimes struct stat sb; 14529933Swosch int ask, ch, first; 1461556Srgrimes char modep[15]; 1471556Srgrimes 1481556Srgrimes /* 1491556Srgrimes * Check access. If interactive and file exists, ask user if it 1501556Srgrimes * should be replaced. Otherwise if file exists but isn't writable 1511556Srgrimes * make sure the user wants to clobber it. 1521556Srgrimes */ 1531556Srgrimes if (!fflg && !access(to, F_OK)) { 15414166Swosch 15514166Swosch /* prompt only if source exist */ 15614166Swosch if (lstat(from, &sb) == -1) { 15714305Swosch warn("%s", from); 15814305Swosch return (1); 15914166Swosch } 16014166Swosch 1611556Srgrimes ask = 0; 1621556Srgrimes if (iflg) { 1631556Srgrimes (void)fprintf(stderr, "overwrite %s? ", to); 1641556Srgrimes ask = 1; 1651556Srgrimes } else if (access(to, W_OK) && !stat(to, &sb)) { 1661556Srgrimes strmode(sb.st_mode, modep); 1671556Srgrimes (void)fprintf(stderr, "override %s%s%s/%s for %s? ", 1681556Srgrimes modep + 1, modep[9] == ' ' ? "" : " ", 1691556Srgrimes user_from_uid(sb.st_uid, 0), 1701556Srgrimes group_from_gid(sb.st_gid, 0), to); 1711556Srgrimes ask = 1; 1721556Srgrimes } 1731556Srgrimes if (ask) { 17429933Swosch first = ch = getchar(); 17529933Swosch while (ch != '\n' && ch != EOF) 17629933Swosch ch = getchar(); 17729933Swosch if (first != 'y' && first != 'Y') 1781556Srgrimes return (0); 1791556Srgrimes } 1801556Srgrimes } 1811556Srgrimes if (!rename(from, to)) 1821556Srgrimes return (0); 1831556Srgrimes 1841556Srgrimes if (errno != EXDEV) { 1851556Srgrimes warn("rename %s to %s", from, to); 1861556Srgrimes return (1); 1871556Srgrimes } 1881556Srgrimes 1891556Srgrimes /* 1901556Srgrimes * If rename fails because we're trying to cross devices, and 1911556Srgrimes * it's a regular file, do the copy internally; otherwise, use 1921556Srgrimes * cp and rm. 1931556Srgrimes */ 1941556Srgrimes if (stat(from, &sb)) { 1951556Srgrimes warn("%s", from); 1961556Srgrimes return (1); 1971556Srgrimes } 1981556Srgrimes return (S_ISREG(sb.st_mode) ? 1991556Srgrimes fastcopy(from, to, &sb) : copy(from, to)); 2001556Srgrimes} 2011556Srgrimes 2021556Srgrimesint 2031556Srgrimesfastcopy(from, to, sbp) 2041556Srgrimes char *from, *to; 2051556Srgrimes struct stat *sbp; 2061556Srgrimes{ 2071556Srgrimes struct timeval tval[2]; 2081556Srgrimes static u_int blen; 2091556Srgrimes static char *bp; 21023525Sguido mode_t oldmode; 2111556Srgrimes register int nread, from_fd, to_fd; 2121556Srgrimes 2131556Srgrimes if ((from_fd = open(from, O_RDONLY, 0)) < 0) { 2141556Srgrimes warn("%s", from); 2151556Srgrimes return (1); 2161556Srgrimes } 21723525Sguido if (blen < sbp->st_blksize) { 21823525Sguido if (bp != NULL) 21923525Sguido free(bp); 22023525Sguido if ((bp = malloc(sbp->st_blksize)) == NULL) { 22123525Sguido blen = 0; 22223525Sguido warnx("malloc failed"); 22323525Sguido return (1); 22423525Sguido } 22523525Sguido blen = sbp->st_blksize; 22623525Sguido } 22723525Sguido while ((to_fd = 22823525Sguido open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) { 22923525Sguido if (errno == EEXIST && unlink(to) == 0) 23023525Sguido continue; 2311556Srgrimes warn("%s", to); 2321556Srgrimes (void)close(from_fd); 2331556Srgrimes return (1); 2341556Srgrimes } 2351556Srgrimes while ((nread = read(from_fd, bp, blen)) > 0) 2361556Srgrimes if (write(to_fd, bp, nread) != nread) { 2371556Srgrimes warn("%s", to); 2381556Srgrimes goto err; 2391556Srgrimes } 2401556Srgrimes if (nread < 0) { 2411556Srgrimes warn("%s", from); 2421556Srgrimeserr: if (unlink(to)) 2431556Srgrimes warn("%s: remove", to); 2441556Srgrimes (void)close(from_fd); 2451556Srgrimes (void)close(to_fd); 2461556Srgrimes return (1); 2471556Srgrimes } 2481556Srgrimes (void)close(from_fd); 2491556Srgrimes 25023525Sguido oldmode = sbp->st_mode & ALLPERMS; 25123525Sguido if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) { 25223525Sguido warn("%s: set owner/group (was: %u/%u)", to, sbp->st_uid, 25323525Sguido sbp->st_gid); 25423525Sguido if (oldmode & (S_ISUID | S_ISGID)) { 25523525Sguido warnx( 25623525Sguido"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)", 25723525Sguido to, oldmode); 25823525Sguido sbp->st_mode &= ~(S_ISUID | S_ISGID); 25923525Sguido } 26023525Sguido } 2611556Srgrimes if (fchmod(to_fd, sbp->st_mode)) 26223525Sguido warn("%s: set mode (was: 0%03o)", to, oldmode); 2631556Srgrimes 2641556Srgrimes tval[0].tv_sec = sbp->st_atime; 2651556Srgrimes tval[1].tv_sec = sbp->st_mtime; 2661556Srgrimes tval[0].tv_usec = tval[1].tv_usec = 0; 2671556Srgrimes if (utimes(to, tval)) 2681556Srgrimes warn("%s: set times", to); 2691556Srgrimes 2701556Srgrimes if (close(to_fd)) { 2711556Srgrimes warn("%s", to); 2721556Srgrimes return (1); 2731556Srgrimes } 2741556Srgrimes 2751556Srgrimes if (unlink(from)) { 2761556Srgrimes warn("%s: remove", from); 2771556Srgrimes return (1); 2781556Srgrimes } 2791556Srgrimes return (0); 2801556Srgrimes} 2811556Srgrimes 2821556Srgrimesint 2831556Srgrimescopy(from, to) 2841556Srgrimes char *from, *to; 2851556Srgrimes{ 2861556Srgrimes int pid, status; 2871556Srgrimes 2881556Srgrimes if ((pid = vfork()) == 0) { 2891556Srgrimes execl(_PATH_CP, "mv", "-PRp", from, to, NULL); 2901556Srgrimes warn("%s", _PATH_CP); 2911556Srgrimes _exit(1); 2921556Srgrimes } 2931556Srgrimes if (waitpid(pid, &status, 0) == -1) { 2941556Srgrimes warn("%s: waitpid", _PATH_CP); 2951556Srgrimes return (1); 2961556Srgrimes } 2971556Srgrimes if (!WIFEXITED(status)) { 2981556Srgrimes warn("%s: did not terminate normally", _PATH_CP); 2991556Srgrimes return (1); 3001556Srgrimes } 3011556Srgrimes if (WEXITSTATUS(status)) { 3021556Srgrimes warn("%s: terminated with %d (non-zero) status", 3031556Srgrimes _PATH_CP, WEXITSTATUS(status)); 3041556Srgrimes return (1); 3051556Srgrimes } 3061556Srgrimes if (!(pid = vfork())) { 3071556Srgrimes execl(_PATH_RM, "mv", "-rf", from, NULL); 3081556Srgrimes warn("%s", _PATH_RM); 3091556Srgrimes _exit(1); 3101556Srgrimes } 3111556Srgrimes if (waitpid(pid, &status, 0) == -1) { 3121556Srgrimes warn("%s: waitpid", _PATH_RM); 3131556Srgrimes return (1); 3141556Srgrimes } 3151556Srgrimes if (!WIFEXITED(status)) { 3161556Srgrimes warn("%s: did not terminate normally", _PATH_RM); 3171556Srgrimes return (1); 3181556Srgrimes } 3191556Srgrimes if (WEXITSTATUS(status)) { 3201556Srgrimes warn("%s: terminated with %d (non-zero) status", 3211556Srgrimes _PATH_RM, WEXITSTATUS(status)); 3221556Srgrimes return (1); 3231556Srgrimes } 3241556Srgrimes return (0); 3251556Srgrimes} 3261556Srgrimes 3271556Srgrimesvoid 3281556Srgrimesusage() 3291556Srgrimes{ 33014305Swosch (void)fprintf(stderr, "%s\n%s\n", 33114305Swosch "usage: mv [-f | -i] src target", 33214305Swosch " mv [-f | -i] src1 ... srcN directory"); 3331556Srgrimes exit(1); 3341556Srgrimes} 335