1 /*	gethostent.c	4.9	84/08/28	*/
2 
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netdb.h>
7 #include <ctype.h>
8 #include <ndbm.h>
9 
10 /*
11  * Internet version.
12  */
13 #define	MAXALIASES	35
14 #define	MAXADDRSIZE	14
15 
16 static char *HOSTDB = "/etc/hosts";
17 static FILE *hostf = NULL;
18 static char line[BUFSIZ+1];
19 static char hostaddr[MAXADDRSIZE];
20 static struct hostent host;
21 static char *host_aliases[MAXALIASES];
22 int _host_stayopen;
23 DBM *_host_db;		/* set by gethostbyname(), gethostbyaddr() */
24 static char *any();
25 
26 sethostent(f)
27 	int f;
28 {
29 	if (hostf != NULL)
30 		rewind(hostf);
31 	_host_stayopen |= f;
32 }
33 
34 endhostent()
35 {
36 	if (hostf) {
37 		fclose(hostf);
38 		hostf = NULL;
39 	}
40 	if (_host_db) {
41 		dbm_close(_host_db);
42 		_host_db = (DBM *)NULL;
43 	}
44 	_host_stayopen = 0;
45 }
46 
47 struct hostent *
48 gethostent()
49 {
50 	char *p;
51 	register char *cp, **q;
52 
53 	if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL)
54 		return (NULL);
55 again:
56 	if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
57 		return (NULL);
58 	if (*p == '#')
59 		goto again;
60 	cp = any(p, "#\n");
61 	if (cp == NULL)
62 		goto again;
63 	*cp = '\0';
64 	cp = any(p, " \t");
65 	if (cp == NULL)
66 		goto again;
67 	*cp++ = '\0';
68 	/* THIS STUFF IS INTERNET SPECIFIC */
69 	host.h_addr = hostaddr;
70 	*((u_long *)host.h_addr) = inet_addr(p);
71 	host.h_length = sizeof (u_long);
72 	host.h_addrtype = AF_INET;
73 	while (*cp == ' ' || *cp == '\t')
74 		cp++;
75 	host.h_name = cp;
76 	q = host.h_aliases = host_aliases;
77 	cp = any(cp, " \t");
78 	if (cp != NULL)
79 		*cp++ = '\0';
80 	while (cp && *cp) {
81 		if (*cp == ' ' || *cp == '\t') {
82 			cp++;
83 			continue;
84 		}
85 		if (q < &host_aliases[MAXALIASES - 1])
86 			*q++ = cp;
87 		cp = any(cp, " \t");
88 		if (cp != NULL)
89 			*cp++ = '\0';
90 	}
91 	*q = NULL;
92 	return (&host);
93 }
94 
95 sethostfile(file)
96 	char *file;
97 {
98 	HOSTDB = file;
99 }
100 
101 static char *
102 any(cp, match)
103 	register char *cp;
104 	char *match;
105 {
106 	register char *mp, c;
107 
108 	while (c = *cp) {
109 		for (mp = match; *mp; mp++)
110 			if (*mp == c)
111 				return (cp);
112 		cp++;
113 	}
114 	return ((char *)0);
115 }
116