comparison target-utils/libcommon/hexarg.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * Many commands take hex arguments. This module contains the parse_hexarg()
3 * function, which is a wrapper around strtoul that performs some additional
4 * checks.
5 */
6
7 #include <sys/types.h>
8 #include <ctype.h>
9 #include <stdlib.h>
10
11 parse_hexarg(arg, maxdigits, valp)
12 char *arg;
13 int maxdigits;
14 u_long *valp;
15 {
16 char *cp = arg, *bp;
17 int len;
18
19 if (cp[0] == '0' && (cp[1] == 'x' || cp[1] == 'X'))
20 cp += 2;
21 for (bp = cp; *cp; cp++)
22 if (!isxdigit(*cp))
23 return(-1);
24 len = cp - bp;
25 if (len < 1 || len > maxdigits)
26 return(-1);
27 *valp = strtoul(arg, 0, 16);
28 return(0);
29 }