gdb - Local mods (compile)
[dragonfly.git] / libexec / mknetid / parse_group.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
1de703da
MD
28 *
29 * @(#)getgrent.c 8.2 (Berkeley) 3/21/94
30 * $FreeBSD: src/libexec/mknetid/parse_group.c,v 1.6 1999/08/28 00:09:42 peter Exp $
fc6d0222 31 * $DragonFly: src/libexec/mknetid/parse_group.c,v 1.3 2006/08/03 16:40:46 swildner Exp $
984263bc
MD
32 */
33
984263bc
MD
34/*
35 * This is a slightly modified chunk of getgrent(3). All the YP support
36 * and unneeded functions have been stripped out.
37 */
38
39#include <sys/types.h>
40#include <grp.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
06a0ed23
SW
45struct group *_getgrent(void);
46int _setgrent(void);
47void _endgrent(void);
984263bc 48FILE *_gr_fp;
06a0ed23 49
984263bc
MD
50static struct group _gr_group;
51static int _gr_stayopen;
06a0ed23 52static int grscan(int, gid_t, char *), start_gr(void);
984263bc
MD
53
54#define MAXGRP 200
55static char *members[MAXGRP];
56#define MAXLINELENGTH 1024
57static char line[MAXLINELENGTH];
58
59struct group *
06a0ed23 60_getgrent(void)
984263bc
MD
61{
62 if (!_gr_fp && !start_gr()) {
63 return NULL;
64 }
65
66
67 if (!grscan(0, 0, NULL))
68 return(NULL);
69 return(&_gr_group);
70}
71
72static int
06a0ed23 73start_gr(void)
984263bc
MD
74{
75 return 1;
76}
77
06a0ed23
SW
78static int
79_setgroupent(int stayopen)
984263bc
MD
80{
81 if (!start_gr())
82 return(0);
83 _gr_stayopen = stayopen;
84 return(1);
85}
86
87int
06a0ed23 88_setgrent(void)
984263bc
MD
89{
90 return(_setgroupent(0));
91}
92
93void
06a0ed23 94_endgrent(void)
984263bc
MD
95{
96 if (_gr_fp) {
97 (void)fclose(_gr_fp);
98 _gr_fp = NULL;
99 }
100}
101
102static int
06a0ed23 103grscan(int search, gid_t gid, char *name)
984263bc 104{
06a0ed23 105 char *cp, **m;
984263bc
MD
106 char *bp;
107 for (;;) {
108 if (!fgets(line, sizeof(line), _gr_fp))
109 return(0);
110 bp = line;
111 /* skip lines that are too big */
112 if (!index(line, '\n')) {
113 int ch;
114
115 while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
116 ;
117 continue;
118 }
119 if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL)
120 break;
121 if (_gr_group.gr_name[0] == '+')
122 continue;
123
124 if (search && name) {
125 if(strcmp(_gr_group.gr_name, name)) {
126 continue;
127 }
128 }
129 if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL)
fc6d0222 130 break;
984263bc
MD
131 if (!(cp = strsep(&bp, ":\n")))
132 continue;
133 _gr_group.gr_gid = atoi(cp);
134 if (search && name == NULL && _gr_group.gr_gid != gid)
135 continue;
136 cp = NULL;
137 if (bp == NULL) /* !! Must check for this! */
138 break;
139 for (m = _gr_group.gr_mem = members;; bp++) {
140 if (m == &members[MAXGRP - 1])
141 break;
142 if (*bp == ',') {
143 if (cp) {
144 *bp = '\0';
145 *m++ = cp;
146 cp = NULL;
147 }
148 } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
149 if (cp) {
150 *bp = '\0';
151 *m++ = cp;
152 }
153 break;
154 } else if (cp == NULL)
155 cp = bp;
156 }
157 *m = NULL;
158 return(1);
159 }
160 /* NOTREACHED */
161 return (0);
162}