Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / mstolfp.c
blob43d6c259197555f2baf8eb91c1c891e84e89b051
1 /* $NetBSD$ */
3 /*
4 * mstolfp - convert an ascii string in milliseconds to an l_fp number
5 */
6 #include <stdio.h>
7 #include <ctype.h>
9 #include "ntp_fp.h"
10 #include "ntp_stdlib.h"
12 int
13 mstolfp(
14 const char *str,
15 l_fp *lfp
18 register const char *cp;
19 register char *bp;
20 register const char *cpdec;
21 char buf[100];
24 * We understand numbers of the form:
26 * [spaces][-][digits][.][digits][spaces|\n|\0]
28 * This is one enormous hack. Since I didn't feel like
29 * rewriting the decoding routine for milliseconds, what
30 * is essentially done here is to make a copy of the string
31 * with the decimal moved over three places so the seconds
32 * decoding routine can be used.
34 bp = buf;
35 cp = str;
36 while (isspace((int)*cp))
37 cp++;
39 if (*cp == '-') {
40 *bp++ = '-';
41 cp++;
44 if (*cp != '.' && !isdigit((int)*cp))
45 return 0;
49 * Search forward for the decimal point or the end of the string.
51 cpdec = cp;
52 while (isdigit((int)*cpdec))
53 cpdec++;
56 * Found something. If we have more than three digits copy the
57 * excess over, else insert a leading 0.
59 if ((cpdec - cp) > 3) {
60 do {
61 *bp++ = (char)*cp++;
62 } while ((cpdec - cp) > 3);
63 } else {
64 *bp++ = '0';
68 * Stick the decimal in. If we've got less than three digits in
69 * front of the millisecond decimal we insert the appropriate number
70 * of zeros.
72 *bp++ = '.';
73 if ((cpdec - cp) < 3) {
74 register int i = 3 - (cpdec - cp);
76 do {
77 *bp++ = '0';
78 } while (--i > 0);
82 * Copy the remainder up to the millisecond decimal. If cpdec
83 * is pointing at a decimal point, copy in the trailing number too.
85 while (cp < cpdec)
86 *bp++ = (char)*cp++;
88 if (*cp == '.') {
89 cp++;
90 while (isdigit((int)*cp))
91 *bp++ = (char)*cp++;
93 *bp = '\0';
96 * Check to make sure the string is properly terminated. If
97 * so, give the buffer to the decoding routine.
99 if (*cp != '\0' && !isspace((int)*cp))
100 return 0;
101 return atolfp(buf, lfp);