1*15756Sralph /* gethostnamadr.c 4.2 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"; 1115662Sralph static DBM *db = (DBM *)NULL; 1215662Sralph static datum curkey; 1315662Sralph static struct hostent host; 1415662Sralph static char *host_aliases[MAXALIASES]; 15*15756Sralph extern int _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); 2715662Sralph key = dbmfetch(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*15756Sralph if (db == (DBM *)0 && (db = ndbmopen(HOSTDB, O_RDONLY)) == (DBM *)0) 5615662Sralph return ((struct hostent *)NULL); 5715662Sralph key.dptr = nam; 5815662Sralph key.dsize = strlen(nam); 5915662Sralph hp = fetchhost(key); 60*15756Sralph if (!_stayopen) { 61*15756Sralph ndbmclose(db); 62*15756Sralph db = (DBM *)NULL; 63*15756Sralph } 6415662Sralph return (hp); 6515662Sralph } 6615662Sralph 6715662Sralph struct hostent * 6815662Sralph gethostbyaddr(addr, length) 6915662Sralph char *addr; 7015662Sralph int length; 7115662Sralph { 7215662Sralph datum key; 7315662Sralph register struct hostent *hp; 7415662Sralph 75*15756Sralph if (db == (DBM *)0 && (db = ndbmopen(HOSTDB, O_RDONLY)) == (DBM *)0) 7615662Sralph return ((struct hostent *)NULL); 7715662Sralph key.dptr = addr; 7815662Sralph key.dsize = length; 7915662Sralph hp = fetchhost(key); 80*15756Sralph if (!_stayopen) { 81*15756Sralph ndbmclose(db); 82*15756Sralph db = (DBM *)NULL; 83*15756Sralph } 8415662Sralph return (hp); 8515662Sralph } 86