|
|
1.1 ! root 1: int errcode; ! 2: ! 3: #include <stdio.h> ! 4: #include <sys/types.h> ! 5: #include <sys/stat.h> ! 6: #include <sys/dir.h> ! 7: ! 8: char *sprintf(); ! 9: ! 10: main(argc, argv) ! 11: char *argv[]; ! 12: { ! 13: register char *arg; ! 14: int fflg, iflg, rflg; ! 15: ! 16: fflg = 0; ! 17: if (isatty(0) == 0) ! 18: fflg++; ! 19: iflg = 0; ! 20: rflg = 0; ! 21: if(argc>1 && argv[1][0]=='-') { ! 22: arg = *++argv; ! 23: argc--; ! 24: while(*++arg != '\0') ! 25: switch(*arg) { ! 26: case 'f': ! 27: fflg++; ! 28: break; ! 29: case 'i': ! 30: iflg++; ! 31: break; ! 32: case 'r': ! 33: rflg++; ! 34: break; ! 35: default: ! 36: printf("rm: unknown option %s\n", *argv); ! 37: exit(1); ! 38: } ! 39: } ! 40: while(--argc > 0) { ! 41: if(!strcmp(*++argv, "..")) { ! 42: fprintf(stderr, "rm: cannot remove `..'\n"); ! 43: continue; ! 44: } ! 45: rm(*argv, fflg, rflg, iflg, 0); ! 46: } ! 47: ! 48: exit(errcode); ! 49: } ! 50: ! 51: rm(arg, fflg, rflg, iflg, level) ! 52: char arg[]; ! 53: { ! 54: struct stat buf; ! 55: struct direct direct; ! 56: char name[100]; ! 57: int d; ! 58: ! 59: if(lstat(arg, &buf)) { ! 60: if (fflg==0) { ! 61: printf("rm: %s nonexistent\n", arg); ! 62: ++errcode; ! 63: } ! 64: return; ! 65: } ! 66: if ((buf.st_mode&S_IFMT) == S_IFDIR) { ! 67: if(rflg) { ! 68: if (access(arg, 02) < 0) { ! 69: if (fflg==0) ! 70: printf("%s not changed\n", arg); ! 71: errcode++; ! 72: return; ! 73: } ! 74: if(iflg && level!=0) { ! 75: printf("directory %s: ", arg); ! 76: if(!yes()) ! 77: return; ! 78: } ! 79: if((d=open(arg, 0)) < 0) { ! 80: printf("rm: %s: cannot read\n", arg); ! 81: exit(1); ! 82: } ! 83: while(read(d, (char *)&direct, sizeof(direct)) == sizeof(direct)) { ! 84: if(direct.d_ino != 0 && !dotname(direct.d_name)) { ! 85: sprintf(name, "%s/%.14s", arg, direct.d_name); ! 86: rm(name, fflg, rflg, iflg, level+1); ! 87: } ! 88: } ! 89: close(d); ! 90: } ! 91: errcode += rmdir(arg, iflg); ! 92: return; ! 93: } ! 94: ! 95: if(iflg) { ! 96: printf("%s: ", arg); ! 97: if(!yes()) ! 98: return; ! 99: } ! 100: else if(!fflg) { ! 101: if (access(arg, 02)<0) { ! 102: printf("rm: %s %o mode ", arg, buf.st_mode&0777); ! 103: if(!yes()) ! 104: return; ! 105: } ! 106: } ! 107: if(unlink(arg) && (fflg==0 || iflg)) { ! 108: printf("rm: %s not removed\n", arg); ! 109: ++errcode; ! 110: } ! 111: } ! 112: ! 113: dotname(s) ! 114: char *s; ! 115: { ! 116: if(s[0] == '.') ! 117: if(s[1] == '.') ! 118: if(s[2] == '\0') ! 119: return(1); ! 120: else ! 121: return(0); ! 122: else if(s[1] == '\0') ! 123: return(1); ! 124: return(0); ! 125: } ! 126: ! 127: rmdir(f, iflg) ! 128: char *f; ! 129: { ! 130: int status, i; ! 131: ! 132: if(dotname(f)) ! 133: return(0); ! 134: if(iflg) { ! 135: printf("%s: ", f); ! 136: if(!yes()) ! 137: return(0); ! 138: } ! 139: while((i=fork()) == -1) ! 140: sleep(3); ! 141: if(i) { ! 142: wait(&status); ! 143: return(status); ! 144: } ! 145: execl("/bin/rmdir", "rm", f, 0); ! 146: execl("/usr/bin/rmdir", "rm", f, 0); ! 147: printf("rm: can't find rmdir\n"); ! 148: exit(1); ! 149: } ! 150: ! 151: yes() ! 152: { ! 153: int i, b; ! 154: ! 155: i = b = getchar(); ! 156: while(b != '\n' && b != EOF) ! 157: b = getchar(); ! 158: return(i == 'y'); ! 159: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.