/* inet_addr.c 4.2 82/10/05 */ #include #include /* * Internet address interpretation routine. * All the network library routines call this * routine to interpret entries in the data bases * which are expected to be an address. */ u_long inet_addr(cp) register char *cp; { register unsigned long val, base, n; register char c; unsigned long parts[4], *pp = parts; again: val = 0; base = 10; if (*cp == '0') base = 8, cp++; if (*cp == 'x' || *cp == 'X') base = 16, cp++; while (c = *cp) { if (isdigit(c)) { val = (val * base) + (c - '0'); cp++; continue; } if (base == 16 && isxdigit(c)) { val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); cp++; continue; } break; } if (*cp == '.') { /* * Internet format: * a.b.c.d * a.b.c (with c treated as 16-bits) * a.b (with b treated as 24 bits) */ if (pp >= parts + 4) return (-1); *pp++ = val, cp++; goto again; } if (*cp && !isspace(*cp)) return (-1); n = pp - parts; if (n > 0) { if (n > 4) return (-1); *pp++ = val; n++; val = parts[0]; if (n > 1) val <<= 24; if (n > 2) val |= (parts[1] & 0xff) << 16; if (n > 3) val |= (parts[2] & 0xff) << 8; if (n > 1) val |= parts[n - 1]; #if vax || pdp11 val = htonl(val); #endif } return (val); }