Make DDB optional in vkernels.
[dragonfly.git] / contrib / tcp_wrappers / misc.c
blobb6bf5cb99dd2b687291be9c216737a4b779df9f4
1 /*
2 * Misc routines that are used by tcpd and by tcpdchk.
3 *
4 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
6 * $FreeBSD: src/contrib/tcp_wrappers/misc.c,v 1.2 2000/02/03 10:26:58 shin Exp $
7 * $DragonFly: src/contrib/tcp_wrappers/misc.c,v 1.2 2003/06/17 04:24:06 dillon Exp $
8 */
10 #ifndef lint
11 static char sccsic[] = "@(#) misc.c 1.2 96/02/11 17:01:29";
12 #endif
14 #include <sys/types.h>
15 #include <sys/param.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <stdio.h>
19 #include <string.h>
21 #include "tcpd.h"
23 extern char *fgets();
25 #ifndef INADDR_NONE
26 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
27 #endif
29 /* xgets - fgets() with backslash-newline stripping */
31 char *xgets(ptr, len, fp)
32 char *ptr;
33 int len;
34 FILE *fp;
36 int got;
37 char *start = ptr;
39 while (fgets(ptr, len, fp)) {
40 got = strlen(ptr);
41 if (got >= 1 && ptr[got - 1] == '\n') {
42 tcpd_context.line++;
43 if (got >= 2 && ptr[got - 2] == '\\') {
44 got -= 2;
45 } else {
46 return (start);
49 ptr += got;
50 len -= got;
51 ptr[0] = 0;
53 return (ptr > start ? start : 0);
56 /* split_at - break string at delimiter or return NULL */
58 char *split_at(string, delimiter)
59 char *string;
60 int delimiter;
62 char *cp;
64 #ifdef INET6
65 int bracket = 0;
67 for (cp = string; cp && *cp; cp++) {
68 switch (*cp) {
69 case '[':
70 bracket++;
71 break;
72 case ']':
73 bracket--;
74 break;
75 default:
76 if (bracket == 0 && *cp == delimiter) {
77 *cp++ = 0;
78 return cp;
80 break;
83 return (NULL);
84 #else
85 if ((cp = strchr(string, delimiter)) != 0)
86 *cp++ = 0;
87 return (cp);
88 #endif
91 /* dot_quad_addr - convert dotted quad to internal form */
93 unsigned long dot_quad_addr(str)
94 char *str;
96 int in_run = 0;
97 int runs = 0;
98 char *cp = str;
100 /* Count the number of runs of non-dot characters. */
102 while (*cp) {
103 if (*cp == '.') {
104 in_run = 0;
105 } else if (in_run == 0) {
106 in_run = 1;
107 runs++;
109 cp++;
111 return (runs == 4 ? inet_addr(str) : INADDR_NONE);