OpenCoverage

uconvert.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/lib/sh/uconvert.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* uconvert - convert string representations of decimal numbers into whole-
2 number/fractional value pairs. */-
3-
4/* Copyright (C) 2008,2009 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#if defined (TIME_WITH_SYS_TIME)-
27# include <sys/time.h>-
28# include <time.h>-
29#else-
30# if defined (HAVE_SYS_TIME_H)-
31# include <sys/time.h>-
32# else-
33# include <time.h>-
34# endif-
35#endif-
36-
37#if defined (HAVE_UNISTD_H)-
38#include <unistd.h>-
39#endif-
40-
41#include <stdio.h>-
42#include "chartypes.h"-
43-
44#include "shell.h"-
45#include "builtins.h"-
46-
47#define DECIMAL '.' /* XXX - should use locale */-
48-
49#define RETURN(x) \-
50do { \-
51 if (ip) *ip = ipart * mult; \-
52 if (up) *up = upart; \-
53 return (x); \-
54} while (0)-
55-
56/*-
57 * An incredibly simplistic floating point converter.-
58 */-
59static int multiplier[7] = { 1, 100000, 10000, 1000, 100, 10, 1 };-
60-
61/* Take a decimal number int-part[.[micro-part]] and convert it to the whole-
62 and fractional portions. The fractional portion is returned in-
63 millionths (micro); callers are responsible for multiplying appropriately.-
64 Return 1 if value converted; 0 if invalid integer for either whole or-
65 fractional parts. */-
66int-
67uconvert(s, ip, up)-
68 char *s;-
69 long *ip, *up;-
70{-
71 int n, mult;-
72 long ipart, upart;-
73 char *p;-
74-
75 ipart = upart = 0;-
76 mult = 1;-
77-
78 if (s && (*s == '-' || *s == '+'))
sDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
*s == '-'Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
*s == '+'Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
0-6
79 {-
80 mult = (*s == '-') ? -1 : 1;
(*s == '-')Description
TRUEnever evaluated
FALSEnever evaluated
0
81 p = s + 1;-
82 }
never executed: end of block
0
83 else-
84 p = s;
executed 6 times by 1 test: p = s;
Executed by:
  • Self test
6
85-
86 for ( ; p && *p; p++)
pDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
*pDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
0-11
87 {-
88 if (*p == DECIMAL) /* decimal point */
*p == '.'Description
TRUEnever evaluated
FALSEevaluated 6 times by 1 test
Evaluated by:
  • Self test
0-6
89 break;
never executed: break;
0
90 if (DIGIT(*p) == 0)
((*p) >= '0' &...) <= '9') == 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
(*p) >= '0'Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(*p) <= '9'Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
0-6
91 RETURN(0);
executed 1 time by 1 test: *ip = ipart * mult;
Executed by:
  • Self test
executed 1 time by 1 test: *up = upart;
Executed by:
  • Self test
executed 1 time by 1 test: return (0);
Executed by:
  • Self test
never executed: end of block
ipDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
upDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-1
92 ipart = (ipart * 10) + (*p - '0');-
93 }
executed 5 times by 1 test: end of block
Executed by:
  • Self test
5
94-
95 if (p == 0 || *p == 0) /* callers ensure p can never be 0; this is to shut up clang */
p == 0Description
TRUEnever evaluated
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
*p == 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-5
96 RETURN(1);
executed 5 times by 1 test: *ip = ipart * mult;
Executed by:
  • Self test
executed 5 times by 1 test: *up = upart;
Executed by:
  • Self test
executed 5 times by 1 test: return (1);
Executed by:
  • Self test
never executed: end of block
ipDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
upDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-5
97-
98 if (*p == DECIMAL)
*p == '.'Description
TRUEnever evaluated
FALSEnever evaluated
0
99 p++;
never executed: p++;
0
100-
101 /* Look for up to six digits past a decimal point. */-
102 for (n = 0; n < 6 && p[n]; n++)
n < 6Description
TRUEnever evaluated
FALSEnever evaluated
p[n]Description
TRUEnever evaluated
FALSEnever evaluated
0
103 {-
104 if (DIGIT(p[n]) == 0)
((p[n]) >= '0'...) <= '9') == 0Description
TRUEnever evaluated
FALSEnever evaluated
(p[n]) >= '0'Description
TRUEnever evaluated
FALSEnever evaluated
(p[n]) <= '9'Description
TRUEnever evaluated
FALSEnever evaluated
0
105 RETURN(0);
never executed: *ip = ipart * mult;
never executed: *up = upart;
never executed: return (0);
never executed: end of block
ipDescription
TRUEnever evaluated
FALSEnever evaluated
upDescription
TRUEnever evaluated
FALSEnever evaluated
0
106 upart = (upart * 10) + (p[n] - '0');-
107 }
never executed: end of block
0
108-
109 /* Now convert to millionths */-
110 upart *= multiplier[n];-
111-
112 if (n == 6 && p[6] >= '5' && p[6] <= '9')
n == 6Description
TRUEnever evaluated
FALSEnever evaluated
p[6] >= '5'Description
TRUEnever evaluated
FALSEnever evaluated
p[6] <= '9'Description
TRUEnever evaluated
FALSEnever evaluated
0
113 upart++; /* round up 1 */
never executed: upart++;
0
114-
115 RETURN(1);
never executed: *ip = ipart * mult;
never executed: *up = upart;
never executed: return (1);
ipDescription
TRUEnever evaluated
FALSEnever evaluated
upDescription
TRUEnever evaluated
FALSEnever evaluated
0
116}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2