Merge branch 'vendor/FILE'
[dragonfly.git] / contrib / tcp_wrappers / misc.c
CommitLineData
984263bc
MD
1 /*
2 * Misc routines that are used by tcpd and by tcpdchk.
3 *
4 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
5 *
6 * $FreeBSD: src/contrib/tcp_wrappers/misc.c,v 1.2 2000/02/03 10:26:58 shin Exp $
1de703da 7 * $DragonFly: src/contrib/tcp_wrappers/misc.c,v 1.2 2003/06/17 04:24:06 dillon Exp $
984263bc
MD
8 */
9
10#ifndef lint
11static char sccsic[] = "@(#) misc.c 1.2 96/02/11 17:01:29";
12#endif
13
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>
20
21#include "tcpd.h"
22
23extern char *fgets();
24
25#ifndef INADDR_NONE
26#define INADDR_NONE (-1) /* XXX should be 0xffffffff */
27#endif
28
29/* xgets - fgets() with backslash-newline stripping */
30
31char *xgets(ptr, len, fp)
32char *ptr;
33int len;
34FILE *fp;
35{
36 int got;
37 char *start = ptr;
38
f604ab72 39 while (len > 1 && fgets(ptr, len, fp)) {
984263bc
MD
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);
47 }
48 }
49 ptr += got;
50 len -= got;
51 ptr[0] = 0;
52 }
53 return (ptr > start ? start : 0);
54}
55
56/* split_at - break string at delimiter or return NULL */
57
58char *split_at(string, delimiter)
59char *string;
60int delimiter;
61{
62 char *cp;
63
64#ifdef INET6
65 int bracket = 0;
66
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;
79 }
80 break;
81 }
82 }
83 return (NULL);
84#else
85 if ((cp = strchr(string, delimiter)) != 0)
86 *cp++ = 0;
87 return (cp);
88#endif
89}
90
91/* dot_quad_addr - convert dotted quad to internal form */
92
93unsigned long dot_quad_addr(str)
94char *str;
95{
96 int in_run = 0;
97 int runs = 0;
98 char *cp = str;
99
100 /* Count the number of runs of non-dot characters. */
101
102 while (*cp) {
103 if (*cp == '.') {
104 in_run = 0;
105 } else if (in_run == 0) {
106 in_run = 1;
107 runs++;
108 }
109 cp++;
110 }
111 return (runs == 4 ? inet_addr(str) : INADDR_NONE);
112}