1 /* gethostent.c 4.1 82/08/25 */ 2 3 #include <stdio.h> 4 #include <sys/socket.h> 5 #include <netdb.h> 6 #include <ctype.h> 7 8 #define MAXALIASES 35 9 10 static char HOSTDB[] = "/usr/lib/hosts"; 11 static FILE *hostf = NULL; 12 static char line[BUFSIZ+1]; 13 static struct hostent host; 14 static char *host_aliases[MAXALIASES]; 15 static int stayopen = 0; 16 static unsigned long value(); 17 static char *any(); 18 19 sethostent(f) 20 int f; 21 { 22 if (hostf == NULL) 23 hostf = fopen(HOSTDB, "r" ); 24 else 25 rewind(hostf); 26 stayopen |= f; 27 } 28 29 endhostent() 30 { 31 if (hostf && !stayopen) { 32 fclose(hostf); 33 hostf = NULL; 34 } 35 } 36 37 struct hostent * 38 gethostent() 39 { 40 char *p; 41 register char *cp, **q; 42 43 if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL) 44 return (NULL); 45 again: 46 if ((p = fgets(line, BUFSIZ, hostf)) == NULL) 47 return (NULL); 48 if (*p == '#') 49 goto again; 50 cp = any(p, "#\n"); 51 if (cp == NULL) 52 goto again; 53 *cp = '\0'; 54 cp = any(p, " \t"); 55 if (cp == NULL) 56 goto again; 57 *cp++ = '\0'; 58 host.h_addr = value(p); 59 while (*cp == ' ' || *cp == '\t') 60 cp++; 61 host.h_name = cp; 62 host.h_addrtype = AF_INET; 63 host.h_aliases = host_aliases; 64 cp = any(cp, " \t"); 65 if (cp != NULL) 66 *cp++ = '\0'; 67 q = host_aliases; 68 while (*cp) { 69 if (*cp == ' ' || *cp == '\t') { 70 cp++; 71 continue; 72 } 73 if (q < &host_aliases[MAXALIASES - 1]) 74 *q++ = cp; 75 cp = any(cp, " \t"); 76 if (*cp != NULL) 77 *cp++ = '\0'; 78 } 79 *q = NULL; 80 return (&host); 81 } 82 83 static unsigned long 84 value(cp) 85 register char *cp; 86 { 87 register unsigned long val, base, n; 88 register char c; 89 unsigned long parts[4], *pp = parts; 90 91 again: 92 val = 0; base = 10; 93 if (*cp == '0') 94 base = 8, cp++; 95 if (*cp == 'x' || *cp == 'X') 96 base = 16, cp++; 97 while (c = *cp) { 98 if (isdigit(c)) { 99 val = (val * base) + (c - '0'); 100 cp++; 101 continue; 102 } 103 if (base == 16 && isxdigit(c)) { 104 val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); 105 cp++; 106 continue; 107 } 108 break; 109 } 110 if (*cp == '.') { 111 /* 112 * Internet format: 113 * a.b.c.d 114 * a.b.c (with c treated as 16-bits) 115 * a.b (with b treated as 24 bits) 116 */ 117 if (pp >= parts + 4) 118 return (-1); 119 *pp++ = val, cp++; 120 goto again; 121 } 122 if (*cp && !isspace(*cp)) 123 return (-1); 124 n = pp - parts; 125 if (n > 0) { 126 if (n > 4) 127 return (-1); 128 *pp++ = val; n++; 129 val = parts[0]; 130 if (n > 1) 131 val <<= 24; 132 if (n > 2) 133 val |= (parts[1] & 0xff) << 16; 134 if (n > 3) 135 val |= (parts[2] & 0xff) << 8; 136 if (n > 1) 137 val |= parts[n - 1]; 138 #if vax || pdp11 139 val = htonl(val); 140 #endif 141 } 142 return (val); 143 } 144 145 static char * 146 any(cp, match) 147 register char *cp; 148 char *match; 149 { 150 register char *mp, c; 151 152 while (c = *cp) { 153 for (mp = match; *mp; mp++) 154 if (*mp == c) 155 return (cp); 156 cp++; 157 } 158 return ((char *)0); 159 } 160