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

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1989 The Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted provided
        !             6:  * that: (1) source distributions retain this entire copyright notice and
        !             7:  * comment, and (2) distributions including binaries display the following
        !             8:  * acknowledgement:  ``This product includes software developed by the
        !             9:  * University of California, Berkeley and its contributors'' in the
        !            10:  * documentation or other materials provided with the distribution and in
        !            11:  * all advertising materials mentioning features or use of this software.
        !            12:  * Neither the name of the University nor the names of its contributors may
        !            13:  * be used to endorse or promote products derived from this software without
        !            14:  * specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
        !            16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
        !            17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #if defined(LIBC_SCCS) && !defined(lint)
        !            21: static char sccsid[] = "@(#)getgrent.c 5.7 (Berkeley) 6/24/90";
        !            22: #endif /* LIBC_SCCS and not lint */
        !            23: 
        !            24: #include <sys/types.h>
        !            25: #include <stdio.h>
        !            26: #include <grp.h>
        !            27: 
        !            28: static FILE *_gr_fp;
        !            29: static struct group _gr_group;
        !            30: static int _gr_stayopen;
        !            31: static char *_gr_file = _PATH_GROUP;
        !            32: 
        !            33: #define        MAXGRP          200
        !            34: static char *members[MAXGRP];
        !            35: #define        MAXLINELENGTH   1024
        !            36: static char line[MAXLINELENGTH];
        !            37: 
        !            38: struct group *
        !            39: getgrent()
        !            40: {
        !            41:        if (!_gr_fp && !start_gr() || !grscan(0, 0, (char *)NULL))
        !            42:                return((struct group *)NULL);
        !            43:        return(&_gr_group);
        !            44: }
        !            45: 
        !            46: struct group *
        !            47: getgrnam(name)
        !            48:        char *name;
        !            49: {
        !            50:        int rval;
        !            51: 
        !            52:        if (!start_gr())
        !            53:                return((struct group *)NULL);
        !            54:        rval = grscan(1, 0, name);
        !            55:        if (!_gr_stayopen)
        !            56:                endgrent();
        !            57:        return(rval ? &_gr_group : (struct group *)NULL);
        !            58: }
        !            59: 
        !            60: struct group *
        !            61: getgrgid(gid)
        !            62:        int gid;
        !            63: {
        !            64:        int rval;
        !            65: 
        !            66:        if (!start_gr())
        !            67:                return((struct group *)NULL);
        !            68:        rval = grscan(1, gid, (char *)NULL);
        !            69:        if (!_gr_stayopen)
        !            70:                endgrent();
        !            71:        return(rval ? &_gr_group : (struct group *)NULL);
        !            72: }
        !            73: 
        !            74: static
        !            75: start_gr()
        !            76: {
        !            77:        if (_gr_fp) {
        !            78:                rewind(_gr_fp);
        !            79:                return(1);
        !            80:        }
        !            81:        return((_gr_fp = fopen(_gr_file, "r")) ? 1 : 0);
        !            82: }
        !            83: 
        !            84: setgrent()
        !            85: {
        !            86:        return(setgroupent(0));
        !            87: }
        !            88: 
        !            89: setgroupent(stayopen)
        !            90:        int stayopen;
        !            91: {
        !            92:        if (!start_gr())
        !            93:                return(0);
        !            94:        _gr_stayopen = stayopen;
        !            95:        return(1);
        !            96: }
        !            97: 
        !            98: void
        !            99: endgrent()
        !           100: {
        !           101:        if (_gr_fp) {
        !           102:                (void)fclose(_gr_fp);
        !           103:                _gr_fp = (FILE *)NULL;
        !           104:        }
        !           105: }
        !           106: 
        !           107: void
        !           108: setgrfile(file)
        !           109:        char *file;
        !           110: {
        !           111:        _gr_file = file;
        !           112: }
        !           113: 
        !           114: static
        !           115: grscan(search, gid, name)
        !           116:        register int search, gid;
        !           117:        register char *name;
        !           118: {
        !           119:        register char *cp, **m;
        !           120:        char *bp;
        !           121:        char *fgets(), *strsep(), *index();
        !           122: 
        !           123:        for (;;) {
        !           124:                if (!fgets(line, sizeof(line), _gr_fp))
        !           125:                        return(0);
        !           126:                bp = line;
        !           127:                /* skip lines that are too big */
        !           128:                if (!index(line, '\n')) {
        !           129:                        int ch;
        !           130: 
        !           131:                        while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
        !           132:                                ;
        !           133:                        continue;
        !           134:                }
        !           135:                _gr_group.gr_name = strsep(&bp, ":\n");
        !           136:                if (search && name && strcmp(_gr_group.gr_name, name))
        !           137:                        continue;
        !           138:                _gr_group.gr_passwd = strsep(&bp, ":\n");
        !           139:                if (!(cp = strsep(&bp, ":\n")))
        !           140:                        continue;
        !           141:                _gr_group.gr_gid = atoi(cp);
        !           142:                if (search && name == NULL && _gr_group.gr_gid != gid)
        !           143:                        continue;
        !           144:                for (m = _gr_group.gr_mem = members;; ++m) {
        !           145:                        if (m == &members[MAXGRP - 1]) {
        !           146:                                *m = NULL;
        !           147:                                break;
        !           148:                        }
        !           149:                        if ((*m = strsep(&bp, ", \n")) == NULL)
        !           150:                                break;
        !           151:                }
        !           152:                return(1);
        !           153:        }
        !           154:        /* NOTREACHED */
        !           155: }

unix.superglobalmegacorp.com

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