Annotation of researchv8dc/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: struct passwd  *getpwnam();
                     13: struct group   *getgrnam();
                     14: 
                     15: main (argc, argv)
                     16:        char *argv[];
                     17: {
                     18:        register c;
                     19:        register char *cuid, *cgid;
                     20:        int uid, gid, status;
                     21:        struct stat stbuf;
                     22:        struct passwd *pwd;
                     23:        struct group *grp;
                     24:        
                     25:        if (argc < 3) {
                     26:                printf("usage: chown uid[,gid] file ...\n");
                     27:                exit(4);
                     28:        }
                     29: 
                     30:        cuid = cgid = argv[1];
                     31: 
                     32:        /* split the first argument into uid and gid parts */
                     33:        while (*cgid != '\0' && *cgid != ',')
                     34:                cgid++;
                     35:        if (*cgid == ',')
                     36:                *cgid++ = '\0';
                     37:        else
                     38:                cgid = NULL;
                     39: 
                     40:        if (isnumber(cuid))
                     41:                uid = atoi(cuid);
                     42:        else {
                     43:                if ((pwd = getpwnam (cuid)) == NULL) {
                     44:                        printf ("unknown user id: %s\n", cuid);
                     45:                        exit (4);
                     46:                }
                     47:                uid = pwd->pw_uid;
                     48:        }
                     49: 
                     50:        if (cgid) {
                     51:                if (isnumber (cgid))
                     52:                        gid = atoi (cgid);
                     53:                else {
                     54:                        if ((grp = getgrnam (cgid)) == NULL) {
                     55:                                printf ("unknown group id: %s\n", cgid);
                     56:                                exit (4);
                     57:                        }
                     58:                        gid = grp->gr_gid;
                     59:                }
                     60:        }
                     61: 
                     62:        for(c=2; c<argc; c++) {
                     63:                stat(argv[c], &stbuf);
                     64:                if(chown(argv[c], uid, cgid? gid: stbuf.st_gid) < 0) {
                     65:                        perror(argv[c]);
                     66:                        status = 1;
                     67:                }
                     68:        }
                     69:        exit(status);
                     70: }
                     71: 
                     72: isnumber(s)
                     73: char *s;
                     74: {
                     75:        register c;
                     76: 
                     77:        while(c = *s++)
                     78:                if(!isdigit(c))
                     79:                        return(0);
                     80:        return(1);
                     81: }

unix.superglobalmegacorp.com

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