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.3 1995/10/07 10:42:48 bde Exp $ 37 */ 38 39 #ifndef lint 40 static 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 46 static 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 64 int fflg, iflg; 65 66 int copy __P((char *, char *)); 67 int do_move __P((char *, char *)); 68 int fastcopy __P((char *, char *, struct stat *)); 69 void usage __P((void)); 70 71 int 72 main(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 fflg = iflg = 0; 83 84 while ((ch = getopt(argc, argv, "-if?")) != EOF) 85 switch (ch) { 86 case 'i': 87 iflg = isatty(STDIN_FILENO); 88 fflg = 0; 89 break; 90 case 'f': 91 fflg = 1; 92 iflg = 0; 93 break; 94 case '-': /* Undocumented; for compatibility. */ 95 goto endarg; 96 case '?': 97 default: 98 usage(); 99 } 100 endarg: argc -= optind; 101 argv += optind; 102 103 if (argc < 2) 104 usage(); 105 106 /* 107 * If the stat on the target fails or the target isn't a directory, 108 * try the move. More than 2 arguments is an error in this case. 109 */ 110 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 111 if (argc > 2) 112 usage(); 113 exit(do_move(argv[0], argv[1])); 114 } 115 116 /* It's a directory, move each file into it. */ 117 (void)strcpy(path, argv[argc - 1]); 118 baselen = strlen(path); 119 endp = &path[baselen]; 120 *endp++ = '/'; 121 ++baselen; 122 for (rval = 0; --argc; ++argv) { 123 /* 124 * Find the last component of the source pathname. It 125 * may have trailing slashes. 126 */ 127 p = *argv + strlen(*argv); 128 while (p != *argv && p[-1] == '/') 129 --p; 130 while (p != *argv && p[-1] != '/') 131 --p; 132 133 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) { 134 warnx("%s: destination pathname too long", *argv); 135 rval = 1; 136 } else { 137 memmove(endp, p, len + 1); 138 if (do_move(*argv, path)) 139 rval = 1; 140 } 141 } 142 exit(rval); 143 } 144 145 int 146 do_move(from, to) 147 char *from, *to; 148 { 149 struct stat sb; 150 int ask, ch; 151 char modep[15]; 152 153 /* 154 * Check access. If interactive and file exists, ask user if it 155 * should be replaced. Otherwise if file exists but isn't writable 156 * make sure the user wants to clobber it. 157 */ 158 if (!fflg && !access(to, F_OK)) { 159 160 /* prompt only if source exist */ 161 if (lstat(from, &sb) == -1) { 162 warn("%s", from); 163 return (1); 164 } 165 166 ask = 0; 167 if (iflg) { 168 (void)fprintf(stderr, "overwrite %s? ", to); 169 ask = 1; 170 } else if (access(to, W_OK) && !stat(to, &sb)) { 171 strmode(sb.st_mode, modep); 172 (void)fprintf(stderr, "override %s%s%s/%s for %s? ", 173 modep + 1, modep[9] == ' ' ? "" : " ", 174 user_from_uid(sb.st_uid, 0), 175 group_from_gid(sb.st_gid, 0), to); 176 ask = 1; 177 } 178 if (ask) { 179 if ((ch = getchar()) != EOF && ch != '\n') 180 while (getchar() != '\n'); 181 if (ch != 'y' && ch != 'Y') 182 return (0); 183 } 184 } 185 if (!rename(from, to)) 186 return (0); 187 188 if (errno != EXDEV) { 189 warn("rename %s to %s", from, to); 190 return (1); 191 } 192 193 /* 194 * If rename fails because we're trying to cross devices, and 195 * it's a regular file, do the copy internally; otherwise, use 196 * cp and rm. 197 */ 198 if (stat(from, &sb)) { 199 warn("%s", from); 200 return (1); 201 } 202 return (S_ISREG(sb.st_mode) ? 203 fastcopy(from, to, &sb) : copy(from, to)); 204 } 205 206 int 207 fastcopy(from, to, sbp) 208 char *from, *to; 209 struct stat *sbp; 210 { 211 struct timeval tval[2]; 212 static u_int blen; 213 static char *bp; 214 register int nread, from_fd, to_fd; 215 216 if ((from_fd = open(from, O_RDONLY, 0)) < 0) { 217 warn("%s", from); 218 return (1); 219 } 220 if ((to_fd = 221 open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) { 222 warn("%s", to); 223 (void)close(from_fd); 224 return (1); 225 } 226 if (!blen && !(bp = malloc(blen = sbp->st_blksize))) { 227 warn(NULL); 228 return (1); 229 } 230 while ((nread = read(from_fd, bp, blen)) > 0) 231 if (write(to_fd, bp, nread) != nread) { 232 warn("%s", to); 233 goto err; 234 } 235 if (nread < 0) { 236 warn("%s", from); 237 err: if (unlink(to)) 238 warn("%s: remove", to); 239 (void)close(from_fd); 240 (void)close(to_fd); 241 return (1); 242 } 243 (void)close(from_fd); 244 245 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) 246 warn("%s: set owner/group", to); 247 if (fchmod(to_fd, sbp->st_mode)) 248 warn("%s: set mode", to); 249 250 tval[0].tv_sec = sbp->st_atime; 251 tval[1].tv_sec = sbp->st_mtime; 252 tval[0].tv_usec = tval[1].tv_usec = 0; 253 if (utimes(to, tval)) 254 warn("%s: set times", to); 255 256 if (close(to_fd)) { 257 warn("%s", to); 258 return (1); 259 } 260 261 if (unlink(from)) { 262 warn("%s: remove", from); 263 return (1); 264 } 265 return (0); 266 } 267 268 int 269 copy(from, to) 270 char *from, *to; 271 { 272 int pid, status; 273 274 if ((pid = vfork()) == 0) { 275 execl(_PATH_CP, "mv", "-PRp", from, to, NULL); 276 warn("%s", _PATH_CP); 277 _exit(1); 278 } 279 if (waitpid(pid, &status, 0) == -1) { 280 warn("%s: waitpid", _PATH_CP); 281 return (1); 282 } 283 if (!WIFEXITED(status)) { 284 warn("%s: did not terminate normally", _PATH_CP); 285 return (1); 286 } 287 if (WEXITSTATUS(status)) { 288 warn("%s: terminated with %d (non-zero) status", 289 _PATH_CP, WEXITSTATUS(status)); 290 return (1); 291 } 292 if (!(pid = vfork())) { 293 execl(_PATH_RM, "mv", "-rf", from, NULL); 294 warn("%s", _PATH_RM); 295 _exit(1); 296 } 297 if (waitpid(pid, &status, 0) == -1) { 298 warn("%s: waitpid", _PATH_RM); 299 return (1); 300 } 301 if (!WIFEXITED(status)) { 302 warn("%s: did not terminate normally", _PATH_RM); 303 return (1); 304 } 305 if (WEXITSTATUS(status)) { 306 warn("%s: terminated with %d (non-zero) status", 307 _PATH_RM, WEXITSTATUS(status)); 308 return (1); 309 } 310 return (0); 311 } 312 313 void 314 usage() 315 { 316 (void)fprintf(stderr, 317 "usage: mv [-if] src target;\n or: mv [-i | -f] src1 ... srcN directory\n"); 318 exit(1); 319 } 320