Fix up mix of man(7)/mdoc(7).
[netbsd-mini2440.git] / regress / lib / libc / inet / inet_network / inet_network.c
blobb881984f7300f9ce54b2b2dec4506290a48488f3
1 /* $NetBSD: inet_network.c,v 1.1 2008/01/19 04:12:21 ginsbach Exp $ */
2 /*
3 * Copyright (c) 2008, The NetBSD Foundation.
4 * All Rights Reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Brian Ginsbach.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <arpa/inet.h>
36 #include <netinet/in.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <util.h>
46 #define WS "\t\n "
48 #define ISODIGIT(c) ((int)(c) >= '0' && (int)(c) <= '7')
50 char *unescape(char *);
51 char *unquote(char *);
53 int
54 main(int argc, char *argv[])
56 size_t len, lineno = 0;
57 char *ep, *line, *input = NULL, *result = NULL;
58 struct in_addr addr;
59 in_addr_t s_addr;
61 while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
62 if (strncmp(line, "input:", 6) == 0) {
63 if (input)
64 free(input);
65 input = &line[7 + strspn(&line[7], WS)];
66 if (input == NULL)
67 errx(1, "missing input at line %ld",
68 (unsigned long)lineno);
69 input = strdup(unescape(unquote(input)));
70 } else if (strncmp(line, "result:", 7) == 0) {
71 if (result)
72 free(result);
73 result = &line[8 + strspn(&line[8], WS)];
74 if (result == NULL)
75 errx(1, "missing result at line %ld",
76 (unsigned long)lineno);
77 if (result == NULL)
78 errx(1,
79 "result without input at line %ld");
80 result = strdup(result);
82 addr.s_addr = inet_network(input);
84 errno = 0;
85 s_addr = strtoul(result, &ep, 0);
86 if (errno == ERANGE || errno == EINVAL)
87 err(1, "line %ld: strtoul(): %s",
88 (unsigned long)lineno, result);
90 if (addr.s_addr != s_addr)
91 #ifdef DEBUG
92 warnx("0x%08x != 0x%08x [%s]\tFAILED",
93 addr.s_addr, s_addr, input);
94 else
95 warnx("0x%08x == 0x%08x [%s]\tPASSED",
96 addr.s_addr, s_addr, input);
97 #else
98 errx(1, "0x%08x != 0x%08x [%s]",
99 addr.s_addr, s_addr, input);
100 #endif
101 } else
102 errx(1, "unknown directive at line %ld",
103 (unsigned long)lineno);
104 free(line);
106 exit(0);
110 * unquote
112 char *
113 unquote(char *orig)
115 char c, *cp, *new = orig;
116 int escaped = 0;
118 for (cp = orig; (*orig = *cp); cp++) {
119 if (*cp == '\'' || *cp == '"')
120 if (!escaped)
121 continue;
122 escaped = (*cp == '\\');
123 ++orig;
126 return (new);
130 * unescape - handle C escapes in a string
132 char *
133 unescape(char *orig)
135 char c, *cp, *new = orig;
136 int i;
138 for (cp = orig; (*orig = *cp); cp++, orig++) {
139 if (*cp != '\\')
140 continue;
142 switch (*++cp) {
143 case 'a': /* alert (bell) */
144 *orig = '\a';
145 continue;
146 case 'b': /* backspace */
147 *orig = '\b';
148 continue;
149 case 'e': /* escape */
150 *orig = '\e';
151 continue;
152 case 'f': /* formfeed */
153 *orig = '\f';
154 continue;
155 case 'n': /* newline */
156 *orig = '\n';
157 continue;
158 case 'r': /* carriage return */
159 *orig = '\r';
160 continue;
161 case 't': /* horizontal tab */
162 *orig = '\t';
163 continue;
164 case 'v': /* vertical tab */
165 *orig = '\v';
166 continue;
167 case '\\': /* backslash */
168 *orig = '\\';
169 continue;
170 case '\'': /* single quote */
171 *orig = '\'';
172 continue;
173 case '\"': /* double quote */
174 *orig = '"';
175 continue;
176 case '0':
177 case '1':
178 case '2':
179 case '3': /* octal */
180 case '4':
181 case '5':
182 case '6':
183 case '7': /* number */
184 for (i = 0, c = 0;
185 ISODIGIT((unsigned char)*cp) && i < 3;
186 i++, cp++) {
187 c <<= 3;
188 c |= (*cp - '0');
190 *orig = c;
191 --cp;
192 continue;
193 case 'x': /* hexidecimal number */
194 cp++; /* skip 'x' */
195 for (i = 0, c = 0;
196 isxdigit((unsigned char)*cp) && i < 2;
197 i++, cp++) {
198 c <<= 4;
199 if (isdigit((unsigned char)*cp))
200 c |= (*cp - '0');
201 else
202 c |= ((toupper((unsigned char)*cp) -
203 'A') + 10);
205 *orig = c;
206 --cp;
207 continue;
208 default:
209 --cp;
210 break;
214 return (new);