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