rename library routines so as not conflict with gethostname
[unix-history] / usr / src / lib / libc / net / SCCS / hosttable / gethostent.c
CommitLineData
5e649950 1/* gethostent.c 4.2 82/10/05 */
bcb2abaf
SL
2
3#include <stdio.h>
5e649950 4#include <sys/types.h>
bcb2abaf
SL
5#include <sys/socket.h>
6#include <netdb.h>
7#include <ctype.h>
8
5e649950
SL
9/*
10 * Internet version.
11 */
bcb2abaf 12#define MAXALIASES 35
5e649950 13#define MAXADDRSIZE 14
bcb2abaf
SL
14
15static char HOSTDB[] = "/usr/lib/hosts";
16static FILE *hostf = NULL;
17static char line[BUFSIZ+1];
5e649950 18static char hostaddr[MAXADDRSIZE];
bcb2abaf
SL
19static struct hostent host;
20static char *host_aliases[MAXALIASES];
21static int stayopen = 0;
bcb2abaf
SL
22static char *any();
23
24sethostent(f)
25 int f;
26{
27 if (hostf == NULL)
28 hostf = fopen(HOSTDB, "r" );
29 else
30 rewind(hostf);
31 stayopen |= f;
32}
33
34endhostent()
35{
36 if (hostf && !stayopen) {
37 fclose(hostf);
38 hostf = NULL;
39 }
40}
41
42struct hostent *
43gethostent()
44{
45 char *p;
46 register char *cp, **q;
47
48 if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL)
49 return (NULL);
50again:
51 if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
52 return (NULL);
53 if (*p == '#')
54 goto again;
55 cp = any(p, "#\n");
56 if (cp == NULL)
57 goto again;
58 *cp = '\0';
59 cp = any(p, " \t");
60 if (cp == NULL)
61 goto again;
62 *cp++ = '\0';
5e649950
SL
63 /* THIS STUFF IS INTERNET SPECIFIC */
64 host.h_addr = hostaddr;
65 *((u_long *)host.h_addr) = inet_addr(p);
66 host.h_length = sizeof (u_long);
67 host.h_addrtype = AF_INET;
bcb2abaf
SL
68 while (*cp == ' ' || *cp == '\t')
69 cp++;
70 host.h_name = cp;
bcb2abaf
SL
71 host.h_aliases = host_aliases;
72 cp = any(cp, " \t");
73 if (cp != NULL)
74 *cp++ = '\0';
75 q = host_aliases;
76 while (*cp) {
77 if (*cp == ' ' || *cp == '\t') {
78 cp++;
79 continue;
80 }
81 if (q < &host_aliases[MAXALIASES - 1])
82 *q++ = cp;
83 cp = any(cp, " \t");
84 if (*cp != NULL)
85 *cp++ = '\0';
86 }
87 *q = NULL;
88 return (&host);
89}
90
bcb2abaf
SL
91static char *
92any(cp, match)
93 register char *cp;
94 char *match;
95{
96 register char *mp, c;
97
98 while (c = *cp) {
99 for (mp = match; *mp; mp++)
100 if (*mp == c)
101 return (cp);
102 cp++;
103 }
104 return ((char *)0);
105}