remove manual subsections
[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
50c7758a
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
bb0cfa24
DF
16 */
17
2ce81398 18#if defined(LIBC_SCCS) && !defined(lint)
50c7758a 19static char sccsid[] = "@(#)inet_network.c 5.5 (Berkeley) %G%";
140b4f5f 20#endif /* LIBC_SCCS and not lint */
a03c8308
SL
21
22#include <sys/types.h>
a02e9915 23#include <netinet/in.h>
a03c8308
SL
24#include <ctype.h>
25
26/*
27 * Internet network address interpretation routine.
28 * The library routines call this routine to interpret
29 * network numbers.
30 */
31u_long
32inet_network(cp)
33 register char *cp;
34{
35 register u_long val, base, n;
36 register char c;
37 u_long parts[4], *pp = parts;
48a5c215 38 register int i;
a03c8308
SL
39
40again:
41 val = 0; base = 10;
42 if (*cp == '0')
43 base = 8, cp++;
44 if (*cp == 'x' || *cp == 'X')
45 base = 16, cp++;
46 while (c = *cp) {
47 if (isdigit(c)) {
48 val = (val * base) + (c - '0');
49 cp++;
50 continue;
51 }
52 if (base == 16 && isxdigit(c)) {
53 val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
54 cp++;
55 continue;
56 }
57 break;
58 }
59 if (*cp == '.') {
60 if (pp >= parts + 4)
a02e9915 61 return (INADDR_NONE);
a03c8308
SL
62 *pp++ = val, cp++;
63 goto again;
64 }
65 if (*cp && !isspace(*cp))
a02e9915 66 return (INADDR_NONE);
a03c8308
SL
67 *pp++ = val;
68 n = pp - parts;
69 if (n > 4)
a02e9915 70 return (INADDR_NONE);
a03c8308
SL
71 for (val = 0, i = 0; i < n; i++) {
72 val <<= 8;
73 val |= parts[i] & 0xff;
74 }
75 return (val);
76}