110047Ssam #ifndef lint 2*10642Smckusick static char *sccsid = "@(#)mv.c 4.11 (Berkeley) 83/01/31"; 310047Ssam #endif 48839Smckusick 51216Sbill /* 61216Sbill * mv file1 file2 71216Sbill */ 810047Ssam #include <sys/param.h> 910047Ssam #include <sys/stat.h> 101216Sbill 111216Sbill #include <stdio.h> 1210047Ssam #include <dir.h> 1310047Ssam #include <errno.h> 141216Sbill #include <signal.h> 151216Sbill 161216Sbill #define DELIM '/' 171216Sbill #define MODEBITS 07777 181216Sbill 1910047Ssam #define ISDIR(st) (((st).st_mode&S_IFMT) == S_IFDIR) 2010047Ssam #define ISLNK(st) (((st).st_mode&S_IFMT) == S_IFLNK) 2110047Ssam #define ISREG(st) (((st).st_mode&S_IFMT) == S_IFREG) 2210047Ssam #define ISDEV(st) \ 2310047Ssam (((st).st_mode&S_IFMT) == S_IFCHR || ((st).st_mode&S_IFMT) == S_IFBLK) 2410047Ssam 251216Sbill char *sprintf(); 261216Sbill char *dname(); 271216Sbill struct stat s1, s2; 2810047Ssam int iflag = 0; /* interactive mode */ 2910047Ssam int fflag = 0; /* force overwriting */ 3010047Ssam extern unsigned errno; 311216Sbill 321216Sbill main(argc, argv) 3310047Ssam register char *argv[]; 341216Sbill { 351216Sbill register i, r; 361931Sroot register char *arg; 3710641Smckusick char *dest; 381216Sbill 391216Sbill if (argc < 2) 401216Sbill goto usage; 4110047Ssam while (argc > 1 && *argv[1] == '-') { 421216Sbill argc--; 431931Sroot arg = *++argv; 441216Sbill 451931Sroot /* 4610047Ssam * all files following a null option 4710047Ssam * are considered file names 481931Sroot */ 4910047Ssam if (*(arg+1) == '\0') 5010047Ssam break; 5110047Ssam while (*++arg != '\0') switch (*arg) { 521931Sroot 5310047Ssam case 'i': 5410047Ssam iflag++; 5510047Ssam break; 561931Sroot 5710047Ssam case 'f': 5810047Ssam fflag++; 5910047Ssam break; 601216Sbill 6110047Ssam default: 6210047Ssam goto usage; 6310047Ssam } 641216Sbill } 651216Sbill if (argc < 3) 661216Sbill goto usage; 6710641Smckusick dest = argv[argc-1]; 68*10642Smckusick if (stat(dest, &s2) >= 0 && ISDIR(s2)) { 6910114Ssam r = 0; 7010047Ssam for (i = 1; i < argc-1; i++) 7110047Ssam r |= movewithshortname(argv[i], dest); 7210047Ssam exit(r); 731216Sbill } 7410641Smckusick if (argc > 3) 7510641Smckusick goto usage; 7610641Smckusick r = move(argv[1], argv[2]); 7710047Ssam exit(r); 7810047Ssam /*NOTREACHED*/ 791216Sbill usage: 8010047Ssam fprintf(stderr, 81*10642Smckusick "usage: mv [-if] f1 f2 or mv [-if] f1 ... fn d1 (`fn' is a file or directory)\n"); 8210047Ssam return (1); 831216Sbill } 841216Sbill 8510047Ssam movewithshortname(src, dest) 8610047Ssam char *src, *dest; 8710047Ssam { 8810047Ssam register char *shortname; 8910047Ssam char target[MAXPATHLEN + 1]; 9010047Ssam 9110047Ssam shortname = dname(src); 9210047Ssam if (strlen(dest) + strlen(shortname) > MAXPATHLEN - 1) { 9310047Ssam error("%s/%s: pathname too long", dest, 9410047Ssam shortname); 9510047Ssam return (1); 9610047Ssam } 9710047Ssam sprintf(target, "%s/%s", dest, shortname); 9810047Ssam return (move(src, target)); 9910047Ssam } 10010047Ssam 1011216Sbill move(source, target) 10210047Ssam char *source, *target; 1031216Sbill { 1041216Sbill 1058839Smckusick if (lstat(source, &s1) < 0) { 10610047Ssam error("cannot access %s", source); 10710047Ssam return (1); 1081216Sbill } 10910047Ssam /* 11010047Ssam * First, try to rename source to destination. 11110047Ssam * The only reason we continue on failure is if 11210047Ssam * the move is on a nondirectory and not across 11310047Ssam * file systems. 11410047Ssam */ 11510047Ssam if (lstat(target, &s2) >= 0) { 11610047Ssam if (iflag && !fflag && query("remove %s? ", target) == 0) 11710047Ssam return (1); 11810047Ssam if (s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino) { 11910047Ssam error("%s and %s are identical", source, target); 12010047Ssam return (1); 1211216Sbill } 12210047Ssam if (access(target, 2) < 0 && !fflag && isatty(fileno(stdin))) { 12310047Ssam if (query("override protection %o for %s? ", 12410047Ssam s2.st_mode & MODEBITS, target) == 0) 12510047Ssam return (1); 1261216Sbill } 12710047Ssam if (rename(source, target) >= 0) 12810047Ssam return (0); 12910047Ssam if (errno != EXDEV) { 13010047Ssam Perror2(source, "rename"); 13110047Ssam return (1); 13210047Ssam } 13310047Ssam if (ISDIR(s1)) { 13410047Ssam error("can't mv directories across file systems"); 13510047Ssam return (1); 13610047Ssam } 13710047Ssam if (unlink(target) < 0) { 13810047Ssam error("cannot unlink %s", target); 13910047Ssam return (1); 14010047Ssam } 14110047Ssam } else { 14210047Ssam if (rename(source, target) >= 0) 14310047Ssam return (0); 14410047Ssam if (ISDIR(s1)) { 14510047Ssam Perror2(source, "rename"); 14610047Ssam return (1); 14710047Ssam } 1481216Sbill } 14910047Ssam /* 15010047Ssam * File can't be renamed, try to recreate the symbolic 15110047Ssam * link or special device, or copy the file wholesale 15210047Ssam * between file systems. 15310047Ssam */ 15410047Ssam if (ISLNK(s1)) { 1558839Smckusick register m; 15610047Ssam char symln[MAXPATHLEN]; 1578839Smckusick 1588839Smckusick if (readlink(source, symln, sizeof (symln)) < 0) { 15910047Ssam Perror(source); 1608839Smckusick return (1); 1618839Smckusick } 1628839Smckusick m = umask(~(s1.st_mode & MODEBITS)); 1638839Smckusick if (symlink(symln, target) < 0) { 16410047Ssam Perror(target); 1658839Smckusick return (1); 1668839Smckusick } 16710047Ssam (void) umask(m); 16810047Ssam goto cleanup; 16910047Ssam } 17010047Ssam if (ISDEV(s1)) { 17110166Ssam time_t tv[2]; 17210166Ssam 17310047Ssam if (mknod(target, s1.st_mode, s1.st_rdev) < 0) { 17410047Ssam Perror(target); 17510047Ssam return (1); 17610047Ssam } 17710166Ssam /* kludge prior to utimes */ 17810166Ssam tv[0] = s1.st_atime; 17910166Ssam tv[1] = s1.st_mtime; 18010166Ssam (void) utime(target, tv); 18110047Ssam goto cleanup; 18210047Ssam } 18310047Ssam if (ISREG(s1)) { 18410047Ssam int i, c, status; 18510166Ssam time_t tv[2]; 18610047Ssam 1871216Sbill i = fork(); 1881216Sbill if (i == -1) { 18910047Ssam error("try again"); 19010047Ssam return (1); 1911216Sbill } 1921216Sbill if (i == 0) { 1931216Sbill execl("/bin/cp", "cp", source, target, 0); 19410047Ssam error("cannot exec /bin/cp"); 1951216Sbill exit(1); 1961216Sbill } 1971216Sbill while ((c = wait(&status)) != i && c != -1) 1981216Sbill ; 1991216Sbill if (status != 0) 20010047Ssam return (1); 20110166Ssam /* kludge prior to utimes */ 20210166Ssam tv[0] = s1.st_atime; 20310166Ssam tv[1] = s1.st_mtime; 20410166Ssam (void) utime(target, tv); 20510047Ssam goto cleanup; 2061216Sbill } 20710047Ssam error("%s: unknown file type %o", source, s1.st_mode); 20810047Ssam return (1); 2091216Sbill 21010047Ssam cleanup: 2111216Sbill if (unlink(source) < 0) { 21210047Ssam error("cannot unlink %s", source); 21310047Ssam return (1); 2141216Sbill } 21510047Ssam return (0); 2161216Sbill } 2171216Sbill 21810047Ssam /*VARARGS*/ 21910047Ssam query(prompt, a1, a2) 22010047Ssam char *a1; 2211216Sbill { 22210047Ssam register char i, c; 2231216Sbill 22410047Ssam fprintf(stderr, prompt, a1, a2); 22510047Ssam i = c = getchar(); 22610047Ssam while (c != '\n' && c != EOF) 22710047Ssam c = getchar(); 22810047Ssam return (i == 'y'); 2291216Sbill } 2301216Sbill 2311216Sbill char * 2321216Sbill dname(name) 23310047Ssam register char *name; 2341216Sbill { 2351216Sbill register char *p; 2361216Sbill 2371216Sbill p = name; 2381216Sbill while (*p) 2391216Sbill if (*p++ == DELIM && *p) 2401216Sbill name = p; 2411216Sbill return name; 2421216Sbill } 2431216Sbill 24410047Ssam /*VARARGS*/ 24510047Ssam error(fmt, a1, a2) 24610047Ssam char *fmt; 2471216Sbill { 2481216Sbill 24910047Ssam fprintf(stderr, "mv: "); 25010047Ssam fprintf(stderr, fmt, a1, a2); 25110047Ssam fprintf(stderr, "\n"); 25210047Ssam } 2531216Sbill 25410047Ssam Perror(s) 25510047Ssam char *s; 25610047Ssam { 25710047Ssam char buf[MAXPATHLEN + 10]; 25810047Ssam 25910047Ssam sprintf(buf, "mv: %s", s); 26010047Ssam perror(buf); 2611216Sbill } 2621216Sbill 26310047Ssam Perror2(s1, s2) 26410047Ssam char *s1, *s2; 2651216Sbill { 26610047Ssam char buf[MAXPATHLEN + 20]; 26710047Ssam 26810047Ssam sprintf(buf, "mv: %s: %s", s1, s2); 26910047Ssam perror(buf); 2701216Sbill } 271