Annotation of researchv10no/cmd/chown.c, revision 1.1.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:        status = 0;
                     29:        for(c=2; c<argc; c++) {
                     30:                if (stat(argv[c], &stbuf) < 0) {
                     31:                        perror(argv[c]);
                     32:                        status = 1;
                     33:                        continue;
                     34:                }
                     35:                u = nouid ? stbuf.st_uid : uid;
                     36:                g = nogid ? stbuf.st_gid : gid;
                     37:                if(u == stbuf.st_uid && g == stbuf.st_gid)
                     38:                        ;
                     39:                else if(chown(argv[c], u, g) < 0) {
                     40:                        perror(argv[c]);
                     41:                        status = 1;
                     42:                }
                     43:        }
                     44:        exit(status);
                     45: }
                     46: 
                     47: uidgid(ug)
                     48: char *ug;
                     49: {
                     50:        char *g;
                     51:        struct passwd *pwd;
                     52:        struct group *grp;
                     53:        char *strchr();
                     54:        struct passwd *getpwnam();
                     55:        struct group *getgrnam();
                     56: 
                     57:        if ((g = strchr(ug, ',')) != NULL)
                     58:                *g++ = '\0';
                     59:        if (*ug == '\0')
                     60:                nouid++;
                     61:        else if (isnumber(ug))
                     62:                uid = atoi(ug);
                     63:        else if ((pwd = getpwnam(ug)) != NULL)
                     64:                uid = pwd->pw_uid;
                     65:        else {
                     66:                fprintf(stderr, "unknown user id: %s\n", ug);
                     67:                exit(4);
                     68:        }
                     69:        if (g == NULL || *g == '\0')
                     70:                nogid++;
                     71:        else if (isnumber(g))
                     72:                gid = atoi(g);
                     73:        else if ((grp = getgrnam(g)) != NULL)
                     74:                gid = grp->gr_gid;
                     75:        else {
                     76:                fprintf(stderr, "unknown group id: %s\n", g);
                     77:                exit(4);
                     78:        }
                     79: }
                     80: 
                     81: isnumber(s)
                     82: char *s;
                     83: {
                     84:        register c;
                     85: 
                     86:        if(*s == '-') s++;
                     87:        while(c = *s++)
                     88:                if(!isdigit(c))
                     89:                        return(0);
                     90:        return(1);
                     91: }

unix.superglobalmegacorp.com

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