Annotation of 43BSD/contrib/news/src/logdir.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  *     UNIX shell - logdir routine
                      3:  *
                      4:  *     Joe Steffen
                      5:  *     Bell Telephone Laboratories
                      6:  *
                      7:  *     This routine does not use the getpwent(3) library routine
                      8:  *     because the latter uses the stdio package.  The allocation of
                      9:  *     storage in this package destroys the integrity of the shell's
                     10:  *     storage allocation.
                     11:  *
                     12:  *     Modified 2/82 by DJ Molny
                     13:  *
                     14:  *     This routine now implements name cacheing, so multiple requests
                     15:  *     for the same logdir do not result in multiple open/reads of
                     16:  *     /etc/passwd.  If the previous request was successful and the name
                     17:  *     is the same as the last request, the same login directory is returned.
                     18:  */
                     19: #ifdef SCCSID
                     20: static char    *SccsId = "@(#)logdir.c 1.4     4/16/85";
                     21: #endif /* SCCSID */
                     22: 
                     23: #define        BUFSIZ  160
                     24: 
                     25: static char line[BUFSIZ+1];
                     26: 
                     27: char *
                     28: logdir(name)
                     29: char *name;
                     30: {
                     31:        int     pwf;
                     32:        static char lastname[BUFSIZ+1];
                     33:        static char lastdir[BUFSIZ+1];
                     34:        register char *p;
                     35:        register int i, j;
                     36:        char *getenv(), *field(), *strcpy();
                     37:        
                     38:        if (*lastdir && !strcmp(lastname,name))         /* djm */
                     39:                return(lastdir);
                     40: 
                     41:        strcpy(lastname, name);                 /* djm */
                     42:        strcpy(lastdir, "");                    /* djm */
                     43:        
                     44: #ifdef IHCC
                     45:        /* if the logname is exptools, see if $TOOLS is set */
                     46:        if (strcmp(name, "exptools") &&
                     47:            (p = getenv("TOOLS")) != 0 && *p != '\0') {
                     48:                strcpy(lastdir, p);
                     49:                return(lastdir);
                     50:        }
                     51: #endif
                     52: 
                     53:        /* attempt to open the password file */
                     54:        if ((pwf = open("/etc/passwd", 0)) == -1)
                     55:                return(0);
                     56:                
                     57:        /* find the matching password entry */
                     58:        do {
                     59:                /* get the next line in the password file */
                     60:                i = read(pwf, line, BUFSIZ);
                     61:                for (j = 0; j < i; j++)
                     62:                        if (line[j] == '\n')
                     63:                                break;
                     64:                /* return a null pointer if the whole file has been read */
                     65:                if (j >= i)
                     66:                        return(0);
                     67:                line[++j] = 0;                  /* terminate the line */
                     68:                lseek(pwf, (long) (j - i), 1);  /* point at the next line */
                     69:                p = field(line);                /* get the logname */
                     70:        } while (strcmp(name, line) != 0);
                     71:        close(pwf);
                     72:        
                     73:        /* skip the intervening fields */
                     74:        p = field(p);
                     75:        p = field(p);
                     76:        p = field(p);
                     77:        p = field(p);
                     78:        
                     79:        /* return the login directory */
                     80:        field(p);
                     81:        strcpy(lastdir,p);                      /* djm */
                     82:        return(p);
                     83: }
                     84: 
                     85: static char *
                     86: field(p)
                     87: register char *p;
                     88: {
                     89:        while (*p && *p != ':')
                     90:                ++p;
                     91:        if (*p) *p++ = 0;
                     92:        return(p);
                     93: }

unix.superglobalmegacorp.com

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