Annotation of researchv10no/cmd/odist/opkg/uid.c, revision 1.1.1.1

1.1       root        1: #include "asd.h"
                      2: #include <pwd.h>
                      3: #include <stdio.h>
                      4: #include <stdlib.h>
                      5: #include <string.h>
                      6: #include <unistd.h>
                      7: 
                      8: 
                      9: #define CHUNK 16
                     10: 
                     11: static struct utab {
                     12:        unsigned uid;
                     13:        char *name;
                     14: } *utab;
                     15: 
                     16: static int size, salloc;
                     17: 
                     18: char *
                     19: uidstr (uid_t uid)
                     20: {
                     21:        register int i;
                     22:        static char buf[12];
                     23:        struct passwd *p;
                     24: 
                     25:        /* search the cache for the uid */
                     26:        for (i = 0; i < size; i++)
                     27:                if (utab[i].uid == uid)
                     28:                        return utab[i].name;
                     29: 
                     30:        /* try to find it in the system's database */
                     31:        if (p = getpwuid(uid)) {
                     32:                if (size % CHUNK == 0)
                     33:                        utab = (struct utab *) ralloc((char *) utab, salloc += CHUNK);
                     34:                utab[size].uid = p->pw_uid;
                     35:                utab[size].name = copy(p->pw_name);
                     36:                ++size;
                     37:                return utab[size - 1].name;
                     38:        }
                     39: 
                     40:        /* failure, invent a string */
                     41:        sprintf (buf, "#%u", uid);
                     42:        return buf;
                     43: }
                     44: 
                     45: uid_t
                     46: uidnum (char *name)
                     47: {
                     48:        register int i;
                     49:        struct passwd *p;
                     50: 
                     51:        /* if it starts with a #, use the number */
                     52:        if (name[0] == '#')
                     53:                return atoi (name + 1);
                     54: 
                     55:        /* try to find it in the system's database */
                     56:        if (p = getpwnam(name)) {
                     57:                if (size % CHUNK == 0)
                     58:                        utab = (struct utab *) ralloc((char *) utab, salloc += CHUNK);
                     59:                utab[size].uid = p->pw_uid;
                     60:                utab[size].name = copy(p->pw_name);
                     61:                ++size;
                     62:                return utab[size - 1].uid;
                     63:        }
                     64: 
                     65:        /* search the cache */
                     66:        for (i = 0; i < size; i++)
                     67:                if (strcmp (utab[i].name, name) == 0)
                     68:                        return utab[i].uid;
                     69: 
                     70:        /* failure, invent a value */
                     71:        return getuid();
                     72: }

unix.superglobalmegacorp.com

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