tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / network / netresolv / irs / getnetent.c
blob1f469abb6cb9f72f30c6e0e6aa0f2779b2fcda7e
1 /* $NetBSD: getnetent.c,v 1.21 2012/03/20 17:44:18 matt 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.
31 * Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
32 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
34 * Permission to use, copy, modify, and distribute this software for any
35 * purpose with or without fee is hereby granted, provided that the above
36 * copyright notice and this permission notice appear in all copies.
38 * from getnetent.c 1.1 (Coimbra) 93/06/02
41 #include <sys/cdefs.h>
42 #if defined(LIBC_SCCS) && !defined(lint)
43 #if 0
44 static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93";
45 static char rcsid[] = "Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp ";
46 #else
47 __RCSID("$NetBSD: getnetent.c,v 1.21 2012/03/20 17:44:18 matt Exp $");
48 #endif
49 #endif /* LIBC_SCCS and not lint */
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <netdb.h>
56 #include <stdio.h>
57 #include <string.h>
59 #ifdef __weak_alias
60 __weak_alias(endnetent,_endnetent)
61 __weak_alias(getnetent,_getnetent)
62 __weak_alias(setnetent,_setnetent)
63 #endif
65 #define MAXALIASES 35
67 static FILE *netf;
68 static char line[BUFSIZ+1];
69 static struct netent net;
70 static char *net_aliases[MAXALIASES];
71 int _net_stayopen;
73 static void __setnetent(int);
74 static void __endnetent(void);
76 void
77 setnetent(int stayopen)
80 sethostent(stayopen);
81 __setnetent(stayopen);
84 void
85 endnetent(void)
88 endhostent();
89 __endnetent();
92 static void
93 __setnetent(int f)
96 if (netf == NULL)
97 netf = fopen(_PATH_NETWORKS, "re");
98 else
99 rewind(netf);
100 _net_stayopen |= f;
103 static void
104 __endnetent(void)
107 if (netf) {
108 fclose(netf);
109 netf = NULL;
111 _net_stayopen = 0;
114 struct netent *
115 getnetent(void)
117 char *p;
118 register char *cp, **q;
120 if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "re")) == NULL)
121 return (NULL);
122 #if (defined(__sparc__) && defined(_LP64)) || \
123 defined(__alpha__) || \
124 (defined(__i386__) && defined(_LP64)) || \
125 (defined(__sh__) && defined(_LP64))
126 net.__n_pad0 = 0;
127 #endif
128 again:
129 p = fgets(line, (int)sizeof line, netf);
130 if (p == NULL)
131 return (NULL);
132 if (*p == '#')
133 goto again;
134 cp = strpbrk(p, "#\n");
135 if (cp == NULL)
136 goto again;
137 *cp = '\0';
138 net.n_name = p;
139 cp = strpbrk(p, " \t");
140 if (cp == NULL)
141 goto again;
142 *cp++ = '\0';
143 while (*cp == ' ' || *cp == '\t')
144 cp++;
145 p = strpbrk(cp, " \t");
146 if (p != NULL)
147 *p++ = '\0';
148 net.n_net = inet_network(cp);
149 net.n_addrtype = AF_INET;
150 q = net.n_aliases = net_aliases;
151 if (p != NULL) {
152 cp = p;
153 while (cp && *cp) {
154 if (*cp == ' ' || *cp == '\t') {
155 cp++;
156 continue;
158 if (q < &net_aliases[MAXALIASES - 1])
159 *q++ = cp;
160 cp = strpbrk(cp, " \t");
161 if (cp != NULL)
162 *cp++ = '\0';
165 *q = NULL;
166 return (&net);