No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / libbind / dist / tests / subr.c
blob306d22f9f12117fdfbb23b63da4bf1e4b8eb89b2
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
20 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
22 * Permission to use, copy, modify, and distribute this software for any
23 * purpose with or without fee is hereby granted, provided that the above
24 * copyright notice and this permission notice appear in all copies, and that
25 * the name of Digital Equipment Corporation not be used in advertising or
26 * publicity pertaining to distribution of the document or software without
27 * specific, written prior permission.
29 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
30 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
32 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
33 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
34 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
35 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
36 * SOFTWARE.
39 #ifndef lint
40 static const char sccsid[] = "@(#)subr.c 5.24 (Berkeley) 3/2/91";
41 static const char rcsid[] = "Id: subr.c,v 1.3 2009/03/03 23:49:07 tbox Exp";
42 #endif /* not lint */
45 *******************************************************************************
47 * subr.c --
49 * Miscellaneous subroutines for the name server
50 * lookup program.
52 * Copyright (c) 1985
53 * Andrew Cherenson
54 * U.C. Berkeley
55 * CS298-26 Fall 1985
57 *******************************************************************************
60 #include "port_before.h"
62 #include <sys/types.h>
63 #include <sys/param.h>
64 #include <sys/socket.h>
66 #include <netinet/in.h>
67 #include <arpa/nameser.h>
68 #include <arpa/inet.h>
70 #include <ctype.h>
71 #include <netdb.h>
72 #include <setjmp.h>
73 #include <signal.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
78 #include "port_after.h"
80 #include "resolv.h"
81 #include "res.h"
85 *******************************************************************************
87 * StringToClass --
89 * Converts a string form of a query class name to its
90 * corresponding integer value.
92 *******************************************************************************
94 int
95 StringToClass(class, dflt, errorfile)
96 char *class;
97 int dflt;
98 FILE *errorfile;
100 int result, success;
102 result = sym_ston(__p_class_syms, class, &success);
103 if (success)
104 return result;
106 if (errorfile)
107 fprintf(errorfile, "unknown query class: %s\n", class);
108 return(dflt);
113 *******************************************************************************
115 * StringToType --
117 * Converts a string form of a query type name to its
118 * corresponding integer value.
120 *******************************************************************************
124 StringToType(type, dflt, errorfile)
125 char *type;
126 int dflt;
127 FILE *errorfile;
129 int result, success;
131 result = sym_ston(__p_type_syms, type, &success);
132 if (success)
133 return (result);
135 if (errorfile)
136 fprintf(errorfile, "unknown query type: %s\n", type);
137 return (dflt);
141 * Skip over leading white space in SRC and then copy the next sequence of
142 * non-whitespace characters into DEST. No more than (DEST_SIZE - 1)
143 * characters are copied. DEST is always null-terminated. Returns 0 if no
144 * characters could be copied into DEST. Returns the number of characters
145 * in SRC that were processed (i.e. the count of characters in the leading
146 * white space and the first non-whitespace sequence).
148 * int i;
149 * char *p = " foo bar ", *q;
150 * char buf[100];
152 * q = p + pickString(p, buf, sizeof buff);
153 * assert (strcmp (q, " bar ") == 0) ;
158 pickString(const char *src, char *dest, size_t dest_size) {
159 const char *start;
160 const char *end ;
161 size_t sublen ;
163 if (dest_size == 0 || dest == NULL || src == NULL)
164 return 0;
166 for (start = src ; isspace((unsigned char)*start) ; start++)
167 /* nada */ ;
169 for (end = start ; *end != '\0' && !isspace((unsigned char)*end) ; end++)
170 /* nada */ ;
172 sublen = end - start ;
174 if (sublen == 0 || sublen > (dest_size - 1))
175 return 0;
177 strncpy (dest, start, sublen);
179 dest[sublen] = '\0' ;
181 return (end - src);
188 * match the string FORMAT against the string SRC. Leading whitespace in
189 * FORMAT will match any amount of (including no) leading whitespace in
190 * SRC. Any amount of whitespace inside FORMAT matches any non-zero amount
191 * of whitespace in SRC. Value returned is 0 if match didn't occur, or the
192 * amount of characters in SRC that did match
194 * int i ;
196 * i = matchString(" a b c", "a b c") ;
197 * assert (i == 5) ;
198 * i = matchString("a b c", " a b c");
199 * assert (i == 0) ; becasue no leading white space in format
200 * i = matchString(" a b c", " a b c");
201 * assert(i == 12);
202 * i = matchString("aa bb ", "aa bb ddd sd");
203 * assert(i == 16);
206 matchString (const char *format, const char *src) {
207 const char *f = format;
208 const char *s = src;
210 if (f == NULL || s == NULL)
211 goto notfound;
213 if (isspace((unsigned char)*f)) {
214 while (isspace((unsigned char)*f))
215 f++ ;
216 while (isspace((unsigned char)*s))
217 s++ ;
220 while (1) {
221 if (isspace((unsigned char)*f)) {
222 if (!isspace((unsigned char)*s))
223 goto notfound;
224 while(isspace((unsigned char)*s))
225 s++;
226 /* any amount of whitespace in the format string
227 will match any amount of space in the source
228 string. */
229 while (isspace((unsigned char)*f))
230 f++;
231 } else if (*f == '\0') {
232 return (s - src);
233 } else if (*f != *s) {
234 goto notfound;
235 } else {
236 s++ ;
237 f++ ;
240 notfound:
241 return 0 ;