|
|
1.1 root 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 the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: */
17:
18: #if defined(LIBC_SCCS) && !defined(lint)
19: static char sccsid[] = "@(#)gethostent.c 5.5 (Berkeley) 6/27/88";
20: #endif /* LIBC_SCCS and not lint */
21:
22: #include <stdio.h>
23: #include <sys/types.h>
24: #include <sys/socket.h>
25: #include <netdb.h>
26: #include <ctype.h>
27: #include <ndbm.h>
28:
29: /*
30: * Internet version.
31: */
32: #define MAXALIASES 35
33: #define MAXADDRSIZE 14
34:
35: static FILE *hostf = NULL;
36: static char line[BUFSIZ+1];
37: static char hostaddr[MAXADDRSIZE];
38: static struct hostent host;
39: static char *host_aliases[MAXALIASES];
40: static char *host_addrs[] = {
41: hostaddr,
42: NULL
43: };
44:
45: /*
46: * The following is shared with gethostnamadr.c
47: */
48: char *_host_file = "/etc/hosts";
49: int _host_stayopen;
50: DBM *_host_db; /* set by gethostbyname(), gethostbyaddr() */
51:
52: static char *any();
53:
54: sethostent(f)
55: int f;
56: {
57: if (hostf != NULL)
58: rewind(hostf);
59: _host_stayopen |= f;
60: }
61:
62: endhostent()
63: {
64: if (hostf) {
65: fclose(hostf);
66: hostf = NULL;
67: }
68: if (_host_db) {
69: dbm_close(_host_db);
70: _host_db = (DBM *)NULL;
71: }
72: _host_stayopen = 0;
73: }
74:
75: struct hostent *
76: gethostent()
77: {
78: char *p;
79: register char *cp, **q;
80:
81: if (hostf == NULL && (hostf = fopen(_host_file, "r" )) == NULL)
82: return (NULL);
83: again:
84: if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
85: return (NULL);
86: if (*p == '#')
87: goto again;
88: cp = any(p, "#\n");
89: if (cp == NULL)
90: goto again;
91: *cp = '\0';
92: cp = any(p, " \t");
93: if (cp == NULL)
94: goto again;
95: *cp++ = '\0';
96: /* THIS STUFF IS INTERNET SPECIFIC */
97: host.h_addr_list = host_addrs;
98: *((u_long *)host.h_addr) = inet_addr(p);
99: host.h_length = sizeof (u_long);
100: host.h_addrtype = AF_INET;
101: while (*cp == ' ' || *cp == '\t')
102: cp++;
103: host.h_name = cp;
104: q = host.h_aliases = host_aliases;
105: cp = any(cp, " \t");
106: if (cp != NULL)
107: *cp++ = '\0';
108: while (cp && *cp) {
109: if (*cp == ' ' || *cp == '\t') {
110: cp++;
111: continue;
112: }
113: if (q < &host_aliases[MAXALIASES - 1])
114: *q++ = cp;
115: cp = any(cp, " \t");
116: if (cp != NULL)
117: *cp++ = '\0';
118: }
119: *q = NULL;
120: return (&host);
121: }
122:
123: sethostfile(file)
124: char *file;
125: {
126: _host_file = file;
127: }
128:
129: static char *
130: any(cp, match)
131: register char *cp;
132: char *match;
133: {
134: register char *mp, c;
135:
136: while (c = *cp) {
137: for (mp = match; *mp; mp++)
138: if (*mp == c)
139: return (cp);
140: cp++;
141: }
142: return ((char *)0);
143: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.