]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/uconvert.c
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / lib / sh / uconvert.c
1 /* uconvert - convert string representations of decimal numbers into whole
2 number/fractional value pairs. */
3
4 /* Copyright (C) 2008,2009,2020 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23
24 #include "bashtypes.h"
25
26 #include "posixtime.h"
27
28 #if defined (HAVE_UNISTD_H)
29 #include <unistd.h>
30 #endif
31
32 #include <stdio.h>
33 #include "chartypes.h"
34
35 #include "shell.h"
36 #include "builtins.h"
37
38 #define DECIMAL '.' /* XXX - should use locale */
39
40 #define RETURN(x) \
41 do { \
42 if (ip) *ip = ipart * mult; \
43 if (up) *up = upart; \
44 if (ep) *ep = p; \
45 return (x); \
46 } while (0)
47
48 /*
49 * An incredibly simplistic floating point converter.
50 */
51 static int multiplier[7] = { 1, 100000, 10000, 1000, 100, 10, 1 };
52
53 /* Take a decimal number int-part[.[micro-part]] and convert it to the whole
54 and fractional portions. The fractional portion is returned in
55 millionths (micro); callers are responsible for multiplying appropriately.
56 EP, if non-null, gets the address of the character where conversion stops.
57 Return 1 if value converted; 0 if invalid integer for either whole or
58 fractional parts. */
59 int
60 uconvert(s, ip, up, ep)
61 char *s;
62 long *ip, *up;
63 char **ep;
64 {
65 int n, mult;
66 long ipart, upart;
67 char *p;
68
69 ipart = upart = 0;
70 mult = 1;
71
72 if (s && (*s == '-' || *s == '+'))
73 {
74 mult = (*s == '-') ? -1 : 1;
75 p = s + 1;
76 }
77 else
78 p = s;
79
80 for ( ; p && *p; p++)
81 {
82 if (*p == DECIMAL) /* decimal point */
83 break;
84 if (DIGIT(*p) == 0)
85 RETURN(0);
86 ipart = (ipart * 10) + (*p - '0');
87 }
88
89 if (p == 0 || *p == 0) /* callers ensure p can never be 0; this is to shut up clang */
90 RETURN(1);
91
92 if (*p == DECIMAL)
93 p++;
94
95 /* Look for up to six digits past a decimal point. */
96 for (n = 0; n < 6 && p[n]; n++)
97 {
98 if (DIGIT(p[n]) == 0)
99 {
100 if (ep)
101 {
102 upart *= multiplier[n];
103 p += n; /* To set EP */
104 }
105 RETURN(0);
106 }
107 upart = (upart * 10) + (p[n] - '0');
108 }
109
110 /* Now convert to millionths */
111 upart *= multiplier[n];
112
113 if (n == 6 && p[6] >= '5' && p[6] <= '9')
114 upart++; /* round up 1 */
115
116 if (ep)
117 {
118 p += n;
119 while (DIGIT(*p))
120 p++;
121 }
122
123 RETURN(1);
124 }