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
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";
45 *******************************************************************************
49 * Miscellaneous subroutines for the name server
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>
78 #include "port_after.h"
85 *******************************************************************************
89 * Converts a string form of a query class name to its
90 * corresponding integer value.
92 *******************************************************************************
95 StringToClass(class, dflt
, errorfile
)
102 result
= sym_ston(__p_class_syms
, class, &success
);
107 fprintf(errorfile
, "unknown query class: %s\n", class);
113 *******************************************************************************
117 * Converts a string form of a query type name to its
118 * corresponding integer value.
120 *******************************************************************************
124 StringToType(type
, dflt
, errorfile
)
131 result
= sym_ston(__p_type_syms
, type
, &success
);
136 fprintf(errorfile
, "unknown query type: %s\n", type
);
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).
149 * char *p = " foo bar ", *q;
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
) {
163 if (dest_size
== 0 || dest
== NULL
|| src
== NULL
)
166 for (start
= src
; isspace((unsigned char)*start
) ; start
++)
169 for (end
= start
; *end
!= '\0' && !isspace((unsigned char)*end
) ; end
++)
172 sublen
= end
- start
;
174 if (sublen
== 0 || sublen
> (dest_size
- 1))
177 strncpy (dest
, start
, sublen
);
179 dest
[sublen
] = '\0' ;
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
196 * i = matchString(" a b c", "a b c") ;
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");
202 * i = matchString("aa bb ", "aa bb ddd sd");
206 matchString (const char *format
, const char *src
) {
207 const char *f
= format
;
210 if (f
== NULL
|| s
== NULL
)
213 if (isspace((unsigned char)*f
)) {
214 while (isspace((unsigned char)*f
))
216 while (isspace((unsigned char)*s
))
221 if (isspace((unsigned char)*f
)) {
222 if (!isspace((unsigned char)*s
))
224 while(isspace((unsigned char)*s
))
226 /* any amount of whitespace in the format string
227 will match any amount of space in the source
229 while (isspace((unsigned char)*f
))
231 } else if (*f
== '\0') {
233 } else if (*f
!= *s
) {