Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / lib / libresolv2 / common / irs / lcl_pr.c
blob6fd6fe224be3180f1d4cf72eff651805641d0747
1 /*
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
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.
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.
35 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
36 * Portions Copyright (c) 1996,1999 by Internet Software Consortium.
38 * Permission to use, copy, modify, and distribute this software for any
39 * purpose with or without fee is hereby granted, provided that the above
40 * copyright notice and this permission notice appear in all copies.
42 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
43 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
45 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
48 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 #include "port_before.h"
53 #include <sys/types.h>
54 #include <netinet/in.h>
55 #include <arpa/nameser.h>
56 #include <resolv.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <string.h>
61 #include <stdio.h>
62 #include <stdlib.h>
64 #include <irs.h>
65 #include <isc/memcluster.h>
67 #include "port_after.h"
69 #include "irs_p.h"
70 #include "lcl_p.h"
72 #ifndef _PATH_PROTOCOLS
73 #define _PATH_PROTOCOLS "/etc/protocols"
74 #endif
75 #define MAXALIASES 35
77 /* Types */
79 struct pvt {
80 FILE * fp;
81 char line[BUFSIZ+1];
82 char * dbuf;
83 struct protoent proto;
84 char * proto_aliases[MAXALIASES];
87 /* Forward */
89 static void pr_close(struct irs_pr *);
90 static struct protoent * pr_next(struct irs_pr *);
91 static struct protoent * pr_byname(struct irs_pr *, const char *);
92 static struct protoent * pr_bynumber(struct irs_pr *, int);
93 static void pr_rewind(struct irs_pr *);
94 static void pr_minimize(struct irs_pr *);
96 /* Portability. */
98 #ifndef SEEK_SET
99 # define SEEK_SET 0
100 #endif
102 /* Public */
104 struct irs_pr *
105 irs_lcl_pr(struct irs_acc *this) {
106 struct irs_pr *pr;
107 struct pvt *pvt;
109 if (!(pr = memget(sizeof *pr))) {
110 errno = ENOMEM;
111 return (NULL);
113 if (!(pvt = memget(sizeof *pvt))) {
114 memput(pr, sizeof *this);
115 errno = ENOMEM;
116 return (NULL);
118 memset(pvt, 0, sizeof *pvt);
119 pr->private = pvt;
120 pr->close = pr_close;
121 pr->byname = pr_byname;
122 pr->bynumber = pr_bynumber;
123 pr->next = pr_next;
124 pr->rewind = pr_rewind;
125 pr->minimize = pr_minimize;
126 pr->res_get = NULL;
127 pr->res_set = NULL;
128 return (pr);
131 /* Methods */
133 static void
134 pr_close(struct irs_pr *this) {
135 struct pvt *pvt = (struct pvt *)this->private;
137 if (pvt->fp)
138 (void) fclose(pvt->fp);
139 free(pvt->dbuf);
140 memput(pvt, sizeof *pvt);
141 memput(this, sizeof *this);
144 static struct protoent *
145 pr_byname(struct irs_pr *this, const char *name) {
147 struct protoent *p;
148 char **cp;
150 pr_rewind(this);
151 while ((p = pr_next(this))) {
152 if (!strcmp(p->p_name, name))
153 goto found;
154 for (cp = p->p_aliases; *cp; cp++)
155 if (!strcmp(*cp, name))
156 goto found;
158 found:
159 return (p);
162 static struct protoent *
163 pr_bynumber(struct irs_pr *this, int proto) {
164 struct protoent *p;
166 pr_rewind(this);
167 while ((p = pr_next(this)))
168 if (p->p_proto == proto)
169 break;
170 return (p);
173 static void
174 pr_rewind(struct irs_pr *this) {
175 struct pvt *pvt = (struct pvt *)this->private;
177 if (pvt->fp) {
178 if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
179 return;
180 (void)fclose(pvt->fp);
182 if (!(pvt->fp = fopen(_PATH_PROTOCOLS, "r" )))
183 return;
184 if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
185 (void)fclose(pvt->fp);
186 pvt->fp = NULL;
190 static struct protoent *
191 pr_next(struct irs_pr *this) {
192 struct pvt *pvt = (struct pvt *)this->private;
193 char *p, *cp, **q;
194 char *bufp, *ndbuf, *dbuf = NULL;
195 int c, bufsiz, offset;
197 if (!pvt->fp)
198 pr_rewind(this);
199 if (!pvt->fp)
200 return (NULL);
201 if (pvt->dbuf) {
202 free(pvt->dbuf);
203 pvt->dbuf = NULL;
205 bufp = pvt->line;
206 bufsiz = BUFSIZ;
207 offset = 0;
208 again:
209 if ((p = fgets(bufp + offset, bufsiz - offset, pvt->fp)) == NULL) {
210 free(dbuf);
211 return (NULL);
213 if (!strchr(p, '\n') && !feof(pvt->fp)) {
214 #define GROWBUF 1024
215 /* allocate space for longer line */
216 if (dbuf == NULL) {
217 if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL)
218 strcpy(ndbuf, bufp);
219 } else
220 ndbuf = realloc(dbuf, bufsiz + GROWBUF);
221 if (ndbuf) {
222 dbuf = ndbuf;
223 bufp = dbuf;
224 bufsiz += GROWBUF;
225 offset = strlen(dbuf);
226 } else {
227 /* allocation failed; skip this long line */
228 while ((c = getc(pvt->fp)) != EOF)
229 if (c == '\n')
230 break;
231 if (c != EOF)
232 ungetc(c, pvt->fp);
234 goto again;
237 p -= offset;
238 offset = 0;
240 if (*p == '#')
241 goto again;
242 cp = strpbrk(p, "#\n");
243 if (cp != NULL)
244 *cp = '\0';
245 pvt->proto.p_name = p;
246 cp = strpbrk(p, " \t");
247 if (cp == NULL)
248 goto again;
249 *cp++ = '\0';
250 while (*cp == ' ' || *cp == '\t')
251 cp++;
252 p = strpbrk(cp, " \t");
253 if (p != NULL)
254 *p++ = '\0';
255 pvt->proto.p_proto = atoi(cp);
256 q = pvt->proto.p_aliases = pvt->proto_aliases;
257 if (p != NULL) {
258 cp = p;
259 while (cp && *cp) {
260 if (*cp == ' ' || *cp == '\t') {
261 cp++;
262 continue;
264 if (q < &pvt->proto_aliases[MAXALIASES - 1])
265 *q++ = cp;
266 cp = strpbrk(cp, " \t");
267 if (cp != NULL)
268 *cp++ = '\0';
271 *q = NULL;
272 pvt->dbuf = dbuf;
273 return (&pvt->proto);
276 static void
277 pr_minimize(struct irs_pr *this) {
278 struct pvt *pvt = (struct pvt *)this->private;
280 if (pvt->fp != NULL) {
281 (void)fclose(pvt->fp);
282 pvt->fp = NULL;
286 /*! \file */