2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 * Portions Copyright (c) 1996 by Internet Software Consortium.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
41 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
42 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
44 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
45 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
46 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
47 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 #if defined(LIBC_SCCS) && !defined(lint)
52 static const char rcsid[] = "$Id: lcl_sv.c,v 1.11 1997/12/04 04:57:58 halley Exp $";
53 #endif /* LIBC_SCCS and not lint */
57 #include "port_before.h"
59 #include <sys/types.h>
60 #include <sys/socket.h>
62 #include <netinet/in.h>
72 #include "port_after.h"
85 char * serv_aliases[MAXALIASES];
90 static void sv_close(struct irs_sv*);
91 static struct servent * sv_next(struct irs_sv *);
92 static struct servent * sv_byname(struct irs_sv *, const char *,
94 static struct servent * sv_byport(struct irs_sv *, int, const char *);
95 static void sv_rewind(struct irs_sv *);
96 static void sv_minimize(struct irs_sv *);
107 irs_lcl_sv(struct irs_acc *this) {
111 if (!(sv = (struct irs_sv *)malloc(sizeof *sv))) {
115 memset(sv, 0x5e, sizeof *sv);
116 if (!(pvt = (struct pvt *)malloc(sizeof *pvt))) {
121 memset(pvt, 0, sizeof *pvt);
123 sv->close = sv_close;
125 sv->byname = sv_byname;
126 sv->byport = sv_byport;
127 sv->rewind = sv_rewind;
128 sv->minimize = sv_minimize;
135 sv_close(struct irs_sv *this) {
136 struct pvt *pvt = (struct pvt *)this->private;
144 static struct servent *
145 sv_byname(struct irs_sv *this, const char *name, const char *proto) {
146 register struct servent *p;
150 while ((p = sv_next(this))) {
151 if (strcmp(name, p->s_name) == 0)
153 for (cp = p->s_aliases; *cp; cp++)
154 if (strcmp(name, *cp) == 0)
158 if (proto == NULL || strcmp(p->s_proto, proto) == 0)
164 static struct servent *
165 sv_byport(struct irs_sv *this, int port, const char *proto) {
166 register struct servent *p;
169 while ((p = sv_next(this))) {
170 if (p->s_port != port)
172 if (proto == NULL || strcmp(p->s_proto, proto) == 0)
179 sv_rewind(struct irs_sv *this) {
180 struct pvt *pvt = (struct pvt *)this->private;
183 if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
185 (void)fclose(pvt->fp);
187 if (!(pvt->fp = fopen(_PATH_SERVICES, "r" )))
189 if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
190 (void)fclose(pvt->fp);
195 static struct servent *
196 sv_next(struct irs_sv *this) {
197 struct pvt *pvt = (struct pvt *)this->private;
199 register char *cp, **q;
206 if ((p = fgets(pvt->line, BUFSIZ, pvt->fp)) == NULL)
210 cp = strpbrk(p, "#\n");
214 pvt->serv.s_name = p;
215 p = strpbrk(p, " \t");
219 while (*p == ' ' || *p == '\t')
221 cp = strpbrk(p, ",/");
225 pvt->serv.s_port = htons((u_short)atoi(p));
226 pvt->serv.s_proto = cp;
227 q = pvt->serv.s_aliases = pvt->serv_aliases;
228 cp = strpbrk(cp, " \t");
232 if (*cp == ' ' || *cp == '\t') {
236 if (q < &pvt->serv_aliases[MAXALIASES - 1])
238 cp = strpbrk(cp, " \t");
247 sv_minimize(struct irs_sv *this) {
248 struct pvt *pvt = (struct pvt *)this->private;
250 if (pvt->fp != NULL) {
251 (void)fclose(pvt->fp);