Annotation of researchv10no/cmd/asd/gid.c, revision 1.1.1.1

1.1       root        1: #include "asd.h"
                      2: #include <grp.h>
                      3: 
                      4: #define CHUNK 16
                      5: 
                      6: struct group *getgrent();
                      7: 
                      8: static struct gtab {
                      9:        unsigned gid;
                     10:        char *name;
                     11: } *gtab;
                     12: 
                     13: static int size;
                     14: 
                     15: char *
                     16: strgid (gid)
                     17:        register unsigned short gid;
                     18: {
                     19:        register int i;
                     20:        static char buf[12];
                     21: 
                     22:        /* search the cache for the gid */
                     23:        for (i = 0; i < size; i++)
                     24:                if (gtab[i].gid == gid)
                     25:                        return gtab[i].name;
                     26: 
                     27:        /* search the file, caching */
                     28:        while (expand())
                     29:                if (gtab[size-1].gid == gid)
                     30:                        return gtab[size-1].name;
                     31: 
                     32:        /* failure, invent a string */
                     33:        sprintf (buf, "#%u", gid);
                     34:        return buf;
                     35: }
                     36: 
                     37: int
                     38: numgid (name)
                     39:        register char *name;
                     40: {
                     41:        register int i;
                     42: 
                     43:        /* if it starts with a #, use the number */
                     44:        if (name[0] == '#')
                     45:                return atoi (name + 1);
                     46: 
                     47:        /* search the cache */
                     48:        for (i = 0; i < size; i++)
                     49:                if (strcmp (gtab[i].name, name) == 0)
                     50:                        return gtab[i].gid;
                     51: 
                     52:        /* search the file */
                     53:        while (expand())
                     54:                if (strcmp (gtab[size-1].name, name) == 0)
                     55:                        return gtab[size-1].gid;
                     56: 
                     57:        /* failure, invent a value */
                     58:        return getgid();
                     59: }
                     60: 
                     61: static int
                     62: expand()
                     63: {
                     64:        register struct group *gr;
                     65:        static ended;
                     66: 
                     67:        if (!ended) {
                     68:                gr = getgrent();
                     69:                if (gr == NULL) {
                     70:                        ended = 1;
                     71:                        endgrent();
                     72:                        return 0;
                     73:                }
                     74:                if (size % CHUNK == 0) {
                     75:                        gtab = (struct gtab *) ralloc ((char *) gtab,
                     76:                            (unsigned) ((size + CHUNK) * sizeof (*gtab)));
                     77:                }
                     78:                gtab[size].gid = gr->gr_gid;
                     79:                gtab[size].name = copy (gr->gr_name);
                     80:                size++;
                     81:                return 1;
                     82:        }
                     83: 
                     84:        return 0;
                     85: }

unix.superglobalmegacorp.com

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