mv.c revision 36383
122347Spst/* 222347Spst * Copyright (c) 1989, 1993, 1994 322347Spst * The Regents of the University of California. All rights reserved. 422347Spst * 522347Spst * This code is derived from software contributed to Berkeley by 622347Spst * Ken Smith of The State University of New York at Buffalo. 722347Spst * 822347Spst * Redistribution and use in source and binary forms, with or without 922347Spst * modification, are permitted provided that the following conditions 1022347Spst * are met: 1129964Sache * 1. Redistributions of source code must retain the above copyright 1229964Sache * notice, this list of conditions and the following disclaimer. 1322347Spst * 2. Redistributions in binary form must reproduce the above copyright 1422347Spst * notice, this list of conditions and the following disclaimer in the 1522347Spst * documentation and/or other materials provided with the distribution. 1622347Spst * 3. All advertising materials mentioning features or use of this software 1722347Spst * must display the following acknowledgement: 1822347Spst * This product includes software developed by the University of 1929964Sache * California, Berkeley and its contributors. 2029964Sache * 4. Neither the name of the University nor the names of its contributors 2129964Sache * may be used to endorse or promote products derived from this software 2222347Spst * without specific prior written permission. 2322347Spst * 2422347Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2522347Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2622347Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2722347Spst * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2822347Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2922347Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3022347Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3122347Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3222347Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3322347Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3422347Spst * SUCH DAMAGE. 3522347Spst */ 3622347Spst 3722347Spst#ifndef lint 3822347Spststatic char const copyright[] = 3922347Spst"@(#) Copyright (c) 1989, 1993, 1994\n\ 4022347Spst The Regents of the University of California. All rights reserved.\n"; 4122347Spst#endif /* not lint */ 4222347Spst 4322347Spst#ifndef lint 4422347Spst#if 0 4522347Spststatic char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 4622347Spst#endif 4722347Spststatic const char rcsid[] = 4822347Spst "$Id: mv.c,v 1.18 1998/05/15 06:25:17 charnier Exp $"; 4922347Spst#endif /* not lint */ 5022347Spst 5122347Spst#include <sys/param.h> 5222347Spst#include <sys/time.h> 5322347Spst#include <sys/wait.h> 5422347Spst#include <sys/stat.h> 5522347Spst#include <sys/mount.h> 5622347Spst 5722347Spst#include <err.h> 5822347Spst#include <errno.h> 5922347Spst#include <fcntl.h> 6022347Spst#include <stdio.h> 6122347Spst#include <stdlib.h> 6222347Spst#include <string.h> 6322347Spst#include <unistd.h> 6422347Spst 6522347Spst#include "pathnames.h" 6622347Spst 6722347Spstint fflg, iflg; 6822347Spst 6922347Spstint copy __P((char *, char *)); 7022347Spstint do_move __P((char *, char *)); 7122347Spstint fastcopy __P((char *, char *, struct stat *)); 7222347Spstvoid usage __P((void)); 7322347Spst 7422347Spstint 7522347Spstmain(argc, argv) 7622347Spst int argc; 7722347Spst char *argv[]; 7822347Spst{ 7922347Spst register int baselen, len, rval; 8022347Spst register char *p, *endp; 8122347Spst struct stat sb; 8222347Spst int ch; 8322347Spst char path[MAXPATHLEN + 1]; 8422347Spst 8522347Spst while ((ch = getopt(argc, argv, "fi")) != -1) 8622347Spst switch (ch) { 8722347Spst case 'i': 8822347Spst iflg = 1; 8922347Spst fflg = 0; 9022347Spst break; 9122347Spst case 'f': 9222347Spst fflg = 1; 9322347Spst iflg = 0; 9422347Spst break; 9522347Spst default: 9622347Spst usage(); 9722347Spst } 9822347Spst argc -= optind; 9922347Spst argv += optind; 10022347Spst 10122347Spst if (argc < 2) 10222347Spst usage(); 10322347Spst 10422347Spst /* 10522347Spst * If the stat on the target fails or the target isn't a directory, 10622347Spst * try the move. More than 2 arguments is an error in this case. 10722347Spst */ 10822347Spst if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 10922347Spst if (argc > 2) 11022347Spst usage(); 11122347Spst exit(do_move(argv[0], argv[1])); 11222347Spst } 11322347Spst 11422347Spst /* It's a directory, move each file into it. */ 11522347Spst (void)strcpy(path, argv[argc - 1]); 11622347Spst baselen = strlen(path); 11722347Spst endp = &path[baselen]; 11822347Spst if (!baselen || *(endp - 1) != '/') { 11922347Spst *endp++ = '/'; 12022347Spst ++baselen; 12122347Spst } 12222347Spst for (rval = 0; --argc; ++argv) { 12322347Spst /* 12422347Spst * Find the last component of the source pathname. It 12522347Spst * may have trailing slashes. 12622347Spst */ 12722347Spst p = *argv + strlen(*argv); 12822347Spst while (p != *argv && p[-1] == '/') 12922347Spst --p; 13022347Spst while (p != *argv && p[-1] != '/') 13122347Spst --p; 13222347Spst 13322347Spst if ((baselen + (len = strlen(p))) >= MAXPATHLEN) { 13422347Spst warnx("%s: destination pathname too long", *argv); 13522347Spst rval = 1; 13622347Spst } else { 13722347Spst memmove(endp, p, len + 1); 13822347Spst if (do_move(*argv, path)) 13922347Spst rval = 1; 14022347Spst } 14122347Spst } 14222347Spst exit(rval); 14329964Sache} 14429964Sache 14529964Sacheint 14629964Sachedo_move(from, to) 14729964Sache char *from, *to; 14829964Sache{ 14929964Sache struct stat sb; 15022347Spst int ask, ch, first; 15122347Spst char modep[15]; 15222347Spst 15322347Spst /* 15422347Spst * Check access. If interactive and file exists, ask user if it 15522347Spst * should be replaced. Otherwise if file exists but isn't writable 15622347Spst * make sure the user wants to clobber it. 15722347Spst */ 15822347Spst if (!fflg && !access(to, F_OK)) { 15922347Spst 16022347Spst /* prompt only if source exist */ 16122347Spst if (lstat(from, &sb) == -1) { 16222347Spst warn("%s", from); 16322347Spst return (1); 16422347Spst } 16522347Spst 16622347Spst#define YESNO "(y/n [n]) " 16722347Spst ask = 0; 16822347Spst if (iflg) { 16929964Sache (void)fprintf(stderr, "overwrite %s? %s", to, YESNO); 17029964Sache ask = 1; 17129964Sache } else if (access(to, W_OK) && !stat(to, &sb)) { 17222347Spst strmode(sb.st_mode, modep); 17322347Spst (void)fprintf(stderr, "override %s%s%s/%s for %s? %s", 17422347Spst modep + 1, modep[9] == ' ' ? "" : " ", 17522347Spst user_from_uid(sb.st_uid, 0), 17622347Spst group_from_gid(sb.st_gid, 0), to, YESNO); 17722347Spst ask = 1; 17822347Spst } 17922347Spst if (ask) { 18022347Spst first = ch = getchar(); 18122347Spst while (ch != '\n' && ch != EOF) 18222347Spst ch = getchar(); 18322347Spst if (first != 'y' && first != 'Y') { 18422347Spst (void)fprintf(stderr, "not overwritten\n"); 18522347Spst return (0); 18622347Spst } 18722347Spst } 18822347Spst } 18922347Spst if (!rename(from, to)) 19022347Spst return (0); 19122347Spst 19222347Spst if (errno == EXDEV) { 19329964Sache struct statfs sfs; 19429964Sache char path[MAXPATHLEN]; 19529964Sache 19629964Sache /* Can't mv(1) a mount point. */ 19729964Sache if (realpath(from, path) == NULL) { 19829964Sache warnx("cannot resolve %s: %s", from, path); 19929964Sache return (1); 20029964Sache } 20129964Sache if (!statfs(path, &sfs) && !strcmp(path, sfs.f_mntonname)) { 20229964Sache warnx("cannot rename a mount point"); 20329964Sache return (1); 20429964Sache } 20522347Spst } else { 20622347Spst warn("rename %s to %s", from, to); 20722347Spst return (1); 20822347Spst } 20929964Sache 21029964Sache /* 21129964Sache * If rename fails because we're trying to cross devices, and 21222347Spst * it's a regular file, do the copy internally; otherwise, use 21322347Spst * cp and rm. 21422347Spst */ 21522347Spst if (stat(from, &sb)) { 21622347Spst warn("%s", from); 21722347Spst return (1); 21822347Spst } 21922347Spst return (S_ISREG(sb.st_mode) ? 22022347Spst fastcopy(from, to, &sb) : copy(from, to)); 22122347Spst} 22222347Spst 22322347Spstint 22422347Spstfastcopy(from, to, sbp) 22522347Spst char *from, *to; 22622347Spst struct stat *sbp; 22722347Spst{ 22822347Spst struct timeval tval[2]; 22922347Spst static u_int blen; 23022347Spst static char *bp; 23122347Spst mode_t oldmode; 23229964Sache register int nread, from_fd, to_fd; 23322347Spst 23422347Spst if ((from_fd = open(from, O_RDONLY, 0)) < 0) { 23522347Spst warn("%s", from); 23622347Spst return (1); 23729964Sache } 23822347Spst if (blen < sbp->st_blksize) { 23922347Spst if (bp != NULL) 24029964Sache free(bp); 24122347Spst if ((bp = malloc(sbp->st_blksize)) == NULL) { 24222347Spst blen = 0; 24329964Sache warnx("malloc failed"); 24422347Spst return (1); 24522347Spst } 24622347Spst blen = sbp->st_blksize; 24722347Spst } 24822347Spst while ((to_fd = 24922347Spst open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) { 25029964Sache if (errno == EEXIST && unlink(to) == 0) 25129964Sache continue; 25229964Sache warn("%s", to); 25329964Sache (void)close(from_fd); 25422347Spst return (1); 25529964Sache } 25629964Sache while ((nread = read(from_fd, bp, blen)) > 0) 25729964Sache if (write(to_fd, bp, nread) != nread) { 25829964Sache warn("%s", to); 25929964Sache goto err; 26022347Spst } 26122347Spst if (nread < 0) { 26222347Spst warn("%s", from); 26322347Spsterr: if (unlink(to)) 26422347Spst warn("%s: remove", to); 26522347Spst (void)close(from_fd); 26622347Spst (void)close(to_fd); 26729964Sache return (1); 26829964Sache } 26929964Sache (void)close(from_fd); 27029964Sache 27129964Sache oldmode = sbp->st_mode & ALLPERMS; 27229964Sache if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) { 27329964Sache warn("%s: set owner/group (was: %u/%u)", to, sbp->st_uid, 27429964Sache sbp->st_gid); 27529964Sache if (oldmode & (S_ISUID | S_ISGID)) { 27629964Sache warnx( 27729964Sache"%s: owner/group changed; clearing suid/sgid (mode was 0%03o)", 27829964Sache to, oldmode); 27929964Sache sbp->st_mode &= ~(S_ISUID | S_ISGID); 28029964Sache } 28129964Sache } 28222347Spst if (fchmod(to_fd, sbp->st_mode)) 28322347Spst warn("%s: set mode (was: 0%03o)", to, oldmode); 28422347Spst 28529964Sache tval[0].tv_sec = sbp->st_atime; 28622347Spst tval[1].tv_sec = sbp->st_mtime; 28722347Spst tval[0].tv_usec = tval[1].tv_usec = 0; 28822347Spst if (utimes(to, tval)) 28922347Spst warn("%s: set times", to); 29022347Spst 29122347Spst if (close(to_fd)) { 29222347Spst warn("%s", to); 29322347Spst return (1); 29422347Spst } 295 296 if (unlink(from)) { 297 warn("%s: remove", from); 298 return (1); 299 } 300 return (0); 301} 302 303int 304copy(from, to) 305 char *from, *to; 306{ 307 int pid, status; 308 309 if ((pid = vfork()) == 0) { 310 execl(_PATH_CP, "mv", "-PRp", from, to, NULL); 311 warn("%s", _PATH_CP); 312 _exit(1); 313 } 314 if (waitpid(pid, &status, 0) == -1) { 315 warn("%s: waitpid", _PATH_CP); 316 return (1); 317 } 318 if (!WIFEXITED(status)) { 319 warn("%s: did not terminate normally", _PATH_CP); 320 return (1); 321 } 322 if (WEXITSTATUS(status)) { 323 warn("%s: terminated with %d (non-zero) status", 324 _PATH_CP, WEXITSTATUS(status)); 325 return (1); 326 } 327 if (!(pid = vfork())) { 328 execl(_PATH_RM, "mv", "-rf", from, NULL); 329 warn("%s", _PATH_RM); 330 _exit(1); 331 } 332 if (waitpid(pid, &status, 0) == -1) { 333 warn("%s: waitpid", _PATH_RM); 334 return (1); 335 } 336 if (!WIFEXITED(status)) { 337 warn("%s: did not terminate normally", _PATH_RM); 338 return (1); 339 } 340 if (WEXITSTATUS(status)) { 341 warn("%s: terminated with %d (non-zero) status", 342 _PATH_RM, WEXITSTATUS(status)); 343 return (1); 344 } 345 return (0); 346} 347 348void 349usage() 350{ 351 (void)fprintf(stderr, "%s\n%s\n", 352 "usage: mv [-f | -i] source target", 353 " mv [-f | -i] source ... directory"); 354 exit(1); 355} 356