1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #if defined(LIBC_SCCS) && !defined(lint)
14 static char sccsid[] = "@(#)gethostent.c	5.4 (Berkeley) 03/07/88";
15 #endif /* LIBC_SCCS and not lint */
16 
17 #include <stdio.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netdb.h>
21 #include <ctype.h>
22 #include <ndbm.h>
23 
24 /*
25  * Internet version.
26  */
27 #define	MAXALIASES	35
28 #define	MAXADDRSIZE	14
29 
30 static FILE *hostf = NULL;
31 static char line[BUFSIZ+1];
32 static char hostaddr[MAXADDRSIZE];
33 static struct hostent host;
34 static char *host_aliases[MAXALIASES];
35 static char *host_addrs[] = {
36 	hostaddr,
37 	NULL
38 };
39 
40 /*
41  * The following is shared with gethostnamadr.c
42  */
43 char	*_host_file = "/etc/hosts";
44 int	_host_stayopen;
45 DBM	*_host_db;	/* set by gethostbyname(), gethostbyaddr() */
46 
47 static char *any();
48 
49 sethostent(f)
50 	int f;
51 {
52 	if (hostf != NULL)
53 		rewind(hostf);
54 	_host_stayopen |= f;
55 }
56 
57 endhostent()
58 {
59 	if (hostf) {
60 		fclose(hostf);
61 		hostf = NULL;
62 	}
63 	if (_host_db) {
64 		dbm_close(_host_db);
65 		_host_db = (DBM *)NULL;
66 	}
67 	_host_stayopen = 0;
68 }
69 
70 struct hostent *
71 gethostent()
72 {
73 	char *p;
74 	register char *cp, **q;
75 
76 	if (hostf == NULL && (hostf = fopen(_host_file, "r" )) == NULL)
77 		return (NULL);
78 again:
79 	if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
80 		return (NULL);
81 	if (*p == '#')
82 		goto again;
83 	cp = any(p, "#\n");
84 	if (cp == NULL)
85 		goto again;
86 	*cp = '\0';
87 	cp = any(p, " \t");
88 	if (cp == NULL)
89 		goto again;
90 	*cp++ = '\0';
91 	/* THIS STUFF IS INTERNET SPECIFIC */
92 	host.h_addr_list = host_addrs;
93 	*((u_long *)host.h_addr) = inet_addr(p);
94 	host.h_length = sizeof (u_long);
95 	host.h_addrtype = AF_INET;
96 	while (*cp == ' ' || *cp == '\t')
97 		cp++;
98 	host.h_name = cp;
99 	q = host.h_aliases = host_aliases;
100 	cp = any(cp, " \t");
101 	if (cp != NULL)
102 		*cp++ = '\0';
103 	while (cp && *cp) {
104 		if (*cp == ' ' || *cp == '\t') {
105 			cp++;
106 			continue;
107 		}
108 		if (q < &host_aliases[MAXALIASES - 1])
109 			*q++ = cp;
110 		cp = any(cp, " \t");
111 		if (cp != NULL)
112 			*cp++ = '\0';
113 	}
114 	*q = NULL;
115 	return (&host);
116 }
117 
118 sethostfile(file)
119 	char *file;
120 {
121 	_host_file = file;
122 }
123 
124 static char *
125 any(cp, match)
126 	register char *cp;
127 	char *match;
128 {
129 	register char *mp, c;
130 
131 	while (c = *cp) {
132 		for (mp = match; *mp; mp++)
133 			if (*mp == c)
134 				return (cp);
135 		cp++;
136 	}
137 	return ((char *)0);
138 }
139