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