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