mv.c (50471) | mv.c (50544) |
---|---|
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 --- 31 unchanged lines hidden (view full) --- 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 46#endif 47static const char rcsid[] = | 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 --- 31 unchanged lines hidden (view full) --- 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44#if 0 45static char sccsid[] = "@(#)mv.c 8.2 (Berkeley) 4/2/94"; 46#endif 47static const char rcsid[] = |
48 "$FreeBSD: head/bin/mv/mv.c 50471 1999-08-27 23:15:48Z peter $"; | 48 "$FreeBSD: head/bin/mv/mv.c 50544 1999-08-29 08:21:16Z mharo $"; |
49#endif /* not lint */ 50 51#include <sys/param.h> 52#include <sys/time.h> 53#include <sys/wait.h> 54#include <sys/stat.h> 55#include <sys/mount.h> 56 57#include <err.h> 58#include <errno.h> 59#include <fcntl.h> 60#include <stdio.h> 61#include <stdlib.h> 62#include <string.h> | 49#endif /* not lint */ 50 51#include <sys/param.h> 52#include <sys/time.h> 53#include <sys/wait.h> 54#include <sys/stat.h> 55#include <sys/mount.h> 56 57#include <err.h> 58#include <errno.h> 59#include <fcntl.h> 60#include <stdio.h> 61#include <stdlib.h> 62#include <string.h> |
63#include <sysexits.h> |
|
63#include <unistd.h> 64 65#include "pathnames.h" 66 | 64#include <unistd.h> 65 66#include "pathnames.h" 67 |
67int fflg, iflg; | 68int fflg, iflg, vflg; |
68 69int copy __P((char *, char *)); 70int do_move __P((char *, char *)); 71int fastcopy __P((char *, char *, struct stat *)); 72void usage __P((void)); 73 74int 75main(argc, argv) 76 int argc; 77 char *argv[]; 78{ 79 register int baselen, len, rval; 80 register char *p, *endp; 81 struct stat sb; 82 int ch; 83 char path[MAXPATHLEN]; 84 | 69 70int copy __P((char *, char *)); 71int do_move __P((char *, char *)); 72int fastcopy __P((char *, char *, struct stat *)); 73void usage __P((void)); 74 75int 76main(argc, argv) 77 int argc; 78 char *argv[]; 79{ 80 register int baselen, len, rval; 81 register char *p, *endp; 82 struct stat sb; 83 int ch; 84 char path[MAXPATHLEN]; 85 |
85 while ((ch = getopt(argc, argv, "fi")) != -1) | 86 while ((ch = getopt(argc, argv, "fiv")) != -1) |
86 switch (ch) { 87 case 'i': 88 iflg = 1; 89 fflg = 0; 90 break; 91 case 'f': 92 fflg = 1; 93 iflg = 0; 94 break; | 87 switch (ch) { 88 case 'i': 89 iflg = 1; 90 fflg = 0; 91 break; 92 case 'f': 93 fflg = 1; 94 iflg = 0; 95 break; |
96 case 'v': 97 vflg = 1; 98 break; |
|
95 default: 96 usage(); 97 } 98 argc -= optind; 99 argv += optind; 100 101 if (argc < 2) 102 usage(); --- 80 unchanged lines hidden (view full) --- 183 while (ch != '\n' && ch != EOF) 184 ch = getchar(); 185 if (first != 'y' && first != 'Y') { 186 (void)fprintf(stderr, "not overwritten\n"); 187 return (0); 188 } 189 } 190 } | 99 default: 100 usage(); 101 } 102 argc -= optind; 103 argv += optind; 104 105 if (argc < 2) 106 usage(); --- 80 unchanged lines hidden (view full) --- 187 while (ch != '\n' && ch != EOF) 188 ch = getchar(); 189 if (first != 'y' && first != 'Y') { 190 (void)fprintf(stderr, "not overwritten\n"); 191 return (0); 192 } 193 } 194 } |
191 if (!rename(from, to)) | 195 if (!rename(from, to)) { 196 if (vflg) 197 printf("%s -> %s\n", from, to); |
192 return (0); | 198 return (0); |
199 } |
|
193 194 if (errno == EXDEV) { 195 struct statfs sfs; 196 char path[MAXPATHLEN]; 197 198 /* Can't mv(1) a mount point. */ 199 if (realpath(from, path) == NULL) { 200 warnx("cannot resolve %s: %s", from, path); --- 93 unchanged lines hidden (view full) --- 294 warn("%s", to); 295 return (1); 296 } 297 298 if (unlink(from)) { 299 warn("%s: remove", from); 300 return (1); 301 } | 200 201 if (errno == EXDEV) { 202 struct statfs sfs; 203 char path[MAXPATHLEN]; 204 205 /* Can't mv(1) a mount point. */ 206 if (realpath(from, path) == NULL) { 207 warnx("cannot resolve %s: %s", from, path); --- 93 unchanged lines hidden (view full) --- 301 warn("%s", to); 302 return (1); 303 } 304 305 if (unlink(from)) { 306 warn("%s: remove", from); 307 return (1); 308 } |
309 if (vflg) 310 printf("%s -> %s\n", from, to); |
|
302 return (0); 303} 304 305int 306copy(from, to) 307 char *from, *to; 308{ 309 int pid, status; 310 311 if ((pid = fork()) == 0) { | 311 return (0); 312} 313 314int 315copy(from, to) 316 char *from, *to; 317{ 318 int pid, status; 319 320 if ((pid = fork()) == 0) { |
312 execl(_PATH_CP, "mv", "-PRp", from, to, NULL); | 321 execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", from, to, NULL); |
313 warn("%s", _PATH_CP); 314 _exit(1); 315 } 316 if (waitpid(pid, &status, 0) == -1) { 317 warn("%s: waitpid", _PATH_CP); 318 return (1); 319 } 320 if (!WIFEXITED(status)) { --- 24 unchanged lines hidden (view full) --- 345 return (1); 346 } 347 return (0); 348} 349 350void 351usage() 352{ | 322 warn("%s", _PATH_CP); 323 _exit(1); 324 } 325 if (waitpid(pid, &status, 0) == -1) { 326 warn("%s: waitpid", _PATH_CP); 327 return (1); 328 } 329 if (!WIFEXITED(status)) { --- 24 unchanged lines hidden (view full) --- 354 return (1); 355 } 356 return (0); 357} 358 359void 360usage() 361{ |
362 |
|
353 (void)fprintf(stderr, "%s\n%s\n", | 363 (void)fprintf(stderr, "%s\n%s\n", |
354 "usage: mv [-f | -i] source target", 355 " mv [-f | -i] source ... directory"); 356 exit(1); | 364 "usage: mv [-f | -i] [-v] source target", 365 " mv [-f | -i] [-v] source ... directory"); 366 exit(EX_USAGE); |
357} | 367} |