1 /* $NetBSD: subr.c,v 1.1.1.2 2012/09/09 16:08:11 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC") 5 * 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. 9 * 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. 17 */ 18 19 /* 20 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 21 * 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. 28 * 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. 37 */ 38 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 */ 43 44 /* 45 ******************************************************************************* 46 * 47 * subr.c -- 48 * 49 * Miscellaneous subroutines for the name server 50 * lookup program. 51 * 52 * Copyright (c) 1985 53 * Andrew Cherenson 54 * U.C. Berkeley 55 * CS298-26 Fall 1985 56 * 57 ******************************************************************************* 58 */ 59 60 #include "port_before.h" 61 62 #include <sys/types.h> 63 #include <sys/param.h> 64 #include <sys/socket.h> 65 66 #include <netinet/in.h> 67 #include <arpa/nameser.h> 68 #include <arpa/inet.h> 69 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> 77 78 #include "port_after.h" 79 80 #include "resolv.h" 81 #include "res.h" 82 83 84 /* 85 ******************************************************************************* 86 * 87 * StringToClass -- 88 * 89 * Converts a string form of a query class name to its 90 * corresponding integer value. 91 * 92 ******************************************************************************* 93 */ 94 int 95 StringToClass(class, dflt, errorfile) 96 char *class; 97 int dflt; 98 FILE *errorfile; 99 { 100 int result, success; 101 102 result = sym_ston(__p_class_syms, class, &success); 103 if (success) 104 return result; 105 106 if (errorfile) 107 fprintf(errorfile, "unknown query class: %s\n", class); 108 return(dflt); 109 } 110 111 113 /* 114 ******************************************************************************* 115 * 116 * StringToType -- 117 * 118 * Converts a string form of a query type name to its 119 * corresponding integer value. 120 * 121 ******************************************************************************* 122 */ 123 124 int 125 StringToType(type, dflt, errorfile) 126 char *type; 127 int dflt; 128 FILE *errorfile; 129 { 130 int result, success; 131 132 result = sym_ston(__p_type_syms, type, &success); 133 if (success) 134 return (result); 135 136 if (errorfile) 137 fprintf(errorfile, "unknown query type: %s\n", type); 138 return (dflt); 139 } 140 141 /* 143 * Skip over leading white space in SRC and then copy the next sequence of 144 * non-whitespace characters into DEST. No more than (DEST_SIZE - 1) 145 * characters are copied. DEST is always null-terminated. Returns 0 if no 146 * characters could be copied into DEST. Returns the number of characters 147 * in SRC that were processed (i.e. the count of characters in the leading 148 * white space and the first non-whitespace sequence). 149 * 150 * int i; 151 * char *p = " foo bar ", *q; 152 * char buf[100]; 153 * 154 * q = p + pickString(p, buf, sizeof buff); 155 * assert (strcmp (q, " bar ") == 0) ; 156 * 157 */ 158 159 int 160 pickString(const char *src, char *dest, size_t dest_size) { 161 const char *start; 162 const char *end ; 163 size_t sublen ; 164 165 if (dest_size == 0 || dest == NULL || src == NULL) 166 return 0; 167 168 for (start = src ; isspace((unsigned char)*start) ; start++) 169 /* nada */ ; 170 171 for (end = start ; *end != '\0' && !isspace((unsigned char)*end) ; end++) 172 /* nada */ ; 173 174 sublen = end - start ; 175 176 if (sublen == 0 || sublen > (dest_size - 1)) 177 return 0; 178 179 strncpy (dest, start, sublen); 180 181 dest[sublen] = '\0' ; 182 183 return (end - src); 184 } 185 186 187 188 189 /* 190 * match the string FORMAT against the string SRC. Leading whitespace in 191 * FORMAT will match any amount of (including no) leading whitespace in 192 * SRC. Any amount of whitespace inside FORMAT matches any non-zero amount 193 * of whitespace in SRC. Value returned is 0 if match didn't occur, or the 194 * amount of characters in SRC that did match 195 * 196 * int i ; 197 * 198 * i = matchString(" a b c", "a b c") ; 199 * assert (i == 5) ; 200 * i = matchString("a b c", " a b c"); 201 * assert (i == 0) ; becasue no leading white space in format 202 * i = matchString(" a b c", " a b c"); 203 * assert(i == 12); 204 * i = matchString("aa bb ", "aa bb ddd sd"); 205 * assert(i == 16); 206 */ 207 int 208 matchString (const char *format, const char *src) { 209 const char *f = format; 210 const char *s = src; 211 212 if (f == NULL || s == NULL) 213 goto notfound; 214 215 if (isspace((unsigned char)*f)) { 216 while (isspace((unsigned char)*f)) 217 f++ ; 218 while (isspace((unsigned char)*s)) 219 s++ ; 220 } 221 222 while (1) { 223 if (isspace((unsigned char)*f)) { 224 if (!isspace((unsigned char)*s)) 225 goto notfound; 226 while(isspace((unsigned char)*s)) 227 s++; 228 /* any amount of whitespace in the format string 229 will match any amount of space in the source 230 string. */ 231 while (isspace((unsigned char)*f)) 232 f++; 233 } else if (*f == '\0') { 234 return (s - src); 235 } else if (*f != *s) { 236 goto notfound; 237 } else { 238 s++ ; 239 f++ ; 240 } 241 } 242 notfound: 243 return 0 ; 244 } 245