xref: /csrg-svn/lib/libc/net/gethostnamadr.c (revision 15761)
1*15761Skarels /*	gethostnamadr.c	4.3	83/12/21	*/
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";
11*15761Skarels DBM *_host_db = (DBM *)NULL;
1215662Sralph static datum curkey;
1315662Sralph static struct hostent host;
1415662Sralph static char *host_aliases[MAXALIASES];
15*15761Skarels int _host_stayopen;	/* set by sethostent(), cleared by endhostent() */
1615662Sralph 
1715662Sralph static struct hostent *
1815662Sralph fetchhost(key)
1915662Sralph 	datum key;
2015662Sralph {
2115662Sralph         register char *cp, **ap;
2215662Sralph 	register int naliases;
2315662Sralph 
2415662Sralph         curkey = key;
2515662Sralph         if (curkey.dptr == 0)
2615662Sralph                 return ((struct hostent *)NULL);
27*15761Skarels 	key = dbmfetch(_host_db, curkey);
2815662Sralph 	if (key.dptr == 0)
2915662Sralph                 return ((struct hostent *)NULL);
3015662Sralph         cp = key.dptr;
3115662Sralph 	host.h_name = cp;
3215662Sralph 	while (*cp++)
3315662Sralph 		;
3415662Sralph 	naliases = *(int *)cp; cp += sizeof (int);
3515662Sralph 	for (ap = host_aliases; naliases > 0; naliases--) {
3615662Sralph 		*ap++ = cp;
3715662Sralph 		while (*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);
4415662Sralph 	host.h_addr = cp;
4515662Sralph         return (&host);
4615662Sralph }
4715662Sralph 
4815662Sralph struct hostent *
4915662Sralph gethostbyname(nam)
5015662Sralph 	char *nam;
5115662Sralph {
5215662Sralph         datum key;
5315662Sralph 	register struct hostent *hp;
5415662Sralph 
55*15761Skarels 	if ((_host_db == (DBM *)NULL)
56*15761Skarels 	  && ((_host_db = ndbmopen(HOSTDB, O_RDONLY)) == (DBM *)NULL))
5715662Sralph                 return ((struct hostent *)NULL);
5815662Sralph         key.dptr = nam;
5915662Sralph         key.dsize = strlen(nam);
6015662Sralph 	hp = fetchhost(key);
61*15761Skarels 	if (!_host_stayopen) {
62*15761Skarels 		ndbmclose(_host_db);
63*15761Skarels 		_host_db = (DBM *)NULL;
6415756Sralph 	}
6515662Sralph         return (hp);
6615662Sralph }
6715662Sralph 
6815662Sralph struct hostent *
6915662Sralph gethostbyaddr(addr, length)
7015662Sralph 	char *addr;
7115662Sralph 	int length;
7215662Sralph {
7315662Sralph         datum key;
7415662Sralph 	register struct hostent *hp;
7515662Sralph 
76*15761Skarels 	if ((_host_db == (DBM *)NULL)
77*15761Skarels 	  && ((_host_db = ndbmopen(HOSTDB, O_RDONLY)) == (DBM *)NULL))
7815662Sralph                 return ((struct hostent *)NULL);
7915662Sralph         key.dptr = addr;
8015662Sralph         key.dsize = length;
8115662Sralph 	hp = fetchhost(key);
82*15761Skarels 	if (!_host_stayopen) {
83*15761Skarels 		ndbmclose(_host_db);
84*15761Skarels 		_host_db = (DBM *)NULL;
8515756Sralph 	}
8615662Sralph         return (hp);
8715662Sralph }
88