comparison target-utils/libcommon/hexarg.c @ 13:f4fc449a64ea

target-utils libcommon infrastructure for interactive commands
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 03 May 2013 06:42:03 +0000
parents
children
comparison
equal deleted inserted replaced
12:165040ce4929 13:f4fc449a64ea
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 }