8030 libmvect: uninitialized variable
[unleashed.git] / usr / src / lib / libbc / libc / inet / inet_addr.c
blobac347b1965b9fbe23794053dd35363280b93ea4b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 1987 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
28 /* from UCB 4.5 82/11/14 */
30 #include <sys/types.h>
31 #include <ctype.h>
32 #include <netinet/in.h>
35 * Internet address interpretation routine.
36 * All the network library routines call this
37 * routine to interpret entries in the data bases
38 * which are expected to be an address.
39 * The value returned is in network order.
41 u_long
42 inet_addr(cp)
43 register char *cp;
45 register u_long val, base, n;
46 register char c;
47 u_long parts[4], *pp = parts;
49 again:
51 * Collect number up to ``.''.
52 * Values are specified as for C:
53 * 0x=hex, 0=octal, other=decimal.
55 val = 0; base = 10;
56 if (*cp == '0') {
57 if (*++cp == 'x' || *cp == 'X')
58 base = 16, cp++;
59 else
60 base = 8;
62 while (c = *cp) {
63 if (isdigit(c)) {
64 if ((c - '0') >= base)
65 break;
66 val = (val * base) + (c - '0');
67 cp++;
68 continue;
70 if (base == 16 && isxdigit(c)) {
71 val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
72 cp++;
73 continue;
75 break;
77 if (*cp == '.') {
79 * Internet format:
80 * a.b.c.d
81 * a.b.c (with c treated as 16-bits)
82 * a.b (with b treated as 24 bits)
84 if (pp >= parts + 4)
85 return (-1);
86 *pp++ = val, cp++;
87 goto again;
90 * Check for trailing characters.
92 if (*cp && !isspace(*cp))
93 return (-1);
94 *pp++ = val;
96 * Concoct the address according to
97 * the number of parts specified.
99 n = pp - parts;
100 switch (n) {
102 case 1: /* a -- 32 bits */
103 val = parts[0];
104 break;
106 case 2: /* a.b -- 8.24 bits */
107 val = (parts[0] << 24) | (parts[1] & 0xffffff);
108 break;
110 case 3: /* a.b.c -- 8.8.16 bits */
111 val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
112 (parts[2] & 0xffff);
113 break;
115 case 4: /* a.b.c.d -- 8.8.8.8 bits */
116 val = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
117 ((parts[2] & 0xff) << 8) | (parts[3] & 0xff);
118 break;
120 default:
121 return (-1);
123 val = htonl(val);
124 return (val);