1 /* $NetBSD: misc.c,v 1.2 1997/10/09 21:20:35 christos Exp $ */ 2 3 /* 4 * Misc routines that are used by tcpd and by tcpdchk. 5 * 6 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 7 */ 8 9 #include <sys/cdefs.h> 10 #ifndef lint 11 #if 0 12 static char sccsic[] = "@(#) misc.c 1.2 96/02/11 17:01:29"; 13 #else 14 __RCSID("$NetBSD: misc.c,v 1.2 1997/10/09 21:20:35 christos Exp $"); 15 #endif 16 #endif 17 18 #include <sys/types.h> 19 #include <sys/param.h> 20 #include <netinet/in.h> 21 #include <arpa/inet.h> 22 #include <stdio.h> 23 #include <string.h> 24 25 #include "tcpd.h" 26 27 #ifndef INADDR_NONE 28 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */ 29 #endif 30 31 /* xgets - fgets() with backslash-newline stripping */ 32 33 char *xgets(ptr, len, fp) 34 char *ptr; 35 int len; 36 FILE *fp; 37 { 38 int got; 39 char *start = ptr; 40 41 while (fgets(ptr, len, fp)) { 42 got = strlen(ptr); 43 if (got >= 1 && ptr[got - 1] == '\n') { 44 tcpd_context.line++; 45 if (got >= 2 && ptr[got - 2] == '\\') { 46 got -= 2; 47 } else { 48 return (start); 49 } 50 } 51 ptr += got; 52 len -= got; 53 ptr[0] = 0; 54 } 55 return (ptr > start ? start : 0); 56 } 57 58 /* split_at - break string at delimiter or return NULL */ 59 60 char *split_at(string, delimiter) 61 char *string; 62 int delimiter; 63 { 64 char *cp; 65 66 if ((cp = strchr(string, delimiter)) != 0) 67 *cp++ = 0; 68 return (cp); 69 } 70 71 /* dot_quad_addr - convert dotted quad to internal form */ 72 73 unsigned long dot_quad_addr(str) 74 char *str; 75 { 76 int in_run = 0; 77 int runs = 0; 78 char *cp = str; 79 80 /* Count the number of runs of non-dot characters. */ 81 82 while (*cp) { 83 if (*cp == '.') { 84 in_run = 0; 85 } else if (in_run == 0) { 86 in_run = 1; 87 runs++; 88 } 89 cp++; 90 } 91 return (runs == 4 ? inet_addr(str) : INADDR_NONE); 92 } 93