add Berkeley specific headers
[unix-history] / usr / src / lib / libc / net / inet_network.c
CommitLineData
bb0cfa24
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
140b4f5f
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
bb0cfa24
DF
11 */
12
2ce81398 13#if defined(LIBC_SCCS) && !defined(lint)
140b4f5f
KB
14static char sccsid[] = "@(#)inet_network.c 5.4 (Berkeley) %G%";
15#endif /* LIBC_SCCS and not lint */
a03c8308
SL
16
17#include <sys/types.h>
a02e9915 18#include <netinet/in.h>
a03c8308
SL
19#include <ctype.h>
20
21/*
22 * Internet network address interpretation routine.
23 * The library routines call this routine to interpret
24 * network numbers.
25 */
26u_long
27inet_network(cp)
28 register char *cp;
29{
30 register u_long val, base, n;
31 register char c;
32 u_long parts[4], *pp = parts;
48a5c215 33 register int i;
a03c8308
SL
34
35again:
36 val = 0; base = 10;
37 if (*cp == '0')
38 base = 8, cp++;
39 if (*cp == 'x' || *cp == 'X')
40 base = 16, cp++;
41 while (c = *cp) {
42 if (isdigit(c)) {
43 val = (val * base) + (c - '0');
44 cp++;
45 continue;
46 }
47 if (base == 16 && isxdigit(c)) {
48 val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
49 cp++;
50 continue;
51 }
52 break;
53 }
54 if (*cp == '.') {
55 if (pp >= parts + 4)
a02e9915 56 return (INADDR_NONE);
a03c8308
SL
57 *pp++ = val, cp++;
58 goto again;
59 }
60 if (*cp && !isspace(*cp))
a02e9915 61 return (INADDR_NONE);
a03c8308
SL
62 *pp++ = val;
63 n = pp - parts;
64 if (n > 4)
a02e9915 65 return (INADDR_NONE);
a03c8308
SL
66 for (val = 0, i = 0; i < n; i++) {
67 val <<= 8;
68 val |= parts[i] & 0xff;
69 }
70 return (val);
71}