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