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