xref: /csrg-svn/lib/libc/net/gethostnamadr.c (revision 15912)
1*15912Sralph /*	gethostnamadr.c	4.4	84/01/31	*/
215662Sralph 
315662Sralph #include <stdio.h>
415662Sralph #include <netdb.h>
515662Sralph #include <sys/file.h>
615662Sralph #include <ndbm.h>
715662Sralph 
815662Sralph #define	MAXALIASES	35
915662Sralph 
1015662Sralph static char HOSTDB[] = "/etc/hosts";
1115761Skarels DBM *_host_db = (DBM *)NULL;
1215662Sralph static struct hostent host;
1315662Sralph static char *host_aliases[MAXALIASES];
14*15912Sralph static char hostbuf[BUFSIZ+1];
1515761Skarels int _host_stayopen;	/* set by sethostent(), cleared by endhostent() */
1615662Sralph 
1715662Sralph static struct hostent *
1815662Sralph fetchhost(key)
1915662Sralph 	datum key;
2015662Sralph {
21*15912Sralph         register char *cp, *tp, **ap;
2215662Sralph 	register int naliases;
2315662Sralph 
24*15912Sralph         if (key.dptr == 0)
2515662Sralph                 return ((struct hostent *)NULL);
26*15912Sralph 	key = dbmfetch(_host_db, key);
2715662Sralph 	if (key.dptr == 0)
2815662Sralph                 return ((struct hostent *)NULL);
2915662Sralph         cp = key.dptr;
30*15912Sralph 	tp = hostbuf;
31*15912Sralph 	host.h_name = tp;
32*15912Sralph 	while (*tp++ = *cp++)
3315662Sralph 		;
3415662Sralph 	naliases = *(int *)cp; cp += sizeof (int);
3515662Sralph 	for (ap = host_aliases; naliases > 0; naliases--) {
36*15912Sralph 		*ap++ = tp;
37*15912Sralph 		while (*tp++ = *cp++)
3815662Sralph 			;
3915662Sralph 	}
4015662Sralph 	*ap = (char *)NULL;
4115662Sralph 	host.h_aliases = host_aliases;
4215662Sralph 	host.h_addrtype = *(int *)cp; cp += sizeof (int);
4315662Sralph 	host.h_length = *(int *)cp; cp += sizeof (int);
44*15912Sralph 	host.h_addr = tp;
45*15912Sralph 	bcopy(cp, tp, host.h_length);
4615662Sralph         return (&host);
4715662Sralph }
4815662Sralph 
4915662Sralph struct hostent *
5015662Sralph gethostbyname(nam)
5115662Sralph 	char *nam;
5215662Sralph {
5315662Sralph         datum key;
5415662Sralph 	register struct hostent *hp;
5515662Sralph 
5615761Skarels 	if ((_host_db == (DBM *)NULL)
5715761Skarels 	  && ((_host_db = ndbmopen(HOSTDB, O_RDONLY)) == (DBM *)NULL))
5815662Sralph                 return ((struct hostent *)NULL);
5915662Sralph         key.dptr = nam;
6015662Sralph         key.dsize = strlen(nam);
6115662Sralph 	hp = fetchhost(key);
6215761Skarels 	if (!_host_stayopen) {
6315761Skarels 		ndbmclose(_host_db);
6415761Skarels 		_host_db = (DBM *)NULL;
6515756Sralph 	}
6615662Sralph         return (hp);
6715662Sralph }
6815662Sralph 
6915662Sralph struct hostent *
7015662Sralph gethostbyaddr(addr, length)
7115662Sralph 	char *addr;
7215662Sralph 	int length;
7315662Sralph {
7415662Sralph         datum key;
7515662Sralph 	register struct hostent *hp;
7615662Sralph 
7715761Skarels 	if ((_host_db == (DBM *)NULL)
7815761Skarels 	  && ((_host_db = ndbmopen(HOSTDB, O_RDONLY)) == (DBM *)NULL))
7915662Sralph                 return ((struct hostent *)NULL);
8015662Sralph         key.dptr = addr;
8115662Sralph         key.dsize = length;
8215662Sralph 	hp = fetchhost(key);
8315761Skarels 	if (!_host_stayopen) {
8415761Skarels 		ndbmclose(_host_db);
8515761Skarels 		_host_db = (DBM *)NULL;
8615756Sralph 	}
8715662Sralph         return (hp);
8815662Sralph }
89