Annotation of 42BSD/ucb/Mail/getname.c, revision 1.1.1.1

1.1       root        1: #ifndef lint
                      2: static char sccsid[] = "@(#)getname.c  2.2 (Berkeley) 8/11/83";
                      3: #endif
                      4: 
                      5: /*
                      6:  * Getname / getuserid for those with no
                      7:  * hashed passwd data base).
                      8:  * Do not compile this module in if you DO have hashed
                      9:  * passwd's -- this is slower.
                     10:  *
                     11:  * Also provided here is a getpw routine which can share
                     12:  * the open file.  This is used for the Version 6 getenv
                     13:  * implementation.
                     14:  */
                     15: 
                     16: #include "rcv.h"
                     17: 
                     18: static FILE *pwfile =  NULL;           /* Pw file held open */
                     19: static char *pwname =  "/etc/passwd";  /* Name of passwd file */
                     20: 
                     21: /*
                     22:  * Search the passwd file for a uid.  Return name through ref parameter
                     23:  * if found, indicating success with 0 return.  Return -1 on error.
                     24:  * If -1 is passed as the user id, close the passwd file.
                     25:  */
                     26: 
                     27: getname(uid, namebuf)
                     28:        char namebuf[];
                     29: {
                     30:        register char *cp, *cp2;
                     31:        char linebuf[BUFSIZ];
                     32: 
                     33:        if (uid == -1) {
                     34:                if (pwfile != NULL)
                     35:                        fclose(pwfile);
                     36:                pwfile = NULL;
                     37:                return(0);
                     38:        }
                     39:        if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL)
                     40:                return(-1);
                     41:        rewind(pwfile);
                     42:        while (fgets(linebuf, BUFSIZ, pwfile) != NULL)
                     43:                if (pweval(linebuf) == uid) {
                     44:                        for (cp = linebuf, cp2 = namebuf; *cp != ':';
                     45:                            *cp2++ = *cp++)
                     46:                                ;
                     47:                        *cp2 = '\0';
                     48:                        return(0);
                     49:                }
                     50:        return(-1);
                     51: }
                     52: 
                     53: /*
                     54:  * Read the users password file line into the passed line
                     55:  * buffer.
                     56:  */
                     57: 
                     58: getpw(uid, linebuf)
                     59:        char linebuf[];
                     60: {
                     61:        register char *cp, *cp2;
                     62: 
                     63:        if (uid == -1) {
                     64:                if (pwfile != NULL)
                     65:                        fclose(pwfile);
                     66:                pwfile = NULL;
                     67:                return(0);
                     68:        }
                     69:        if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL)
                     70:                return(-1);
                     71:        rewind(pwfile);
                     72:        while (fgets(linebuf, BUFSIZ, pwfile) != NULL)
                     73:                if (pweval(linebuf) == uid) {
                     74:                        if (linebuf[0] != '\0')
                     75:                                linebuf[strlen(linebuf)-1] = '\0';
                     76:                        return(0);
                     77:                }
                     78:        return(-1);
                     79: }
                     80: 
                     81: /*
                     82:  * Look for passwd line belonging to 'name'
                     83:  */
                     84: 
                     85: getpwnam(name, linebuf)
                     86:        char name[], linebuf[];
                     87: {
                     88:        register char *cp, *cp2;
                     89: 
                     90:        if (name == NOSTR) {
                     91:                if (pwfile != NULL)
                     92:                        fclose(pwfile);
                     93:                pwfile = NULL;
                     94:                return(0);
                     95:        }
                     96:        if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL) {
                     97:                perror(pwname);
                     98:                return(-1);
                     99:        }
                    100:        rewind(pwfile);
                    101:        while (fgets(linebuf, BUFSIZ, pwfile) != NULL) {
                    102:                cp = linebuf;
                    103:                cp2 = name;
                    104:                while (*cp2++ == *cp++)
                    105:                        ;
                    106:                if (*--cp == ':' && *--cp2 == 0)
                    107:                        return(0);
                    108:        }
                    109:        return(-1);
                    110: }
                    111: 
                    112: /*
                    113:  * Convert the passed name to a user id and return it.  Return -1
                    114:  * on error.  Iff the name passed is -1 (yech) close the pwfile.
                    115:  */
                    116: 
                    117: getuserid(name)
                    118:        char name[];
                    119: {
                    120:        register char *cp, *cp2;
                    121:        char linebuf[BUFSIZ];
                    122: 
                    123:        if (name == (char *) -1) {
                    124:                if (pwfile != NULL)
                    125:                        fclose(pwfile);
                    126:                pwfile = NULL;
                    127:                return(0);
                    128:        }
                    129:        if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL)
                    130:                return(-1);
                    131:        rewind(pwfile);
                    132:        while (fgets(linebuf, BUFSIZ, pwfile) != NULL) {
                    133:                for (cp = name, cp2 = linebuf; *cp++ == *cp2++;)
                    134:                        ;
                    135:                if (*--cp == '\0' && *--cp2 == ':')
                    136:                        return(pweval(linebuf));
                    137:        }
                    138:        return(-1);
                    139: }
                    140: 
                    141: /*
                    142:  * Evaluate the user id of the passed passwd line and return it.
                    143:  */
                    144: 
                    145: static
                    146: pweval(line)
                    147:        char line[];
                    148: {
                    149:        register char *cp;
                    150:        register int i;
                    151:        register int uid;
                    152: 
                    153:        for (cp = line, i = 0; i < 2; i += (*cp++ == ':'))
                    154:                ;
                    155:        uid = atoi(cp);
                    156: 
                    157: #ifdef UIDGID
                    158:        while (*cp && *cp != ':')
                    159:                cp++;
                    160:        cp++;
                    161:        uid |= atoi(cp) << 8;
                    162: #endif
                    163: 
                    164:        return(uid);
                    165: }

unix.superglobalmegacorp.com

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