1 /* gethostent.c 4.10 85/01/21 */ 2 3 #include <stdio.h> 4 #include <sys/types.h> 5 #include <sys/socket.h> 6 #include <netdb.h> 7 #include <ctype.h> 8 #include <ndbm.h> 9 10 /* 11 * Internet version. 12 */ 13 #define MAXALIASES 35 14 #define MAXADDRSIZE 14 15 16 static FILE *hostf = NULL; 17 static char line[BUFSIZ+1]; 18 static char hostaddr[MAXADDRSIZE]; 19 static struct hostent host; 20 static char *host_aliases[MAXALIASES]; 21 22 /* 23 * The following is shared with gethostnamadr.c 24 */ 25 char *_host_file = "/etc/hosts"; 26 int _host_stayopen; 27 DBM *_host_db; /* set by gethostbyname(), gethostbyaddr() */ 28 29 static char *any(); 30 31 sethostent(f) 32 int f; 33 { 34 if (hostf != NULL) 35 rewind(hostf); 36 _host_stayopen |= f; 37 } 38 39 endhostent() 40 { 41 if (hostf) { 42 fclose(hostf); 43 hostf = NULL; 44 } 45 if (_host_db) { 46 dbm_close(_host_db); 47 _host_db = (DBM *)NULL; 48 } 49 _host_stayopen = 0; 50 } 51 52 struct hostent * 53 gethostent() 54 { 55 char *p; 56 register char *cp, **q; 57 58 if (hostf == NULL && (hostf = fopen(_host_file, "r" )) == NULL) 59 return (NULL); 60 again: 61 if ((p = fgets(line, BUFSIZ, hostf)) == NULL) 62 return (NULL); 63 if (*p == '#') 64 goto again; 65 cp = any(p, "#\n"); 66 if (cp == NULL) 67 goto again; 68 *cp = '\0'; 69 cp = any(p, " \t"); 70 if (cp == NULL) 71 goto again; 72 *cp++ = '\0'; 73 /* THIS STUFF IS INTERNET SPECIFIC */ 74 host.h_addr = hostaddr; 75 *((u_long *)host.h_addr) = inet_addr(p); 76 host.h_length = sizeof (u_long); 77 host.h_addrtype = AF_INET; 78 while (*cp == ' ' || *cp == '\t') 79 cp++; 80 host.h_name = cp; 81 q = host.h_aliases = host_aliases; 82 cp = any(cp, " \t"); 83 if (cp != NULL) 84 *cp++ = '\0'; 85 while (cp && *cp) { 86 if (*cp == ' ' || *cp == '\t') { 87 cp++; 88 continue; 89 } 90 if (q < &host_aliases[MAXALIASES - 1]) 91 *q++ = cp; 92 cp = any(cp, " \t"); 93 if (cp != NULL) 94 *cp++ = '\0'; 95 } 96 *q = NULL; 97 return (&host); 98 } 99 100 sethostfile(file) 101 char *file; 102 { 103 _host_file = file; 104 } 105 106 static char * 107 any(cp, match) 108 register char *cp; 109 char *match; 110 { 111 register char *mp, c; 112 113 while (c = *cp) { 114 for (mp = match; *mp; mp++) 115 if (*mp == c) 116 return (cp); 117 cp++; 118 } 119 return ((char *)0); 120 } 121