Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / atolfp.c
blob668853c682e8b8337e7d8dd25c7b442973d624e1
1 /* $NetBSD$ */
3 /*
4 * atolfp - convert an ascii 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"
13 * Powers of 10
15 static u_long ten_to_the_n[10] = {
17 10,
18 100,
19 1000,
20 10000,
21 100000,
22 1000000,
23 10000000,
24 100000000,
25 1000000000,
29 int
30 atolfp(
31 const char *str,
32 l_fp *lfp
35 register const char *cp;
36 register u_long dec_i;
37 register u_long dec_f;
38 char *ind;
39 int ndec;
40 int isneg;
41 static const char *digits = "0123456789";
43 isneg = 0;
44 dec_i = dec_f = 0;
45 ndec = 0;
46 cp = str;
49 * We understand numbers of the form:
51 * [spaces][-|+][digits][.][digits][spaces|\n|\0]
53 while (isspace((int)*cp))
54 cp++;
56 if (*cp == '-') {
57 cp++;
58 isneg = 1;
61 if (*cp == '+')
62 cp++;
64 if (*cp != '.' && !isdigit((int)*cp))
65 return 0;
67 while (*cp != '\0' && (ind = strchr(digits, *cp)) != NULL) {
68 dec_i = (dec_i << 3) + (dec_i << 1); /* multiply by 10 */
69 dec_i += (ind - digits);
70 cp++;
73 if (*cp != '\0' && !isspace((int)*cp)) {
74 if (*cp++ != '.')
75 return 0;
77 while (ndec < 9 && *cp != '\0'
78 && (ind = strchr(digits, *cp)) != NULL) {
79 ndec++;
80 dec_f = (dec_f << 3) + (dec_f << 1); /* *10 */
81 dec_f += (ind - digits);
82 cp++;
85 while (isdigit((int)*cp))
86 cp++;
88 if (*cp != '\0' && !isspace((int)*cp))
89 return 0;
92 if (ndec > 0) {
93 register u_long tmp;
94 register u_long bit;
95 register u_long ten_fact;
97 ten_fact = ten_to_the_n[ndec];
99 tmp = 0;
100 bit = 0x80000000;
101 while (bit != 0) {
102 dec_f <<= 1;
103 if (dec_f >= ten_fact) {
104 tmp |= bit;
105 dec_f -= ten_fact;
107 bit >>= 1;
109 if ((dec_f << 1) > ten_fact)
110 tmp++;
111 dec_f = tmp;
114 if (isneg)
115 M_NEG(dec_i, dec_f);
117 lfp->l_ui = dec_i;
118 lfp->l_uf = dec_f;
119 return 1;