]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/getnetbyht.c
UefiCpuPkg/MicrocodeUpdateDxe: Fix coding style issues in INF file
[mirror_edk2.git] / StdLib / BsdSocketLib / getnetbyht.c
CommitLineData
d7ce7006 1/*\r
2 * Copyright (c) 1983, 1993\r
3 * The Regents of the University of California. All rights reserved.\r
4 *\r
5 * Redistribution and use in source and binary forms, with or without\r
6 * modification, are permitted provided that the following conditions\r
7 * are met:\r
8 * 1. Redistributions of source code must retain the above copyright\r
9 * notice, this list of conditions and the following disclaimer.\r
10 * 2. Redistributions in binary form must reproduce the above copyright\r
11 * notice, this list of conditions and the following disclaimer in the\r
12 * documentation and/or other materials provided with the distribution.\r
13 * 3. All advertising materials mentioning features or use of this software\r
14 * must display the following acknowledgement:\r
15 * This product includes software developed by the University of\r
16 * California, Berkeley and its contributors.\r
17 * 4. Neither the name of the University nor the names of its contributors\r
18 * may be used to endorse or promote products derived from this software\r
19 * without specific prior written permission.\r
20 *\r
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\r
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\r
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\r
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\r
31 * SUCH DAMAGE.\r
32 */\r
33\r
34/* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro\r
35 * Dep. Matematica Universidade de Coimbra, Portugal, Europe\r
36 *\r
37 * Permission to use, copy, modify, and distribute this software for any\r
38 * purpose with or without fee is hereby granted, provided that the above\r
39 * copyright notice and this permission notice appear in all copies.\r
40 *\r
41 * from getnetent.c 1.1 (Coimbra) 93/06/02\r
42 */\r
43\r
44#if defined(LIBC_SCCS) && !defined(lint)\r
45static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93";\r
46static char orig_rcsid[] = "From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp";\r
47static chat rcsid[] = "$Id: getnetbyht.c,v 1.1.1.1 2003/11/19 01:51:27 kyu3 Exp $";\r
48#endif /* LIBC_SCCS and not lint */\r
49\r
50#include <sys/types.h>\r
51#include <sys/socket.h>\r
52#include <netinet/in.h>\r
53#include <arpa/inet.h>\r
54#include <arpa/nameser.h>\r
55#include <netdb.h>\r
56#include <stdio.h>\r
57#include <string.h>\r
58\r
59#define MAXALIASES 35\r
60\r
61static FILE *netf;\r
62static char line[BUFSIZ+1];\r
63static struct netent net;\r
64static char *net_aliases[MAXALIASES];\r
65static int _net_stayopen;\r
66\r
67void\r
68_setnethtent(int f)\r
69{\r
70\r
71 if (netf == NULL)\r
72 netf = fopen(_PATH_NETWORKS, "r" );\r
73 else\r
74 rewind(netf);\r
75 _net_stayopen |= f;\r
76}\r
77\r
78void\r
79_endnethtent()\r
80{\r
81\r
82 if (netf) {\r
83 fclose(netf);\r
84 netf = NULL;\r
85 }\r
86 _net_stayopen = 0;\r
87}\r
88\r
89struct netent *\r
90getnetent()\r
91{\r
92 char *p;\r
93 register char *cp, **q;\r
94\r
95 if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL)\r
96 return (NULL);\r
97again:\r
98 p = fgets(line, sizeof line, netf);\r
99 if (p == NULL)\r
100 return (NULL);\r
101 if (*p == '#')\r
102 goto again;\r
103 cp = strpbrk(p, "#\n");\r
104 if (cp == NULL)\r
105 goto again;\r
106 *cp = '\0';\r
107 net.n_name = p;\r
108 cp = strpbrk(p, " \t");\r
109 if (cp == NULL)\r
110 goto again;\r
111 *cp++ = '\0';\r
112 while (*cp == ' ' || *cp == '\t')\r
113 cp++;\r
114 p = strpbrk(cp, " \t");\r
115 if (p != NULL)\r
116 *p++ = '\0';\r
117 net.n_net = inet_network(cp);\r
118 net.n_addrtype = AF_INET;\r
119 q = net.n_aliases = net_aliases;\r
120 if (p != NULL) \r
121 cp = p;\r
122 while (cp && *cp) {\r
123 if (*cp == ' ' || *cp == '\t') {\r
124 cp++;\r
125 continue;\r
126 }\r
127 if (q < &net_aliases[MAXALIASES - 1])\r
128 *q++ = cp;\r
129 cp = strpbrk(cp, " \t");\r
130 if (cp != NULL)\r
131 *cp++ = '\0';\r
132 }\r
133 *q = NULL;\r
134 return (&net);\r
135}\r
136\r
137struct netent *\r
138_getnetbyhtname(register const char *name)\r
139{\r
140 register struct netent *p;\r
141 register char **cp;\r
142\r
143 setnetent(_net_stayopen);\r
144 while ( NULL != (p = getnetent()) ) {\r
145 if (strcasecmp(p->n_name, name) == 0)\r
146 break;\r
147 for (cp = p->n_aliases; *cp != 0; cp++)\r
148 if (strcasecmp(*cp, name) == 0)\r
149 goto found;\r
150 }\r
151found:\r
152 if (!_net_stayopen)\r
153 endnetent();\r
154 return (p);\r
155}\r
156\r
157struct netent *\r
158_getnetbyhtaddr(register unsigned long net, register int type)\r
159{\r
160 register struct netent *p;\r
161\r
162 setnetent(_net_stayopen);\r
163 while ( NULL != (p = getnetent()) )\r
164 if (p->n_addrtype == type && p->n_net == net)\r
165 break;\r
166 if (!_net_stayopen)\r
167 endnetent();\r
168 return (p);\r
169}\r