Commit | Line | Data |
---|---|---|
bb0cfa24 DF |
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[] = "@(#)inet_network.c 5.1 (Berkeley) %G%"; | |
9 | #endif not lint | |
a03c8308 SL |
10 | |
11 | #include <sys/types.h> | |
12 | #include <ctype.h> | |
13 | ||
14 | /* | |
15 | * Internet network address interpretation routine. | |
16 | * The library routines call this routine to interpret | |
17 | * network numbers. | |
18 | */ | |
19 | u_long | |
20 | inet_network(cp) | |
21 | register char *cp; | |
22 | { | |
23 | register u_long val, base, n; | |
24 | register char c; | |
25 | u_long parts[4], *pp = parts; | |
48a5c215 | 26 | register int i; |
a03c8308 SL |
27 | |
28 | again: | |
29 | val = 0; base = 10; | |
30 | if (*cp == '0') | |
31 | base = 8, cp++; | |
32 | if (*cp == 'x' || *cp == 'X') | |
33 | base = 16, cp++; | |
34 | while (c = *cp) { | |
35 | if (isdigit(c)) { | |
36 | val = (val * base) + (c - '0'); | |
37 | cp++; | |
38 | continue; | |
39 | } | |
40 | if (base == 16 && isxdigit(c)) { | |
41 | val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); | |
42 | cp++; | |
43 | continue; | |
44 | } | |
45 | break; | |
46 | } | |
47 | if (*cp == '.') { | |
48 | if (pp >= parts + 4) | |
49 | return (-1); | |
50 | *pp++ = val, cp++; | |
51 | goto again; | |
52 | } | |
53 | if (*cp && !isspace(*cp)) | |
54 | return (-1); | |
55 | *pp++ = val; | |
56 | n = pp - parts; | |
57 | if (n > 4) | |
58 | return (-1); | |
59 | for (val = 0, i = 0; i < n; i++) { | |
60 | val <<= 8; | |
61 | val |= parts[i] & 0xff; | |
62 | } | |
63 | return (val); | |
64 | } |