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.1 (Berkeley) 05/30/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 30 /* 31 * The following is shared with gethostnamadr.c 32 */ 33 char *_host_file = "/etc/hosts"; 34 int _host_stayopen; 35 DBM *_host_db; /* set by gethostbyname(), gethostbyaddr() */ 36 37 static char *any(); 38 39 sethostent(f) 40 int f; 41 { 42 if (hostf != NULL) 43 rewind(hostf); 44 _host_stayopen |= f; 45 } 46 47 endhostent() 48 { 49 if (hostf) { 50 fclose(hostf); 51 hostf = NULL; 52 } 53 if (_host_db) { 54 dbm_close(_host_db); 55 _host_db = (DBM *)NULL; 56 } 57 _host_stayopen = 0; 58 } 59 60 struct hostent * 61 gethostent() 62 { 63 char *p; 64 register char *cp, **q; 65 66 if (hostf == NULL && (hostf = fopen(_host_file, "r" )) == NULL) 67 return (NULL); 68 again: 69 if ((p = fgets(line, BUFSIZ, hostf)) == NULL) 70 return (NULL); 71 if (*p == '#') 72 goto again; 73 cp = any(p, "#\n"); 74 if (cp == NULL) 75 goto again; 76 *cp = '\0'; 77 cp = any(p, " \t"); 78 if (cp == NULL) 79 goto again; 80 *cp++ = '\0'; 81 /* THIS STUFF IS INTERNET SPECIFIC */ 82 host.h_addr = hostaddr; 83 *((u_long *)host.h_addr) = inet_addr(p); 84 host.h_length = sizeof (u_long); 85 host.h_addrtype = AF_INET; 86 while (*cp == ' ' || *cp == '\t') 87 cp++; 88 host.h_name = cp; 89 q = host.h_aliases = host_aliases; 90 cp = any(cp, " \t"); 91 if (cp != NULL) 92 *cp++ = '\0'; 93 while (cp && *cp) { 94 if (*cp == ' ' || *cp == '\t') { 95 cp++; 96 continue; 97 } 98 if (q < &host_aliases[MAXALIASES - 1]) 99 *q++ = cp; 100 cp = any(cp, " \t"); 101 if (cp != NULL) 102 *cp++ = '\0'; 103 } 104 *q = NULL; 105 return (&host); 106 } 107 108 sethostfile(file) 109 char *file; 110 { 111 _host_file = file; 112 } 113 114 static char * 115 any(cp, match) 116 register char *cp; 117 char *match; 118 { 119 register char *mp, c; 120 121 while (c = *cp) { 122 for (mp = match; *mp; mp++) 123 if (*mp == c) 124 return (cp); 125 cp++; 126 } 127 return ((char *)0); 128 } 129