1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate  * Copyright (c) 1996, by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate  * All rights reserved.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <ctype.h>
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * More generic than get_u_long. Supports byte, short, long, longlong.
35*0Sstevel@tonic-gate  * Returns 0 for success, -1 for failure.
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate int
get_number(char ** src,void * dest,int len)38*0Sstevel@tonic-gate get_number(char **src, void *dest, int len)
39*0Sstevel@tonic-gate {
40*0Sstevel@tonic-gate 	register unsigned	base;
41*0Sstevel@tonic-gate 	register char	c;
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 	if (len != 1 && (len % 2) != 0 || len > 8)
44*0Sstevel@tonic-gate 		return (-1);	/* not valid */
45*0Sstevel@tonic-gate 	/*
46*0Sstevel@tonic-gate 	 * Collect number up to first illegal character.  Values are specified
47*0Sstevel@tonic-gate 	 * as for C:  0x=hex, 0=octal, other=decimal.
48*0Sstevel@tonic-gate 	 */
49*0Sstevel@tonic-gate 	base = 10;
50*0Sstevel@tonic-gate 	if (**src == '0') {
51*0Sstevel@tonic-gate 		base = 8;
52*0Sstevel@tonic-gate 		(*src)++;
53*0Sstevel@tonic-gate 	}
54*0Sstevel@tonic-gate 	if (**src == 'x' || **src == 'X') {
55*0Sstevel@tonic-gate 		base = 16,
56*0Sstevel@tonic-gate 		(*src)++;
57*0Sstevel@tonic-gate 	}
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	while (c = **src) {
60*0Sstevel@tonic-gate 		if (isdigit(c)) {
61*0Sstevel@tonic-gate 			switch (len) {
62*0Sstevel@tonic-gate 			case 1:
63*0Sstevel@tonic-gate 				*(u_char *) dest =
64*0Sstevel@tonic-gate 				    (*(u_char *) dest) * base + (c - '0');
65*0Sstevel@tonic-gate 				break;
66*0Sstevel@tonic-gate 			case 2:
67*0Sstevel@tonic-gate 				*(u_short *) dest = (*(u_short *) dest) *
68*0Sstevel@tonic-gate 				    base + (c - '0');
69*0Sstevel@tonic-gate 				break;
70*0Sstevel@tonic-gate 			case 4:
71*0Sstevel@tonic-gate 				*(u_long *) dest = (*(u_long *) dest) *
72*0Sstevel@tonic-gate 				    base + (c - '0');
73*0Sstevel@tonic-gate 				break;
74*0Sstevel@tonic-gate 			case 8:
75*0Sstevel@tonic-gate 				*(u_longlong_t *) dest =
76*0Sstevel@tonic-gate 				    (*(u_longlong_t *) dest) * base +
77*0Sstevel@tonic-gate 				    (c - '0');
78*0Sstevel@tonic-gate 				break;
79*0Sstevel@tonic-gate 			}
80*0Sstevel@tonic-gate 			(*src)++;
81*0Sstevel@tonic-gate 			continue;
82*0Sstevel@tonic-gate 		}
83*0Sstevel@tonic-gate 		if (base == 16 && isxdigit(c)) {
84*0Sstevel@tonic-gate 			switch (len) {
85*0Sstevel@tonic-gate 			case 1:
86*0Sstevel@tonic-gate 				*(u_char *) dest =
87*0Sstevel@tonic-gate 				    ((*(u_char *) dest) << 4) + ((c & ~32) +
88*0Sstevel@tonic-gate 				    10 - 'A');
89*0Sstevel@tonic-gate 				break;
90*0Sstevel@tonic-gate 			case 2:
91*0Sstevel@tonic-gate 				*(u_short *) dest =
92*0Sstevel@tonic-gate 				    ((*(u_short *) dest) << 4) + ((c & ~32) +
93*0Sstevel@tonic-gate 				    10 - 'A');
94*0Sstevel@tonic-gate 				break;
95*0Sstevel@tonic-gate 			case 4:
96*0Sstevel@tonic-gate 				*(u_long *) dest =
97*0Sstevel@tonic-gate 				    ((*(u_long *) dest) << 4) + ((c & ~32) +
98*0Sstevel@tonic-gate 				    10 - 'A');
99*0Sstevel@tonic-gate 				break;
100*0Sstevel@tonic-gate 			case 8:
101*0Sstevel@tonic-gate 				*(u_longlong_t *) dest =
102*0Sstevel@tonic-gate 				    ((*(u_longlong_t *) dest) << 4) +
103*0Sstevel@tonic-gate 				    ((c & ~32) + 10 - 'A');
104*0Sstevel@tonic-gate 				break;
105*0Sstevel@tonic-gate 			}
106*0Sstevel@tonic-gate 		    (*src)++;
107*0Sstevel@tonic-gate 		    continue;
108*0Sstevel@tonic-gate 		}
109*0Sstevel@tonic-gate 		break;
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate 	return (0);
112*0Sstevel@tonic-gate }
main()113*0Sstevel@tonic-gate main()
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate 	char *src;
116*0Sstevel@tonic-gate 	u_char one;
117*0Sstevel@tonic-gate 	u_short two;
118*0Sstevel@tonic-gate 	u_long four;
119*0Sstevel@tonic-gate 	u_longlong_t eight;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	/*
122*0Sstevel@tonic-gate 	 * Try single octet (dec)
123*0Sstevel@tonic-gate 	 */
124*0Sstevel@tonic-gate 	src = "a56";
125*0Sstevel@tonic-gate 	one = 0;
126*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &one, 1) != 0)
127*0Sstevel@tonic-gate 		printf("byte failed.\n");
128*0Sstevel@tonic-gate 	else
129*0Sstevel@tonic-gate 		printf("byte: %d\n", one);
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	src = "65535";
132*0Sstevel@tonic-gate 	two = 0;
133*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &two, 2) != 0)
134*0Sstevel@tonic-gate 		printf("short failed.\n");
135*0Sstevel@tonic-gate 	else
136*0Sstevel@tonic-gate 		printf("short: %d\n", two);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	src = "4294967296";
139*0Sstevel@tonic-gate 	four = 0;
140*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &four, 4) != 0)
141*0Sstevel@tonic-gate 		printf("long failed.\n");
142*0Sstevel@tonic-gate 	else
143*0Sstevel@tonic-gate 		printf("long: %d\n", four);
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	src = "4289672944289672944";
146*0Sstevel@tonic-gate 	eight = 0;
147*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &eight, 8) != 0)
148*0Sstevel@tonic-gate 		printf("longlong failed.\n");
149*0Sstevel@tonic-gate 	else
150*0Sstevel@tonic-gate 		printf("longlong: %d\n", eight);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/*
155*0Sstevel@tonic-gate 	 * Try single octet (hex)
156*0Sstevel@tonic-gate 	 */
157*0Sstevel@tonic-gate 	src = "0xff";
158*0Sstevel@tonic-gate 	one = 0;
159*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &one, 1) != 0)
160*0Sstevel@tonic-gate 		printf("byte failed.\n");
161*0Sstevel@tonic-gate 	else
162*0Sstevel@tonic-gate 		printf("byte: 0x%x\n", one);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	src = "0xffff";
165*0Sstevel@tonic-gate 	two = 0;
166*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &two, 2) != 0)
167*0Sstevel@tonic-gate 		printf("short failed.\n");
168*0Sstevel@tonic-gate 	else
169*0Sstevel@tonic-gate 		printf("short: 0x%x\n", two);
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	src = "0xffffffff";
172*0Sstevel@tonic-gate 	four = 0;
173*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &four, 4) != 0)
174*0Sstevel@tonic-gate 		printf("long failed.\n");
175*0Sstevel@tonic-gate 	else
176*0Sstevel@tonic-gate 		printf("long: 0x%x\n", four);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	src = "0xffffffffffffffff";
179*0Sstevel@tonic-gate 	eight = 0;
180*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &eight, 8) != 0)
181*0Sstevel@tonic-gate 		printf("longlong failed.\n");
182*0Sstevel@tonic-gate 	else
183*0Sstevel@tonic-gate 		printf("longlong: 0x%x\n", eight);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	/*
186*0Sstevel@tonic-gate 	 * Try single octet (Oct)
187*0Sstevel@tonic-gate 	 */
188*0Sstevel@tonic-gate 	src = "0376";
189*0Sstevel@tonic-gate 	one = 0;
190*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &one, 1) != 0)
191*0Sstevel@tonic-gate 		printf("byte failed.\n");
192*0Sstevel@tonic-gate 	else
193*0Sstevel@tonic-gate 		printf("byte: 0x%x\n", one);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	src = "0177776";
196*0Sstevel@tonic-gate 	two = 0;
197*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &two, 2) != 0)
198*0Sstevel@tonic-gate 		printf("short failed.\n");
199*0Sstevel@tonic-gate 	else
200*0Sstevel@tonic-gate 		printf("short: 0x%x\n", two);
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	src = "037777777776";
203*0Sstevel@tonic-gate 	four = 0;
204*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &four, 4) != 0)
205*0Sstevel@tonic-gate 		printf("long failed.\n");
206*0Sstevel@tonic-gate 	else
207*0Sstevel@tonic-gate 		printf("long: 0x%x\n", four);
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	src = "01777777777777777777776";
210*0Sstevel@tonic-gate 	eight = 0;
211*0Sstevel@tonic-gate 	if (get_number(&src, (void *) &eight, 8) != 0)
212*0Sstevel@tonic-gate 		printf("longlong failed.\n");
213*0Sstevel@tonic-gate 	else
214*0Sstevel@tonic-gate 		printf("longlong: 0x%x\n", eight);
215*0Sstevel@tonic-gate 	return (0);
216*0Sstevel@tonic-gate }
217