fix file closes so multiple rcmd's work.
[unix-history] / usr / src / lib / libc / net / inet_network.c
CommitLineData
48a5c215 1/* @(#)inet_network.c 4.2 %G% */
a03c8308
SL
2
3#include <sys/types.h>
4#include <ctype.h>
5
6/*
7 * Internet network address interpretation routine.
8 * The library routines call this routine to interpret
9 * network numbers.
10 */
11u_long
12inet_network(cp)
13 register char *cp;
14{
15 register u_long val, base, n;
16 register char c;
17 u_long parts[4], *pp = parts;
48a5c215 18 register int i;
a03c8308
SL
19
20again:
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 if (pp >= parts + 4)
41 return (-1);
42 *pp++ = val, cp++;
43 goto again;
44 }
45 if (*cp && !isspace(*cp))
46 return (-1);
47 *pp++ = val;
48 n = pp - parts;
49 if (n > 4)
50 return (-1);
51 for (val = 0, i = 0; i < n; i++) {
52 val <<= 8;
53 val |= parts[i] & 0xff;
54 }
55 return (val);
56}