mv.c revision 1.14
1/* $NetBSD: mv.c,v 1.14 1997/10/08 11:26:04 kleink 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 47static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 48#else 49__RCSID("$NetBSD: mv.c,v 1.14 1997/10/08 11:26:04 kleink 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 <locale.h> 62#include <stdio.h> 63#include <stdlib.h> 64#include <string.h> 65#include <unistd.h> 66#include <pwd.h> 67#include <grp.h> 68 69#include "pathnames.h" 70 71int fflg, iflg; 72int stdin_ok; 73 74int copy __P((char *, char *)); 75int do_move __P((char *, char *)); 76int fastcopy __P((char *, char *, struct stat *)); 77void usage __P((void)); 78int main __P((int, char *[])); 79 80int 81main(argc, argv) 82 int argc; 83 char *argv[]; 84{ 85 int baselen, len, rval; 86 char *p, *endp; 87 struct stat sb; 88 int ch; 89 char path[MAXPATHLEN + 1]; 90 91 setlocale(LC_ALL, ""); 92 93 while ((ch = getopt(argc, argv, "if")) != -1) 94 switch (ch) { 95 case 'i': 96 fflg = 0; 97 iflg = 1; 98 break; 99 case 'f': 100 iflg = 0; 101 fflg = 1; 102 break; 103 case '?': 104 default: 105 usage(); 106 } 107 argc -= optind; 108 argv += optind; 109 110 if (argc < 2) 111 usage(); 112 113 stdin_ok = isatty(STDIN_FILENO); 114 115 /* 116 * If the stat on the target fails or the target isn't a directory, 117 * try the move. More than 2 arguments is an error in this case. 118 */ 119 if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) { 120 if (argc > 2) 121 usage(); 122 exit(do_move(argv[0], argv[1])); 123 } 124 125 /* It's a directory, move each file into it. */ 126 (void)strcpy(path, argv[argc - 1]); 127 baselen = strlen(path); 128 endp = &path[baselen]; 129 *endp++ = '/'; 130 ++baselen; 131 for (rval = 0; --argc; ++argv) { 132 p = *argv + strlen(*argv) - 1; 133 while (*p == '/' && p != *argv) 134 *p-- = '\0'; 135 if ((p = strrchr(*argv, '/')) == NULL) 136 p = *argv; 137 else 138 ++p; 139 140 if ((baselen + (len = strlen(p))) >= MAXPATHLEN) { 141 warnx("%s: destination pathname too long", *argv); 142 rval = 1; 143 } else { 144 memmove(endp, p, len + 1); 145 if (do_move(*argv, path)) 146 rval = 1; 147 } 148 } 149 exit(rval); 150} 151 152int 153do_move(from, to) 154 char *from, *to; 155{ 156 struct stat sb; 157 char modep[15]; 158 159 /* 160 * (1) If the destination path exists, the -f option is not specified 161 * and either of the following conditions are true: 162 * 163 * (a) The perimissions of the destination path do not permit 164 * writing and the standard input is a terminal. 165 * (b) The -i option is specified. 166 * 167 * the mv utility shall write a prompt to standard error and 168 * read a line from standard input. If the response is not 169 * affirmative, mv shall do nothing more with the current 170 * source file... 171 */ 172 if (!fflg && !access(to, F_OK)) { 173 int ask = 1; 174 int ch; 175 176 if (iflg) { 177 (void)fprintf(stderr, "overwrite %s? ", to); 178 } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) { 179 strmode(sb.st_mode, modep); 180 (void)fprintf(stderr, "override %s%s%s/%s for %s? ", 181 modep + 1, modep[9] == ' ' ? "" : " ", 182 user_from_uid(sb.st_uid, 0), 183 group_from_gid(sb.st_gid, 0), to); 184 } else 185 ask = 0; 186 if (ask) { 187 if ((ch = getchar()) != EOF && ch != '\n') 188 while (getchar() != '\n'); 189 if (ch != 'y' && ch != 'Y') 190 return (0); 191 } 192 } 193 194 /* 195 * (2) If rename() succeeds, mv shall do nothing more with the 196 * current source file. If it fails for any other reason than 197 * EXDEV, mv shall write a diagnostic message to the standard 198 * error and do nothing more with the current source file. 199 * 200 * (3) If the destination path exists, and it is a file of type 201 * directory and source_file is not a file of type directory, 202 * or it is a file not of type directory, and source file is 203 * a file of type directory, mv shall write a diagnostic 204 * message to standard error, and do nothing more with the 205 * current source file... 206 */ 207 if (!rename(from, to)) 208 return (0); 209 210 if (errno != EXDEV) { 211 warn("rename %s to %s", from, to); 212 return (1); 213 } 214 215 /* 216 * (4) If the destination path exists, mv shall attempt to remove it. 217 * If this fails for any reason, mv shall write a diagnostic 218 * message to the standard error and do nothing more with the 219 * current source file... 220 */ 221 if (!stat(to, &sb)) { 222 if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) { 223 warn("can't remove %s", to); 224 return (1); 225 } 226 } 227 228 /* 229 * (5) The file hierarchy rooted in source_file shall be duplicated 230 * as a file hiearchy rooted in the destination path... 231 */ 232 if (stat(from, &sb)) { 233 warn("%s", from); 234 return (1); 235 } 236 return (S_ISREG(sb.st_mode) ? 237 fastcopy(from, to, &sb) : copy(from, to)); 238} 239 240int 241fastcopy(from, to, sbp) 242 char *from, *to; 243 struct stat *sbp; 244{ 245 struct timeval tval[2]; 246 static u_int blen; 247 static char *bp; 248 int nread, from_fd, to_fd; 249 250 if ((from_fd = open(from, O_RDONLY, 0)) < 0) { 251 warn("%s", from); 252 return (1); 253 } 254 if ((to_fd = 255 open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) { 256 warn("%s", to); 257 (void)close(from_fd); 258 return (1); 259 } 260 if (!blen && !(bp = malloc(blen = sbp->st_blksize))) { 261 warn("%s", ""); 262 return (1); 263 } 264 while ((nread = read(from_fd, bp, blen)) > 0) 265 if (write(to_fd, bp, nread) != nread) { 266 warn("%s", to); 267 goto err; 268 } 269 if (nread < 0) { 270 warn("%s", from); 271err: if (unlink(to)) 272 warn("%s: remove", to); 273 (void)close(from_fd); 274 (void)close(to_fd); 275 return (1); 276 } 277 (void)close(from_fd); 278 279 if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) 280 if (errno != EPERM) 281 warn("%s: set owner/group", to); 282 if (fchmod(to_fd, sbp->st_mode)) 283 warn("%s: set mode", to); 284 285 tval[0].tv_sec = sbp->st_atime; 286 tval[1].tv_sec = sbp->st_mtime; 287 tval[0].tv_usec = tval[1].tv_usec = 0; 288 if (utimes(to, tval)) 289 warn("%s: set times", to); 290 291 if (close(to_fd)) { 292 warn("%s", to); 293 return (1); 294 } 295 296 if (unlink(from)) { 297 warn("%s: remove", from); 298 return (1); 299 } 300 return (0); 301} 302 303int 304copy(from, to) 305 char *from, *to; 306{ 307 int pid, status; 308 309 if ((pid = vfork()) == 0) { 310 execl(_PATH_CP, "mv", "-PRp", from, to, NULL); 311 warn("%s", _PATH_CP); 312 _exit(1); 313 } 314 if (waitpid(pid, &status, 0) == -1) { 315 warn("%s: waitpid", _PATH_CP); 316 return (1); 317 } 318 if (!WIFEXITED(status)) { 319 warn("%s: did not terminate normally", _PATH_CP); 320 return (1); 321 } 322 if (WEXITSTATUS(status)) { 323 warn("%s: terminated with %d (non-zero) status", 324 _PATH_CP, WEXITSTATUS(status)); 325 return (1); 326 } 327 if (!(pid = vfork())) { 328 execl(_PATH_RM, "mv", "-rf", from, NULL); 329 warn("%s", _PATH_RM); 330 _exit(1); 331 } 332 if (waitpid(pid, &status, 0) == -1) { 333 warn("%s: waitpid", _PATH_RM); 334 return (1); 335 } 336 if (!WIFEXITED(status)) { 337 warn("%s: did not terminate normally", _PATH_RM); 338 return (1); 339 } 340 if (WEXITSTATUS(status)) { 341 warn("%s: terminated with %d (non-zero) status", 342 _PATH_RM, WEXITSTATUS(status)); 343 return (1); 344 } 345 return (0); 346} 347 348void 349usage() 350{ 351 352 (void)fprintf(stderr, "usage: mv [-fi] source target\n"); 353 (void)fprintf(stderr, " mv [-fi] source ... directory\n"); 354 exit(1); 355} 356