getprotoent.c revision 149313
1/*
2 * Copyright (c) 1983, 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.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)getprotoent.c	8.1 (Berkeley) 6/4/93";
36#endif /* LIBC_SCCS and not lint */
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libc/net/getprotoent.c 149313 2005-08-20 07:59:13Z stefanf $");
39
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <limits.h>
43#include <netdb.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include "namespace.h"
48#include "reentrant.h"
49#include "un-namespace.h"
50#include "netdb_private.h"
51
52static struct protodata protodata;
53static thread_key_t protodata_key;
54static once_t protodata_init_once = ONCE_INITIALIZER;
55static int protodata_thr_keycreated = 0;
56
57static void
58protoent_data_clear(struct protoent_data *ped)
59{
60	if (ped->fp) {
61		fclose(ped->fp);
62		ped->fp = NULL;
63	}
64}
65
66static void
67protodata_free(void *ptr)
68{
69	struct protodata *pd = ptr;
70
71	if (pd == NULL)
72		return;
73	protoent_data_clear(&pd->data);
74	free(pd);
75}
76
77static void
78protodata_keycreate(void)
79{
80	protodata_thr_keycreated =
81	    (thr_keycreate(&protodata_key, protodata_free) == 0);
82}
83
84struct protodata *
85__protodata_init(void)
86{
87	struct protodata *pd;
88
89	if (thr_main() != 0)
90		return (&protodata);
91	if (thr_once(&protodata_init_once, protodata_keycreate) != 0 ||
92	    !protodata_thr_keycreated)
93		return (NULL);
94	if ((pd = thr_getspecific(protodata_key)) != NULL)
95		return (pd);
96	if ((pd = calloc(1, sizeof(*pd))) == NULL)
97		return (NULL);
98	if (thr_setspecific(protodata_key, pd) == 0)
99		return (pd);
100	free(pd);
101	return (NULL);
102}
103
104void
105setprotoent_r(int f, struct protoent_data *ped)
106{
107	if (ped->fp == NULL)
108		ped->fp = fopen(_PATH_PROTOCOLS, "r");
109	else
110		rewind(ped->fp);
111	ped->stayopen |= f;
112}
113
114void
115endprotoent_r(struct protoent_data *ped)
116{
117	if (ped->fp) {
118		fclose(ped->fp);
119		ped->fp = NULL;
120	}
121	ped->stayopen = 0;
122}
123
124int
125getprotoent_r(struct protoent *pe, struct protoent_data *ped)
126{
127	char *p;
128	char *cp, **q, *endp;
129	long l;
130
131	if (ped->fp == NULL && (ped->fp = fopen(_PATH_PROTOCOLS, "r")) == NULL)
132		return (-1);
133again:
134	if ((p = fgets(ped->line, sizeof ped->line, ped->fp)) == NULL)
135		return (-1);
136	if (*p == '#')
137		goto again;
138	cp = strpbrk(p, "#\n");
139	if (cp != NULL)
140		*cp = '\0';
141	pe->p_name = p;
142	cp = strpbrk(p, " \t");
143	if (cp == NULL)
144		goto again;
145	*cp++ = '\0';
146	while (*cp == ' ' || *cp == '\t')
147		cp++;
148	p = strpbrk(cp, " \t");
149	if (p != NULL)
150		*p++ = '\0';
151	l = strtol(cp, &endp, 10);
152	if (endp == cp || *endp != '\0' || l < 0 || l > USHRT_MAX)
153		goto again;
154	pe->p_proto = l;
155	q = pe->p_aliases = ped->aliases;
156	if (p != NULL) {
157		cp = p;
158		while (cp && *cp) {
159			if (*cp == ' ' || *cp == '\t') {
160				cp++;
161				continue;
162			}
163			if (q < &ped->aliases[_MAXALIASES - 1])
164				*q++ = cp;
165			cp = strpbrk(cp, " \t");
166			if (cp != NULL)
167				*cp++ = '\0';
168		}
169	}
170	*q = NULL;
171	return (0);
172}
173
174void
175setprotoent(int f)
176{
177	struct protodata *pd;
178
179	if ((pd = __protodata_init()) == NULL)
180		return;
181	setprotoent_r(f, &pd->data);
182}
183
184void
185endprotoent(void)
186{
187	struct protodata *pd;
188
189	if ((pd = __protodata_init()) == NULL)
190		return;
191	endprotoent_r(&pd->data);
192}
193
194struct protoent *
195getprotoent(void)
196{
197	struct protodata *pd;
198
199	if ((pd = __protodata_init()) == NULL)
200		return (NULL);
201	if (getprotoent_r(&pd->proto, &pd->data) != 0)
202		return (NULL);
203	return (&pd->proto);
204}
205