Annotation of researchv8dc/cmd/rmdir/rmdir.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Remove directory
        !             3:  */
        !             4: 
        !             5: #include <sys/types.h>
        !             6: #include <sys/dir.h>
        !             7: #include <sys/stat.h>
        !             8: #include <stdio.h>
        !             9: 
        !            10: int    Errors = 0;
        !            11: char   *rindex();
        !            12: char   *strcat();
        !            13: char   *strcpy();
        !            14: char   *cmdname;
        !            15: 
        !            16: main(argc,argv)
        !            17: int argc;
        !            18: char **argv;
        !            19: {
        !            20: 
        !            21:        cmdname = argv[0];
        !            22:        if(argc < 2) {
        !            23:                fprintf(stderr, "%s: arg count\n", cmdname);
        !            24:                exit(1);
        !            25:        }
        !            26:        while(--argc)
        !            27:                rmdir(*++argv);
        !            28:        exit(Errors!=0);
        !            29: }
        !            30: 
        !            31: rmdir(d)
        !            32: char *d;
        !            33: {
        !            34:        int     fd;
        !            35:        char    *np, name[500];
        !            36:        struct  stat    st, cst;
        !            37:        struct  direct  dir;
        !            38: 
        !            39:        strcpy(name, d);
        !            40:        if((np = rindex(name, '/')) == NULL)
        !            41:                np = name;
        !            42:        if(stat(name,&st) < 0) {
        !            43:                fprintf(stderr, "%s: %s non-existent\n", cmdname, name);
        !            44:                ++Errors;
        !            45:                return;
        !            46:        }
        !            47:        if (stat("", &cst) < 0) {
        !            48:                fprintf(stderr, "%s: cannot stat \", cmdname\"");
        !            49:                ++Errors;
        !            50:                exit(1);
        !            51:        }
        !            52:        if((st.st_mode & S_IFMT) != S_IFDIR) {
        !            53:                fprintf(stderr, "%s: %s not a directory\n", cmdname, name);
        !            54:                ++Errors;
        !            55:                return;
        !            56:        }
        !            57:        if(st.st_ino==cst.st_ino &&st.st_dev==cst.st_dev) {
        !            58:                fprintf(stderr, "%s: cannot remove current directory\n", cmdname);
        !            59:                ++Errors;
        !            60:                return;
        !            61:        }
        !            62:        if((fd = open(name,0)) < 0) {
        !            63:                fprintf(stderr, "%s: %s unreadable\n", cmdname, name);
        !            64:                ++Errors;
        !            65:                return;
        !            66:        }
        !            67:        while(read(fd, (char *)&dir, sizeof dir) == sizeof dir) {
        !            68:                if(dir.d_ino == 0) continue;
        !            69:                if(!strcmp(dir.d_name, ".") || !strcmp(dir.d_name, ".."))
        !            70:                        continue;
        !            71:                fprintf(stderr, "%s: %s not empty\n", cmdname, name);
        !            72:                ++Errors;
        !            73:                close(fd);
        !            74:                return;
        !            75:        }
        !            76:        close(fd);
        !            77:        if(!strcmp(np, ".") || !strcmp(np, "..")) {
        !            78:                fprintf(stderr, "%s: cannot remove . or ..\n", cmdname);
        !            79:                ++Errors;
        !            80:                return;
        !            81:        }
        !            82:        strcat(name, "/.");
        !            83:        if((access(name, 0)) < 0) {             /* name/. non-existent */
        !            84:                strcat(name, ".");
        !            85:                goto unl;
        !            86:        }
        !            87:        strcat(name, ".");
        !            88:        if((access(name, 0)) < 0)               /* name/.. non-existent */
        !            89:                goto unl2;
        !            90:        if(access(name, 02)) {
        !            91:                name[strlen(name)-3] = '\0';
        !            92:                fprintf(stderr, "%s: %s: no permission\n", cmdname, name);
        !            93:                ++Errors;
        !            94:                return;
        !            95:        }
        !            96: unl:
        !            97:        unlink(name);   /* unlink name/.. */
        !            98: unl2:
        !            99:        name[strlen(name)-1] = '\0';
        !           100:        unlink(name);   /* unlink name/.  */
        !           101:        name[strlen(name)-2] = '\0';
        !           102:        if (unlink(name) < 0) {
        !           103:                fprintf(stderr, "%s: %s not removed\n", cmdname, name);
        !           104:                ++Errors;
        !           105:        }
        !           106: }

unix.superglobalmegacorp.com

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