1 /* gethostnamadr.c 4.5 84/08/28 */ 2 3 #include <stdio.h> 4 #include <netdb.h> 5 #include <sys/file.h> 6 #include <ndbm.h> 7 8 #define MAXALIASES 35 9 10 static char HOSTDB[] = "/etc/hosts"; 11 DBM *_host_db = (DBM *)NULL; 12 static struct hostent host; 13 static char *host_aliases[MAXALIASES]; 14 static char hostbuf[BUFSIZ+1]; 15 int _host_stayopen; /* set by sethostent(), cleared by endhostent() */ 16 17 static struct hostent * 18 fetchhost(key) 19 datum key; 20 { 21 register char *cp, *tp, **ap; 22 register int naliases; 23 24 if (key.dptr == 0) 25 return ((struct hostent *)NULL); 26 key = dbm_fetch(_host_db, key); 27 if (key.dptr == 0) 28 return ((struct hostent *)NULL); 29 cp = key.dptr; 30 tp = hostbuf; 31 host.h_name = tp; 32 while (*tp++ = *cp++) 33 ; 34 naliases = *(int *)cp; cp += sizeof (int); 35 for (ap = host_aliases; naliases > 0; naliases--) { 36 *ap++ = tp; 37 while (*tp++ = *cp++) 38 ; 39 } 40 *ap = (char *)NULL; 41 host.h_aliases = host_aliases; 42 host.h_addrtype = *(int *)cp; cp += sizeof (int); 43 host.h_length = *(int *)cp; cp += sizeof (int); 44 host.h_addr = tp; 45 bcopy(cp, tp, host.h_length); 46 return (&host); 47 } 48 49 struct hostent * 50 gethostbyname(nam) 51 char *nam; 52 { 53 datum key; 54 register struct hostent *hp; 55 56 if ((_host_db == (DBM *)NULL) 57 && ((_host_db = dbm_open(HOSTDB, O_RDONLY)) == (DBM *)NULL)) 58 return ((struct hostent *)NULL); 59 key.dptr = nam; 60 key.dsize = strlen(nam); 61 hp = fetchhost(key); 62 if (!_host_stayopen) { 63 dbm_close(_host_db); 64 _host_db = (DBM *)NULL; 65 } 66 return (hp); 67 } 68 69 struct hostent * 70 gethostbyaddr(addr, length) 71 char *addr; 72 int length; 73 { 74 datum key; 75 register struct hostent *hp; 76 77 if ((_host_db == (DBM *)NULL) 78 && ((_host_db = dbm_open(HOSTDB, O_RDONLY)) == (DBM *)NULL)) 79 return ((struct hostent *)NULL); 80 key.dptr = addr; 81 key.dsize = length; 82 hp = fetchhost(key); 83 if (!_host_stayopen) { 84 dbm_close(_host_db); 85 _host_db = (DBM *)NULL; 86 } 87 return (hp); 88 } 89