Annotation of 43BSDReno/lib/libc/gen/getusershell.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1985 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that: (1) source distributions retain this entire copyright
        !             7:  * notice and comment, and (2) distributions including binaries display
        !             8:  * the following acknowledgement:  ``This product includes software
        !             9:  * developed by the University of California, Berkeley and its contributors''
        !            10:  * in the documentation or other materials provided with the distribution
        !            11:  * and in all advertising materials mentioning features or use of this
        !            12:  * software. Neither the name of the University nor the names of its
        !            13:  * contributors may be used to endorse or promote products derived
        !            14:  * from this software without specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #if defined(LIBC_SCCS) && !defined(lint)
        !            21: static char sccsid[] = "@(#)getusershell.c     5.6 (Berkeley) 6/1/90";
        !            22: #endif /* LIBC_SCCS and not lint */
        !            23: 
        !            24: #include <sys/param.h>
        !            25: #include <sys/file.h>
        !            26: #include <sys/stat.h>
        !            27: #include <ctype.h>
        !            28: #include <stdio.h>
        !            29: 
        !            30: #define SHELLS "/etc/shells"
        !            31: 
        !            32: /*
        !            33:  * Do not add local shells here.  They should be added in /etc/shells
        !            34:  */
        !            35: static char *okshells[] =
        !            36:     { "/bin/sh", "/bin/csh", 0 };
        !            37: 
        !            38: static char **shells, *strings;
        !            39: static char **curshell = NULL;
        !            40: extern char **initshells();
        !            41: 
        !            42: /*
        !            43:  * Get a list of shells from SHELLS, if it exists.
        !            44:  */
        !            45: char *
        !            46: getusershell()
        !            47: {
        !            48:        char *ret;
        !            49: 
        !            50:        if (curshell == NULL)
        !            51:                curshell = initshells();
        !            52:        ret = *curshell;
        !            53:        if (ret != NULL)
        !            54:                curshell++;
        !            55:        return (ret);
        !            56: }
        !            57: 
        !            58: endusershell()
        !            59: {
        !            60:        
        !            61:        if (shells != NULL)
        !            62:                free((char *)shells);
        !            63:        shells = NULL;
        !            64:        if (strings != NULL)
        !            65:                free(strings);
        !            66:        strings = NULL;
        !            67:        curshell = NULL;
        !            68: }
        !            69: 
        !            70: setusershell()
        !            71: {
        !            72: 
        !            73:        curshell = initshells();
        !            74: }
        !            75: 
        !            76: static char **
        !            77: initshells()
        !            78: {
        !            79:        register char **sp, *cp;
        !            80:        register FILE *fp;
        !            81:        struct stat statb;
        !            82:        extern char *malloc(), *calloc();
        !            83: 
        !            84:        if (shells != NULL)
        !            85:                free((char *)shells);
        !            86:        shells = NULL;
        !            87:        if (strings != NULL)
        !            88:                free(strings);
        !            89:        strings = NULL;
        !            90:        if ((fp = fopen(SHELLS, "r")) == (FILE *)0)
        !            91:                return(okshells);
        !            92:        if (fstat(fileno(fp), &statb) == -1) {
        !            93:                (void)fclose(fp);
        !            94:                return(okshells);
        !            95:        }
        !            96:        if ((strings = malloc((unsigned)statb.st_size)) == NULL) {
        !            97:                (void)fclose(fp);
        !            98:                return(okshells);
        !            99:        }
        !           100:        shells = (char **)calloc((unsigned)statb.st_size / 3, sizeof (char *));
        !           101:        if (shells == NULL) {
        !           102:                (void)fclose(fp);
        !           103:                free(strings);
        !           104:                strings = NULL;
        !           105:                return(okshells);
        !           106:        }
        !           107:        sp = shells;
        !           108:        cp = strings;
        !           109:        while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
        !           110:                while (*cp != '#' && *cp != '/' && *cp != '\0')
        !           111:                        cp++;
        !           112:                if (*cp == '#' || *cp == '\0')
        !           113:                        continue;
        !           114:                *sp++ = cp;
        !           115:                while (!isspace(*cp) && *cp != '#' && *cp != '\0')
        !           116:                        cp++;
        !           117:                *cp++ = '\0';
        !           118:        }
        !           119:        *sp = (char *)0;
        !           120:        (void)fclose(fp);
        !           121:        return (shells);
        !           122: }

unix.superglobalmegacorp.com

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