Annotation of researchv10no/cmd/groups.c, revision 1.1.1.1

1.1       root        1: /*
                      2: **     Print out current access groups.
                      3: */
                      4: 
                      5: char *         Usage                   = "[-h{eader}] [-s{ort}]";
                      6: 
                      7: #include       <sys/param.h>
                      8: #include       <grp.h>
                      9: #include       <stdio.h>
                     10: #include       <trace.h>
                     11: 
                     12: char *         format                  = " %s";
                     13: short          groups[NGROUPS+1];
                     14: char *         grnames[NGROUPS+1];
                     15: char *         name;
                     16: int            noheader;
                     17: int            nosort;
                     18: int            Traceflag;
                     19: 
                     20: #define                NULLSTR                 (char *)0
                     21: #define                SYSERROR                (-1)
                     22: 
                     23: extern char *  malloc();
                     24: extern char *  strcpy();
                     25: extern char *  strrchr();
                     26: extern int     strlen();
                     27: extern char    _sobuf[];
                     28: 
                     29: void           prgr(), syserror(), usage();
                     30: int            comp();
                     31: char *         newstr();
                     32: 
                     33: int
                     34: main(argc, argv)
                     35:        int     argc;
                     36:        char *  argv[];
                     37: {
                     38:        int     ngroups = NGROUPS;
                     39: 
                     40:        if ( (name = strrchr(*argv, '/')) != NULLSTR )
                     41:                name++;
                     42:        else
                     43:                name = *argv;
                     44: 
                     45:        while ( --argc > 0 )
                     46:        {
                     47:                if ( **++argv == '-' )
                     48:                {
                     49:                        register int    c;
                     50: 
                     51:                        while ( c = *++*argv )
                     52:                        {
                     53:                                switch ( c )
                     54:                                {
                     55:                                case 'h':       noheader = 1; format = "%s\n";
                     56:                                                continue;
                     57:                                case 's':       nosort = 1;
                     58:                                                continue;
                     59:                                case 't':       Traceflag = 1;
                     60:                                                continue;
                     61:                                default:        usage("unreconised flag '%c'", c);
                     62:                                }
                     63: 
                     64:                                while ( (c = **argv) <= '9' && c >= '0' )
                     65:                                        ++*argv;
                     66:                                --*argv;
                     67:                        }
                     68: 
                     69: break2:                        ;
                     70:                }
                     71:                else
                     72:                        usage("unrecognised argument \"%s\"", *argv);
                     73:        }
                     74: 
                     75:        setbuf(stdout, _sobuf);
                     76: 
                     77:        if ( (ngroups = getgroups(NGROUPS, groups)) == SYSERROR )
                     78:                syserror("getgroups");
                     79: 
                     80:        prgr(ngroups);
                     81: 
                     82:        return 0;
                     83: }
                     84: 
                     85: void
                     86: prgr(n)
                     87:        register int            n;
                     88: {
                     89:        register short *        gp;
                     90:        register char **        cpp;
                     91:        register int            i;
                     92:        register struct group * grp;
                     93:        register char *         name;
                     94:        char                    space[20];
                     95: 
                     96:        groups[n++] = getgid();
                     97: 
                     98:        Trace(("prgr(%d), gid %d\n", n-1, groups[n-1]));
                     99: 
                    100:        if ( !noheader )
                    101:                printf("Access group%s:", n==1?"":"s");
                    102: 
                    103:        for ( gp = groups, cpp = grnames, i = n ; i-- ; )
                    104:        {
                    105:                Trace(("Got group id %d", *gp));
                    106: 
                    107:                if ( (grp = getgrgid(*gp++)) == (struct group *)0 )
                    108:                {
                    109:                        (void)sprintf(space, "<%d>", gp[-1]);
                    110:                        name = space;
                    111:                }
                    112:                else
                    113:                        name = grp->gr_name;
                    114: 
                    115:                Trace((" name \"%s\"\n", name));
                    116: 
                    117:                if ( nosort )
                    118:                        printf(format, name);
                    119:                else
                    120:                        *cpp++ = newstr(name);
                    121:        }
                    122: 
                    123:        if ( !nosort )
                    124:        {
                    125:                qsort((char *)grnames, n, sizeof(char **), comp);
                    126: 
                    127:                for ( cpp = grnames, i = n ; i-- ; )
                    128:                        printf(format, *cpp++);
                    129:        }
                    130: 
                    131:        if ( !noheader )
                    132:                printf(".\n");
                    133: }
                    134: 
                    135: void
                    136: syserror(s)
                    137:        char *  s;
                    138: {
                    139:        perror(s);
                    140:        exit(2);
                    141: }
                    142: 
                    143: void
                    144: usage(s, a)
                    145:        char *  s;
                    146:        char *  a;
                    147: {
                    148:        fprintf(stderr, "%s: ", name);
                    149:        fprintf(stderr, s, a);
                    150:        fprintf(stderr, ".\nUsage: \"%s %s\".\n", name, Usage);
                    151:        exit(1);
                    152: }
                    153: 
                    154: char *
                    155: Malloc(size)
                    156:        int             size;
                    157: {
                    158:        register char * cp;
                    159: 
                    160:        while ( (cp = malloc((unsigned)size)) == NULLSTR )
                    161:                syserror("no memory for Malloc");
                    162: 
                    163:        return cp;
                    164: }
                    165: 
                    166: char *
                    167: newstr(s)
                    168:        char *  s;
                    169: {
                    170:        if ( s == NULLSTR )
                    171:                s = "";
                    172: 
                    173:        return strcpy(Malloc(strlen(s)+1), s);
                    174: }
                    175: 
                    176: int
                    177: comp(a1, a2)
                    178:        char **         a1;
                    179:        char **         a2;
                    180: {
                    181:        register char * s1      = *a1;
                    182:        register char * s2      = *a2;
                    183:        register int    c1;
                    184:        register int    c2;
                    185: 
                    186:        while ( c1 = *s1++ )
                    187:        {
                    188:                if ( (c2 = *s2++) == 0 )
                    189:                        return 1;
                    190: 
                    191:                if ( (c1 |= 040) != (c2 |= 040) )
                    192:                        return (c1&0xff) - (c2&0xff);
                    193:        }
                    194: 
                    195:        return -((*s2)&0xff);
                    196: }
                    197: 
                    198: #ifdef GETGROUP
                    199: int
                    200: getgroups(n, gp)
                    201:        int                     n;
                    202:        short *                 gp;
                    203: {
                    204:        register int            i;
                    205:        int                     count = 1;
                    206: 
                    207:        *gp++ = 0;
                    208: 
                    209:        for ( i = 1 ; i < n ; i++ )
                    210:        {
                    211:                if ( getgrgid(i) != (struct group *)0 )
                    212:                {
                    213:                        *gp++ = i;
                    214:                        count++;
                    215:                }
                    216:        }
                    217: 
                    218:        return count;
                    219: }
                    220: #endif

unix.superglobalmegacorp.com

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