Annotation of researchv9/cmd/rm.c, revision 1.1.1.1

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:                                usage();
                     37:                        }
                     38:        }
                     39:        if (argc <= 1)
                     40:                usage();
                     41:        while(--argc > 0) {
                     42:                if(!strcmp(*++argv, "..")) {
                     43:                        fprintf(stderr, "rm: cannot remove `..'\n");
                     44:                        continue;
                     45:                }
                     46:                rm(*argv, fflg, rflg, iflg, 0);
                     47:        }
                     48: 
                     49:        exit(errcode);
                     50: }
                     51: 
                     52: usage()
                     53: {
                     54:        fprintf(stderr, "usage: rm [-rfi] file ...\n");
                     55:        exit(1);
                     56: }
                     57: 
                     58: rm(arg, fflg, rflg, iflg, level)
                     59: char arg[];
                     60: {
                     61:        struct stat buf;
                     62:        struct direct direct;
                     63:        char name[100];
                     64:        int d;
                     65: 
                     66:        if(lstat(arg, &buf)) {
                     67:                if (fflg==0) {
                     68:                        fprintf(stderr, "rm: %s nonexistent\n", arg);
                     69:                        ++errcode;
                     70:                }
                     71:                return;
                     72:        }
                     73:        if ((buf.st_mode&S_IFMT) == S_IFDIR) {
                     74:                if(rflg) {
                     75:                        if (access(arg, 02) < 0) {
                     76:                                if (fflg==0)
                     77:                                        fprintf(stderr, "%s not changed\n", arg);
                     78:                                errcode++;
                     79:                                return;
                     80:                        }
                     81:                        if(iflg && level!=0) {
                     82:                                printf("directory %s: ", arg);
                     83:                                if(!yes())
                     84:                                        return;
                     85:                        }
                     86:                        if((d=open(arg, 0)) < 0) {
                     87:                                fprintf(stderr, "rm: %s: cannot read\n", arg);
                     88:                                exit(1);
                     89:                        }
                     90:                        while(read(d, (char *)&direct, sizeof(direct)) == sizeof(direct)) {
                     91:                                if(direct.d_ino != 0 && !dotname(direct.d_name)) {
                     92:                                        sprintf(name, "%s/%.14s", arg, direct.d_name);
                     93:                                        rm(name, fflg, rflg, iflg, level+1);
                     94:                                }
                     95:                        }
                     96:                        close(d);
                     97:                }
                     98:                if (iflg) {
                     99:                        printf("%s: ", arg);
                    100:                        if (!yes())
                    101:                                return;
                    102:                }
                    103:                if (rmdir(arg) < 0 && (fflg==0 || iflg)) {
                    104:                        perror(arg);
                    105:                        errcode++;
                    106:                }
                    107:                return;
                    108:        }
                    109: 
                    110:        if(iflg) {
                    111:                printf("%s: ", arg);
                    112:                if(!yes())
                    113:                        return;
                    114:        }
                    115:        else if(!fflg) {
                    116:                if (access(arg, 02)<0) {
                    117:                        printf("rm: %s %o mode ", arg, buf.st_mode&0777);
                    118:                        if(!yes())
                    119:                                return;
                    120:                }
                    121:        }
                    122:        if(unlink(arg) && (fflg==0 || iflg)) {
                    123:                perror(arg);
                    124:                ++errcode;
                    125:        }
                    126: }
                    127: 
                    128: dotname(s)
                    129: char *s;
                    130: {
                    131:        if(s[0] == '.')
                    132:                if(s[1] == '.')
                    133:                        if(s[2] == '\0')
                    134:                                return(1);
                    135:                        else
                    136:                                return(0);
                    137:                else if(s[1] == '\0')
                    138:                        return(1);
                    139:        return(0);
                    140: }
                    141: 
                    142: yes()
                    143: {
                    144:        int i, b;
                    145: 
                    146:        fflush(stdout);
                    147:        i = b = getchar();
                    148:        while(b != '\n' && b != EOF)
                    149:                b = getchar();
                    150:        return(i == 'y');
                    151: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.