Annotation of 42BSD/bin/chgrp.c, revision 1.1.1.1

1.1       root        1: #ifndef lint
                      2: static char *sccsid = "@(#)chgrp.c     4.6 83/05/10";
                      3: #endif
                      4: 
                      5: /*
                      6:  * chgrp gid file ...
                      7:  */
                      8: 
                      9: #include <stdio.h>
                     10: #include <ctype.h>
                     11: #include <sys/types.h>
                     12: #include <sys/stat.h>
                     13: #include <grp.h>
                     14: #include <pwd.h>
                     15: 
                     16: struct group *gr, *getgrnam(), *getgrgid();
                     17: struct passwd *getpwuid(), *pwd;
                     18: struct stat stbuf;
                     19: int    gid, uid;
                     20: int    status;
                     21: int    fflag;
                     22: /* VARARGS */
                     23: int    fprintf();
                     24: 
                     25: main(argc, argv)
                     26:        int argc;
                     27:        char *argv[];
                     28: {
                     29:        register c, i;
                     30: 
                     31:        argc--, argv++;
                     32:        if (argc > 0 && strcmp(argv[0], "-f") == 0) {
                     33:                fflag++;
                     34:                argv++, argc--;
                     35:        }
                     36:        if (argc < 2) {
                     37:                printf("usage: chgrp [-f] gid file ...\n");
                     38:                exit(2);
                     39:        }
                     40:        uid = getuid();
                     41:        if (isnumber(argv[0])) {
                     42:                gid = atoi(argv[0]);
                     43:                gr = getgrgid(gid);
                     44:                if (uid && gr == NULL) {
                     45:                        printf("%s: unknown group\n", argv[0]);
                     46:                        exit(2);
                     47:                }
                     48:        } else {
                     49:                gr = getgrnam(argv[0]);
                     50:                if (gr == NULL) {
                     51:                        printf("%s: unknown group\n", argv[0]);
                     52:                        exit(2);
                     53:                }
                     54:                gid = gr->gr_gid;
                     55:        }
                     56:        pwd = getpwuid(uid);
                     57:        if (pwd == NULL) {
                     58:                fprintf(stderr, "Who are you?\n");
                     59:                exit(2);
                     60:        }
                     61:        if (uid && pwd->pw_gid != gid) {
                     62:                for (i=0; gr->gr_mem[i]; i++)
                     63:                        if (!(strcmp(pwd->pw_name, gr->gr_mem[i])))
                     64:                                goto ok;
                     65:                if (fflag)
                     66:                        exit(0);
                     67:                fprintf(stderr, "You are not a member of the %s group.\n",
                     68:                    argv[0]);
                     69:                exit(2);
                     70:        }
                     71: ok:
                     72:        for (c = 1; c < argc; c++) {
                     73:                if (stat(argv[c], &stbuf)) {
                     74:                        perror(argv[c]);
                     75:                        continue;
                     76:                }
                     77:                if (uid && uid != stbuf.st_uid) {
                     78:                        if (fflag)
                     79:                                continue;
                     80:                        fprintf(stderr, "You are not the owner of %s\n",
                     81:                            argv[c]);
                     82:                        status = 1;
                     83:                        continue;
                     84:                }
                     85:                if (chown(argv[c], stbuf.st_uid, gid) && !fflag)
                     86:                        perror(argv[c]);
                     87:        }
                     88:        exit(status);
                     89: }
                     90: 
                     91: isnumber(s)
                     92:        char *s;
                     93: {
                     94:        register int c;
                     95: 
                     96:        while (c = *s++)
                     97:                if (!isdigit(c))
                     98:                        return (0);
                     99:        return (1);
                    100: }

unix.superglobalmegacorp.com

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