Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / hextolfp.c
bloba5c2fcbf0ff3048a6b0553ba4f33d628f5c0d20d
1 /* $NetBSD$ */
3 /*
4 * hextolfp - convert an ascii hex string to an l_fp number
5 */
6 #include <stdio.h>
7 #include <ctype.h>
9 #include "ntp_fp.h"
10 #include "ntp_string.h"
11 #include "ntp_stdlib.h"
13 int
14 hextolfp(
15 const char *str,
16 l_fp *lfp
19 register const char *cp;
20 register const char *cpstart;
21 register u_long dec_i;
22 register u_long dec_f;
23 char *ind = NULL;
24 static const char *digits = "0123456789abcdefABCDEF";
26 dec_i = dec_f = 0;
27 cp = str;
30 * We understand numbers of the form:
32 * [spaces]8_hex_digits[.]8_hex_digits[spaces|\n|\0]
34 while (isspace((int)*cp))
35 cp++;
37 cpstart = cp;
38 while (*cp != '\0' && (cp - cpstart) < 8 &&
39 (ind = strchr(digits, *cp)) != NULL) {
40 dec_i = dec_i << 4; /* multiply by 16 */
41 dec_i += ((ind - digits) > 15) ? (ind - digits) - 6
42 : (ind - digits);
43 cp++;
46 if ((cp - cpstart) < 8 || ind == NULL)
47 return 0;
48 if (*cp == '.')
49 cp++;
51 cpstart = cp;
52 while (*cp != '\0' && (cp - cpstart) < 8 &&
53 (ind = strchr(digits, *cp)) != NULL) {
54 dec_f = dec_f << 4; /* multiply by 16 */
55 dec_f += ((ind - digits) > 15) ? (ind - digits) - 6
56 : (ind - digits);
57 cp++;
60 if ((cp - cpstart) < 8 || ind == NULL)
61 return 0;
63 if (*cp != '\0' && !isspace((int)*cp))
64 return 0;
66 lfp->l_ui = dec_i;
67 lfp->l_uf = dec_f;
68 return 1;