1 /* $NetBSD: mv.c,v 1.27 2001/09/16 21:53:55 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Ken Smith of The State University of New York at Buffalo. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 __COPYRIGHT("@(#) 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 #if 0 47 static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 48 #else 49 __RCSID("$NetBSD: mv.c,v 1.27 2001/09/16 21:53:55 wiz Exp $"); 50 #endif 51 #endif /* not lint */ 52 53 #include <sys/param.h> 54 #include <sys/time.h> 55 #include <sys/wait.h> 56 #include <sys/stat.h> 57 58 #include <err.h> 59 #include <errno.h> 60 #include <fcntl.h> 61 #include <grp.h> 62 #include <locale.h> 63 #include <pwd.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 69 #include "pathnames.h" 70 71 int fflg, iflg; 72 int stdin_ok; 73 74 int copy(char *, char *); 75 int do_move(char *, char *); 76 int fastcopy(char *, char *, struct stat *); 77 void usage(void); 78 int main(int, char *[]); 79 80 int 81 main(int argc, char *argv[]) 82 { 83 int baselen, ch, len, rval; 84 char *p, *endp; 85 struct stat sb; 86 char path[MAXPATHLEN + 1]; 87 88 setprogname(argv[0]); 89 (void)setlocale(LC_ALL, ""); 90 91 while ((ch = getopt(argc, argv, "if")) != -1) 92 switch (ch) { 93 case 'i': 94 fflg = 0; 95 iflg = 1; 96 break; 97 case 'f': 98 iflg = 0; 99 fflg = 1; 100 break; 101 case '?': 102 default: 103 usage(); 104 } 105 argc -= optind; 106 argv += optind; 107 108 if (argc < 2) 109 usage(); 110 111 stdin_ok = isatty(STDIN_FILENO); 112 113 /* 114 * If the stat on the target fails or the target isn't a directory, 115 * try the move. More than 2 arguments is an error in this case. 116 */ 117 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 118 if (argc > 2) 119 usage(); 120 exit(do_move(argv[0], argv[1])); 121 } 122 123 /* It's a directory, move each file into it. */ 124 (void)strcpy(path, argv[argc - 1]); 125 baselen = strlen(path); 126 endp = &path[baselen]; 127 *endp++ = '/'; 128 ++baselen; 129 for (rval = 0; --argc; ++argv) { 130 p = *argv + strlen(*argv) - 1; 131 while (*p == '/' && p != *argv) 132 *p-- = '\0'; 133 if ((p = strrchr(*argv, '/')) == NULL) 134 p = *argv; 135 else 136 ++p; 137 138 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) { 139 warnx("%s: destination pathname too long", *argv); 140 rval = 1; 141 } else { 142 memmove(endp, p, len + 1); 143 if (do_move(*argv, path)) 144 rval = 1; 145 } 146 } 147 exit(rval); 148 /* NOTREACHED */ 149 } 150 151 int 152 do_move(char *from, char *to) 153 { 154 struct stat sb; 155 char modep[15]; 156 157 /* 158 * (1) If the destination path exists, the -f option is not specified 159 * and either of the following conditions are true: 160 * 161 * (a) The permissions of the destination path do not permit 162 * writing and the standard input is a terminal. 163 * (b) The -i option is specified. 164 * 165 * the mv utility shall write a prompt to standard error and 166 * read a line from standard input. If the response is not 167 * affirmative, mv shall do nothing more with the current 168 * source file... 169 */ 170 if (!fflg && !access(to, F_OK)) { 171 int ask = 1; 172 int ch; 173 174 if (iflg) { 175 if (access(from, F_OK)) { 176 warn("rename %s", from); 177 return (1); 178 } 179 (void)fprintf(stderr, "overwrite %s? ", to); 180 } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) { 181 if (access(from, F_OK)) { 182 warn("rename %s", from); 183 return (1); 184 } 185 strmode(sb.st_mode, modep); 186 (void)fprintf(stderr, "override %s%s%s/%s for %s? ", 187 modep + 1, modep[9] == ' ' ? "" : " ", 188 user_from_uid(sb.st_uid, 0), 189 group_from_gid(sb.st_gid, 0), to); 190 } else 191 ask = 0; 192 if (ask) { 193 if ((ch = getchar()) != EOF && ch != '\n') 194 while (getchar() != '\n'); 195 if (ch != 'y' && ch != 'Y') 196 return (0); 197 } 198 } 199 200 /* 201 * (2) If rename() succeeds, mv shall do nothing more with the 202 * current source file. If it fails for any other reason than 203 * EXDEV, mv shall write a diagnostic message to the standard 204 * error and do nothing more with the current source file. 205 * 206 * (3) If the destination path exists, and it is a file of type 207 * directory and source_file is not a file of type directory, 208 * or it is a file not of type directory, and source file is 209 * a file of type directory, mv shall write a diagnostic 210 * message to standard error, and do nothing more with the 211 * current source file... 212 */ 213 if (!rename(from, to)) 214 return (0); 215 216 if (errno != EXDEV) { 217 warn("rename %s to %s", from, to); 218 return (1); 219 } 220 221 /* 222 * (4) If the destination path exists, mv shall attempt to remove it. 223 * If this fails for any reason, mv shall write a diagnostic 224 * message to the standard error and do nothing more with the 225 * current source file... 226 */ 227 if (!lstat(to, &sb)) { 228 if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) { 229 warn("can't remove %s", to); 230 return (1); 231 } 232 } 233 234 /* 235 * (5) The file hierarchy rooted in source_file shall be duplicated 236 * as a file hierarchy rooted in the destination path... 237 */ 238 if (lstat(from, &sb)) { 239 warn("%s", from); 240 return (1); 241 } 242 return (S_ISREG(sb.st_mode) ? 243 fastcopy(from, to, &sb) : copy(from, to)); 244 } 245 246 int 247 fastcopy(char *from, char *to, struct stat *sbp) 248 { 249 struct timeval tval[2]; 250 static u_int blen; 251 static char *bp; 252 int nread, from_fd, to_fd; 253 254 if ((from_fd = open(from, O_RDONLY, 0)) < 0) { 255 warn("%s", from); 256 return (1); 257 } 258 if ((to_fd = 259 open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) { 260 warn("%s", to); 261 (void)close(from_fd); 262 return (1); 263 } 264 if (!blen && !(bp = malloc(blen = sbp->st_blksize))) { 265 warn(NULL); 266 return (1); 267 } 268 while ((nread = read(from_fd, bp, blen)) > 0) 269 if (write(to_fd, bp, nread) != nread) { 270 warn("%s", to); 271 goto err; 272 } 273 if (nread < 0) { 274 warn("%s", from); 275 err: if (unlink(to)) 276 warn("%s: remove", to); 277 (void)close(from_fd); 278 (void)close(to_fd); 279 return (1); 280 } 281 (void)close(from_fd); 282 #ifdef BSD4_4 283 TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec); 284 TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec); 285 #else 286 tval[0].tv_sec = sbp->st_atime; 287 tval[1].tv_sec = sbp->st_mtime; 288 tval[0].tv_usec = 0; 289 tval[1].tv_usec = 0; 290 #endif 291 #ifdef __SVR4 292 if (utimes(to, tval)) 293 #else 294 if (futimes(to_fd, tval)) 295 #endif 296 warn("%s: set times", to); 297 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) { 298 if (errno != EPERM) 299 warn("%s: set owner/group", to); 300 sbp->st_mode &= ~(S_ISUID | S_ISGID); 301 } 302 if (fchmod(to_fd, sbp->st_mode)) 303 warn("%s: set mode", to); 304 if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP)) 305 warn("%s: set flags (was: 0%07o)", to, sbp->st_flags); 306 307 if (close(to_fd)) { 308 warn("%s", to); 309 return (1); 310 } 311 312 if (unlink(from)) { 313 warn("%s: remove", from); 314 return (1); 315 } 316 return (0); 317 } 318 319 int 320 copy(char *from, char *to) 321 { 322 int pid, status; 323 324 if ((pid = vfork()) == 0) { 325 execl(_PATH_CP, "mv", "-PRp", from, to, NULL); 326 warn("%s", _PATH_CP); 327 _exit(1); 328 } 329 if (waitpid(pid, &status, 0) == -1) { 330 warn("%s: waitpid", _PATH_CP); 331 return (1); 332 } 333 if (!WIFEXITED(status)) { 334 warn("%s: did not terminate normally", _PATH_CP); 335 return (1); 336 } 337 if (WEXITSTATUS(status)) { 338 warn("%s: terminated with %d (non-zero) status", 339 _PATH_CP, WEXITSTATUS(status)); 340 return (1); 341 } 342 if (!(pid = vfork())) { 343 execl(_PATH_RM, "mv", "-rf", from, NULL); 344 warn("%s", _PATH_RM); 345 _exit(1); 346 } 347 if (waitpid(pid, &status, 0) == -1) { 348 warn("%s: waitpid", _PATH_RM); 349 return (1); 350 } 351 if (!WIFEXITED(status)) { 352 warn("%s: did not terminate normally", _PATH_RM); 353 return (1); 354 } 355 if (WEXITSTATUS(status)) { 356 warn("%s: terminated with %d (non-zero) status", 357 _PATH_RM, WEXITSTATUS(status)); 358 return (1); 359 } 360 return (0); 361 } 362 363 void 364 usage(void) 365 { 366 (void)fprintf(stderr, "usage: %s [-fi] source target\n" 367 " %s [-fi] source ... directory\n", getprogname(), 368 getprogname()); 369 exit(1); 370 /* NOTREACHED */ 371 } 372