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