drm/linux: Add kasprintf and kvasprintf()
[dragonfly.git] / contrib / tcp_wrappers / misc.c
blobc16bb8c63499d987eca10e4fbf474daa6b886a92
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 */
9 #include <sys/types.h>
10 #include <sys/param.h>
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 #include <stdio.h>
14 #include <string.h>
16 #include "tcpd.h"
18 extern char *fgets();
20 #ifndef INADDR_NONE
21 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
22 #endif
24 /* xgets - fgets() with backslash-newline stripping */
26 char *xgets(ptr, len, fp)
27 char *ptr;
28 int len;
29 FILE *fp;
31 int got;
32 char *start = ptr;
34 while (len > 1 && fgets(ptr, len, fp)) {
35 got = strlen(ptr);
36 if (got >= 1 && ptr[got - 1] == '\n') {
37 tcpd_context.line++;
38 if (got >= 2 && ptr[got - 2] == '\\') {
39 got -= 2;
40 } else {
41 return (start);
44 ptr += got;
45 len -= got;
46 ptr[0] = 0;
48 return (ptr > start ? start : 0);
51 /* split_at - break string at delimiter or return NULL */
53 char *split_at(string, delimiter)
54 char *string;
55 int delimiter;
57 char *cp;
59 #ifdef INET6
60 int bracket = 0;
62 for (cp = string; cp && *cp; cp++) {
63 switch (*cp) {
64 case '[':
65 bracket++;
66 break;
67 case ']':
68 bracket--;
69 break;
70 default:
71 if (bracket == 0 && *cp == delimiter) {
72 *cp++ = 0;
73 return cp;
75 break;
78 return (NULL);
79 #else
80 if ((cp = strchr(string, delimiter)) != 0)
81 *cp++ = 0;
82 return (cp);
83 #endif
86 /* dot_quad_addr - convert dotted quad to internal form */
88 unsigned long dot_quad_addr(str)
89 char *str;
91 int in_run = 0;
92 int runs = 0;
93 char *cp = str;
95 /* Count the number of runs of non-dot characters. */
97 while (*cp) {
98 if (*cp == '.') {
99 in_run = 0;
100 } else if (in_run == 0) {
101 in_run = 1;
102 runs++;
104 cp++;
106 return (runs == 4 ? inet_addr(str) : INADDR_NONE);