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