Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / hextoint.c
blob835e9d90c36bd3a8de38ea66f58172dd34e2686e
1 /* $NetBSD$ */
3 /*
4 * hextoint - convert an ascii string in hex to an unsigned
5 * long, with error checking
6 */
7 #include <ctype.h>
9 #include "ntp_stdlib.h"
11 int
12 hextoint(
13 const char *str,
14 u_long *ival
17 register u_long u;
18 register const char *cp;
20 cp = str;
22 if (*cp == '\0')
23 return 0;
25 u = 0;
26 while (*cp != '\0') {
27 if (!isxdigit((int)*cp))
28 return 0;
29 if (u >= 0x10000000)
30 return 0; /* overflow */
31 u <<= 4;
32 if (*cp <= '9') /* very ascii dependent */
33 u += *cp++ - '0';
34 else if (*cp >= 'a')
35 u += *cp++ - 'a' + 10;
36 else
37 u += *cp++ - 'A' + 10;
39 *ival = u;
40 return 1;