1 /* inet_addr.c 4.2 82/10/05 */ 2 3 #include <sys/types.h> 4 #include <ctype.h> 5 6 /* 7 * Internet address interpretation routine. 8 * All the network library routines call this 9 * routine to interpret entries in the data bases 10 * which are expected to be an address. 11 */ 12 u_long 13 inet_addr(cp) 14 register char *cp; 15 { 16 register unsigned long val, base, n; 17 register char c; 18 unsigned long parts[4], *pp = parts; 19 20 again: 21 val = 0; base = 10; 22 if (*cp == '0') 23 base = 8, cp++; 24 if (*cp == 'x' || *cp == 'X') 25 base = 16, cp++; 26 while (c = *cp) { 27 if (isdigit(c)) { 28 val = (val * base) + (c - '0'); 29 cp++; 30 continue; 31 } 32 if (base == 16 && isxdigit(c)) { 33 val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); 34 cp++; 35 continue; 36 } 37 break; 38 } 39 if (*cp == '.') { 40 /* 41 * Internet format: 42 * a.b.c.d 43 * a.b.c (with c treated as 16-bits) 44 * a.b (with b treated as 24 bits) 45 */ 46 if (pp >= parts + 4) 47 return (-1); 48 *pp++ = val, cp++; 49 goto again; 50 } 51 if (*cp && !isspace(*cp)) 52 return (-1); 53 n = pp - parts; 54 if (n > 0) { 55 if (n > 4) 56 return (-1); 57 *pp++ = val; n++; 58 val = parts[0]; 59 if (n > 1) 60 val <<= 24; 61 if (n > 2) 62 val |= (parts[1] & 0xff) << 16; 63 if (n > 3) 64 val |= (parts[2] & 0xff) << 8; 65 if (n > 1) 66 val |= parts[n - 1]; 67 #if vax || pdp11 68 val = htonl(val); 69 #endif 70 } 71 return (val); 72 } 73 74