tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / network / netresolv / irs / getprotoent_r.c
blobd1a9c0dfb186323295c03b46861a22deabf5cb83
1 /* $NetBSD: getprotoent_r.c,v 1.6 2011/10/15 23:00:02 christos Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93";
36 #else
37 __RCSID("$NetBSD: getprotoent_r.c,v 1.6 2011/10/15 23:00:02 christos Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
41 #include <netdb.h>
42 #include <stdio.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #include <FindDirectory.h>
49 #include <libutil.h>
50 #include "protoent.h"
52 #ifdef __weak_alias
53 __weak_alias(endprotoent_r,_endprotoent_r)
54 __weak_alias(getprotoent_r,_getprotoent_r)
55 __weak_alias(setprotoent_r,_setprotoent_r)
56 #endif
58 void
59 setprotoent_r(int f, struct protoent_data *pd)
61 if (pd->fp == NULL) {
62 char buffer[256];
63 find_directory(B_SYSTEM_DATA_DIRECTORY, 0, false, buffer, sizeof(buffer));
64 strlcat(buffer, "/network/protocols", sizeof(buffer));
66 pd->fp = fopen(buffer, "re");
67 } else
68 rewind(pd->fp);
69 pd->stayopen |= f;
72 void
73 endprotoent_r(struct protoent_data *pd)
75 if (pd->fp) {
76 (void)fclose(pd->fp);
77 pd->fp = NULL;
79 if (pd->aliases) {
80 free(pd->aliases);
81 pd->aliases = NULL;
82 pd->maxaliases = 0;
84 if (pd->line) {
85 free(pd->line);
86 pd->line = NULL;
88 pd->stayopen = 0;
91 struct protoent *
92 getprotoent_r(struct protoent *pr, struct protoent_data *pd)
94 char *p, *cp, **q;
95 size_t i = 0;
96 int oerrno;
98 if (pd->fp == NULL)
99 setprotoent_r(0, pd);
100 if (pd->fp == NULL)
101 return NULL;
103 for (;;) {
104 if (pd->line)
105 free(pd->line);
106 pd->line = fparseln(pd->fp, NULL, NULL, NULL,
107 FPARSELN_UNESCALL);
108 if (pd->line == NULL)
109 return NULL;
110 pr->p_name = p = pd->line;
111 cp = strpbrk(p, " \t");
112 if (cp == NULL)
113 continue;
114 *cp++ = '\0';
115 while (*cp == ' ' || *cp == '\t')
116 cp++;
117 p = strpbrk(cp, " \t");
118 if (p != NULL)
119 *p++ = '\0';
120 pr->p_proto = atoi(cp);
121 if (pd->aliases == NULL) {
122 pd->maxaliases = 10;
123 pd->aliases = malloc(pd->maxaliases * sizeof(char *));
124 if (pd->aliases == NULL) {
125 oerrno = errno;
126 endprotoent_r(pd);
127 errno = oerrno;
128 return NULL;
131 q = pr->p_aliases = pd->aliases;
132 if (p != NULL) {
133 cp = p;
134 while (cp && *cp) {
135 if (*cp == ' ' || *cp == '\t') {
136 cp++;
137 continue;
139 if (i == pd->maxaliases - 2) {
140 pd->maxaliases *= 2;
141 q = realloc(q,
142 pd->maxaliases * sizeof(char *));
143 if (q == NULL) {
144 oerrno = errno;
145 endprotoent_r(pd);
146 errno = oerrno;
147 return NULL;
149 pr->p_aliases = pd->aliases = q;
151 q[i++] = cp;
153 cp = strpbrk(cp, " \t");
154 if (cp != NULL)
155 *cp++ = '\0';
158 q[i] = NULL;
159 return pr;