1 /* $NetBSD: subr.c,v 1.1.1.1 2009/04/12 15:33:59 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 112 /* 113 ******************************************************************************* 114 * 115 * StringToType -- 116 * 117 * Converts a string form of a query type name to its 118 * corresponding integer value. 119 * 120 ******************************************************************************* 121 */ 122 123 int 124 StringToType(type, dflt, errorfile) 125 char *type; 126 int dflt; 127 FILE *errorfile; 128 { 129 int result, success; 130 131 result = sym_ston(__p_type_syms, type, &success); 132 if (success) 133 return (result); 134 135 if (errorfile) 136 fprintf(errorfile, "unknown query type: %s\n", type); 137 return (dflt); 138 } 139 140 /* 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). 147 * 148 * int i; 149 * char *p = " foo bar ", *q; 150 * char buf[100]; 151 * 152 * q = p + pickString(p, buf, sizeof buff); 153 * assert (strcmp (q, " bar ") == 0) ; 154 * 155 */ 156 157 int 158 pickString(const char *src, char *dest, size_t dest_size) { 159 const char *start; 160 const char *end ; 161 size_t sublen ; 162 163 if (dest_size == 0 || dest == NULL || src == NULL) 164 return 0; 165 166 for (start = src ; isspace((unsigned char)*start) ; start++) 167 /* nada */ ; 168 169 for (end = start ; *end != '\0' && !isspace((unsigned char)*end) ; end++) 170 /* nada */ ; 171 172 sublen = end - start ; 173 174 if (sublen == 0 || sublen > (dest_size - 1)) 175 return 0; 176 177 strncpy (dest, start, sublen); 178 179 dest[sublen] = '\0' ; 180 181 return (end - src); 182 } 183 184 185 186 187 /* 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 193 * 194 * int i ; 195 * 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); 204 */ 205 int 206 matchString (const char *format, const char *src) { 207 const char *f = format; 208 const char *s = src; 209 210 if (f == NULL || s == NULL) 211 goto notfound; 212 213 if (isspace((unsigned char)*f)) { 214 while (isspace((unsigned char)*f)) 215 f++ ; 216 while (isspace((unsigned char)*s)) 217 s++ ; 218 } 219 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++ ; 238 } 239 } 240 notfound: 241 return 0 ; 242 } 243