Annotation of researchv9/cmd/chown.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * chown uid[,gid] file ...
        !             3:  */
        !             4: 
        !             5: #include <stdio.h>
        !             6: #include <ctype.h>
        !             7: #include <sys/types.h>
        !             8: #include <sys/stat.h>
        !             9: #include <pwd.h>
        !            10: #include <grp.h>
        !            11: 
        !            12: int uid, gid;
        !            13: int nouid, nogid;
        !            14: 
        !            15: main (argc, argv)
        !            16:        char *argv[];
        !            17: {
        !            18:        register c;
        !            19:        int status;
        !            20:        struct stat stbuf;
        !            21:        int u, g;
        !            22:        
        !            23:        if (argc < 3) {
        !            24:                printf("usage: chown uid[,gid] file ...\n");
        !            25:                exit(4);
        !            26:        }
        !            27:        uidgid(argv[1]);
        !            28:        for(c=2; c<argc; c++) {
        !            29:                if (stat(argv[c], &stbuf) < 0) {
        !            30:                        perror(argv[c]);
        !            31:                        status = 1;
        !            32:                        continue;
        !            33:                }
        !            34:                u = nouid ? stbuf.st_uid : uid;
        !            35:                g = nogid ? stbuf.st_gid : gid;
        !            36:                if(chown(argv[c], u, g) < 0) {
        !            37:                        perror(argv[c]);
        !            38:                        status = 1;
        !            39:                }
        !            40:        }
        !            41:        exit(status);
        !            42: }
        !            43: 
        !            44: uidgid(ug)
        !            45: char *ug;
        !            46: {
        !            47:        char *g;
        !            48:        struct passwd *pwd;
        !            49:        struct group *grp;
        !            50:        char *strchr();
        !            51:        struct passwd *getpwnam();
        !            52:        struct group *getgrnam();
        !            53: 
        !            54:        if ((g = strchr(ug, ',')) != NULL)
        !            55:                *g++ = '\0';
        !            56:        if (*ug == '\0')
        !            57:                nouid++;
        !            58:        else if (isnumber(ug))
        !            59:                uid = atoi(ug);
        !            60:        else if ((pwd = getpwnam(ug)) != NULL)
        !            61:                uid = pwd->pw_uid;
        !            62:        else {
        !            63:                fprintf(stderr, "unknown user id: %s\n", ug);
        !            64:                exit(4);
        !            65:        }
        !            66:        if (g == NULL || *g == '\0')
        !            67:                nogid++;
        !            68:        else if (isnumber(g))
        !            69:                gid = atoi(g);
        !            70:        else if ((grp = getgrnam(g)) != NULL)
        !            71:                gid = grp->gr_gid;
        !            72:        else {
        !            73:                fprintf(stderr, "unknown group id: %s\n", g);
        !            74:                exit(4);
        !            75:        }
        !            76: }
        !            77: 
        !            78: isnumber(s)
        !            79: char *s;
        !            80: {
        !            81:        register c;
        !            82: 
        !            83:        while(c = *s++)
        !            84:                if(!isdigit(c))
        !            85:                        return(0);
        !            86:        return(1);
        !            87: }

unix.superglobalmegacorp.com

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