Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | typedef enum { | - |
9 | NONE = 0, KILO = 1, MEGA = 2, GIGA = 3, TERA = 4, PETA = 5, EXA = 6 | - |
10 | } unit_type; | - |
11 | | - |
12 | | - |
13 | static unit_type units[] = { NONE, KILO, MEGA, GIGA, TERA, PETA, EXA }; | - |
14 | static char scale_chars[] = "BKMGTPE"; | - |
15 | static long long scale_factors[] = { | - |
16 | 1LL, | - |
17 | 1024LL, | - |
18 | 1024LL*1024, | - |
19 | 1024LL*1024*1024, | - |
20 | 1024LL*1024*1024*1024, | - |
21 | 1024LL*1024*1024*1024*1024, | - |
22 | 1024LL*1024*1024*1024*1024*1024, | - |
23 | }; | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | int | - |
32 | scan_scaled(char *scaled, long long *result) | - |
33 | { | - |
34 | char *p = scaled; | - |
35 | int sign = 0; | - |
36 | unsigned int i, ndigits = 0, fract_digits = 0; | - |
37 | long long scale_fact = 1, whole = 0, fpart = 0; | - |
38 | | - |
39 | | - |
40 | while ( | - |
41 | (((TRUE | never evaluated | FALSE | never evaluated |
| 0 |
42 | (unsigned char)*pTRUE | never evaluated | FALSE | never evaluated |
| 0 |
43 | ) & ~0x7f) == 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
44 | && | - |
45 | ((*TRUE | never evaluated | FALSE | never evaluated |
__ctype_b_loc ())[(int) ((TRUE | never evaluated | FALSE | never evaluated |
| 0 |
46 | (unsigned char)*pTRUE | never evaluated | FALSE | never evaluated |
| 0 |
47 | ))] & (unsigned short int) _ISspace)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
48 | ) | - |
49 | ++ never executed: ++p; p;never executed: ++p; | 0 |
50 | | - |
51 | | - |
52 | while (*TRUE | never evaluated | FALSE | never evaluated |
p == '-'TRUE | never evaluated | FALSE | never evaluated |
|| *TRUE | never evaluated | FALSE | never evaluated |
p == '+'TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
53 | if (*TRUE | never evaluated | FALSE | never evaluated |
p == '-'TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
54 | if (signTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
55 | | - |
56 | (*__errno_location ()) | - |
57 | = | - |
58 | 22 | - |
59 | ; | - |
60 | return never executed: return -1; -1;never executed: return -1; | 0 |
61 | } | - |
62 | sign = -1; | - |
63 | ++p; | - |
64 | } never executed: end of block else if (*TRUE | never evaluated | FALSE | never evaluated |
p == '+'TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
65 | if (signTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
66 | | - |
67 | (*__errno_location ()) | - |
68 | = | - |
69 | 22 | - |
70 | ; | - |
71 | return never executed: return -1; -1;never executed: return -1; | 0 |
72 | } | - |
73 | sign = +1; | - |
74 | ++p; | - |
75 | } never executed: end of block | 0 |
76 | } never executed: end of block | 0 |
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | for (; | - |
84 | (((TRUE | never evaluated | FALSE | never evaluated |
| 0 |
85 | (unsigned char)*pTRUE | never evaluated | FALSE | never evaluated |
| 0 |
86 | ) & ~0x7f) == 0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
87 | && | - |
88 | ( | - |
89 | ((*TRUE | never evaluated | FALSE | never evaluated |
__ctype_b_loc ())[(int) ((TRUE | never evaluated | FALSE | never evaluated |
| 0 |
90 | (unsigned char)*pTRUE | never evaluated | FALSE | never evaluated |
| 0 |
91 | ))] & (unsigned short int) _ISdigit)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
92 | || *TRUE | never evaluated | FALSE | never evaluated |
p=='.'TRUE | never evaluated | FALSE | never evaluated |
); ++p) { | 0 |
93 | if (*TRUE | never evaluated | FALSE | never evaluated |
p == '.'TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
94 | if (fract_digits > 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
95 | | - |
96 | (*__errno_location ()) | - |
97 | = | - |
98 | 22 | - |
99 | ; | - |
100 | return never executed: return -1; -1;never executed: return -1; | 0 |
101 | } | - |
102 | fract_digits = 1; | - |
103 | continue; never executed: continue; | 0 |
104 | } | - |
105 | | - |
106 | i = (*p) - '0'; | - |
107 | if (fract_digits > 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
108 | if (fract_digits >= ((sizeof(units)/sizeof(units[0])) * 3)-1TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
109 | | - |
110 | continue; never executed: continue; | 0 |
111 | fract_digits++; | - |
112 | if (fpart > 0x7fffffffffffffffLL / 10TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
113 | | - |
114 | (*__errno_location ()) | - |
115 | = | - |
116 | 34 | - |
117 | ; | - |
118 | return never executed: return -1; -1;never executed: return -1; | 0 |
119 | } | - |
120 | fpart *= 10; | - |
121 | if (i > 0x7fffffffffffffffLL - fpartTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
122 | | - |
123 | (*__errno_location ()) | - |
124 | = | - |
125 | 34 | - |
126 | ; | - |
127 | return never executed: return -1; -1;never executed: return -1; | 0 |
128 | } | - |
129 | fpart += i; | - |
130 | } never executed: end of block else { | 0 |
131 | if (++TRUE | never evaluated | FALSE | never evaluated |
ndigits >= ((sizeof(units)/sizeof(units[0])) * 3)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
132 | | - |
133 | (*__errno_location ()) | - |
134 | = | - |
135 | 34 | - |
136 | ; | - |
137 | return never executed: return -1; -1;never executed: return -1; | 0 |
138 | } | - |
139 | if (whole > 0x7fffffffffffffffLL / 10TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
140 | | - |
141 | (*__errno_location ()) | - |
142 | = | - |
143 | 34 | - |
144 | ; | - |
145 | return never executed: return -1; -1;never executed: return -1; | 0 |
146 | } | - |
147 | whole *= 10; | - |
148 | if (i > 0x7fffffffffffffffLL - wholeTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
149 | | - |
150 | (*__errno_location ()) | - |
151 | = | - |
152 | 34 | - |
153 | ; | - |
154 | return never executed: return -1; -1;never executed: return -1; | 0 |
155 | } | - |
156 | whole += i; | - |
157 | } never executed: end of block | 0 |
158 | } | - |
159 | | - |
160 | if (signTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
161 | whole *= sign; | - |
162 | fpart *= sign; | - |
163 | } never executed: end of block | 0 |
164 | | - |
165 | | - |
166 | if (!*pTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
167 | *result = whole; | - |
168 | return never executed: return 0; 0;never executed: return 0; | 0 |
169 | } | - |
170 | | - |
171 | | - |
172 | for (i = 0; i < (sizeof(units)/sizeof(units[0]))TRUE | never evaluated | FALSE | never evaluated |
; i++) { | 0 |
173 | | - |
174 | | - |
175 | if (*TRUE | never evaluated | FALSE | never evaluated |
p == scale_chars[i]TRUE | never evaluated | FALSE | never evaluated |
|| | 0 |
176 | *TRUE | never evaluated | FALSE | never evaluated |
p == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
177 | (__extension__ ({ int __res; if (sizeof (TRUE | never evaluated | FALSE | never evaluated |
| 0 |
178 | (unsigned char)scale_chars[i]TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
179 | ) > 1) { if (__builtin_constant_p (TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
180 | (unsigned char)scale_chars[i]TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
181 | )) { int __c = (TRUE | never evaluated | FALSE | never evaluated |
| 0 |
182 | (unsigned char)scale_chars[i]TRUE | never evaluated | FALSE | never evaluated |
| 0 |
183 | ); __res = __c < -128TRUE | never evaluated | FALSE | never evaluated |
|| __c > 255TRUE | never evaluated | FALSE | never evaluated |
? __c : (*__ctype_tolower_loc ())[__c]; } else __res = tolower (never executed: __res = tolower ( (unsigned char)scale_chars[i] ); | 0 |
184 | (unsigned char)scale_chars[i]TRUE | never evaluated | FALSE | never evaluated |
never executed: __res = tolower ( (unsigned char)scale_chars[i] ); | 0 |
185 | ); } else __res = (*__ctype_tolower_loc ())[(int) (TRUE | never evaluated | FALSE | never evaluated |
never executed: __res = (*__ctype_tolower_loc ())[(int) ( (unsigned char)scale_chars[i] )]; | 0 |
186 | (unsigned char)scale_chars[i]TRUE | never evaluated | FALSE | never evaluated |
never executed: __res = (*__ctype_tolower_loc ())[(int) ( (unsigned char)scale_chars[i] )]; | 0 |
187 | )]; __res; }))TRUE | never evaluated | FALSE | never evaluated |
| 0 |
188 | ) { | - |
189 | | - |
190 | | - |
191 | if ( | - |
192 | ((*TRUE | never evaluated | FALSE | never evaluated |
__ctype_b_loc ())[(int) ((TRUE | never evaluated | FALSE | never evaluated |
| 0 |
193 | (unsigned char)*(p+1)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
194 | ))] & (unsigned short int) _ISalnum)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
195 | ) { | - |
196 | | - |
197 | (*__errno_location ()) | - |
198 | = | - |
199 | 22 | - |
200 | ; | - |
201 | return never executed: return -1; -1;never executed: return -1; | 0 |
202 | } | - |
203 | scale_fact = scale_factors[i]; | - |
204 | | - |
205 | | - |
206 | if (whole > 0x7fffffffffffffffLL / scale_factTRUE | never evaluated | FALSE | never evaluated |
|| | 0 |
207 | whole < TRUE | never evaluated | FALSE | never evaluated |
| 0 |
208 | (-0x7fffffffffffffffLL - 1LL) TRUE | never evaluated | FALSE | never evaluated |
| 0 |
209 | / scale_factTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
210 | | - |
211 | (*__errno_location ()) | - |
212 | = | - |
213 | 34 | - |
214 | ; | - |
215 | return never executed: return -1; -1;never executed: return -1; | 0 |
216 | } | - |
217 | | - |
218 | | - |
219 | whole *= scale_fact; | - |
220 | | - |
221 | | - |
222 | | - |
223 | | - |
224 | while (fpart >= 0x7fffffffffffffffLL / scale_factTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
225 | fpart /= 10; | - |
226 | fract_digits--; | - |
227 | } never executed: end of block | 0 |
228 | fpart *= scale_fact; | - |
229 | if (fract_digits > 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
230 | for (i = 0; i < fract_digits -1TRUE | never evaluated | FALSE | never evaluated |
; i++) | 0 |
231 | fpart /= 10; never executed: fpart /= 10; | 0 |
232 | } never executed: end of block | 0 |
233 | whole += fpart; | - |
234 | *result = whole; | - |
235 | return never executed: return 0; 0;never executed: return 0; | 0 |
236 | } | - |
237 | } never executed: end of block | 0 |
238 | | - |
239 | | - |
240 | | - |
241 | (*__errno_location ()) | - |
242 | = | - |
243 | 22 | - |
244 | ; | - |
245 | return never executed: return -1; -1;never executed: return -1; | 0 |
246 | } | - |
247 | | - |
248 | | - |
249 | | - |
250 | | - |
251 | | - |
252 | int | - |
253 | fmt_scaled(long long number, char *result) | - |
254 | { | - |
255 | long long abval, fract = 0; | - |
256 | unsigned int i; | - |
257 | unit_type unit = NONE; | - |
258 | | - |
259 | abval = llabs(number); | - |
260 | | - |
261 | | - |
262 | | - |
263 | | - |
264 | if (abval < 0TRUE | never evaluated | FALSE | never evaluated |
|| abval / 1024 >= scale_factors[(sizeof(units)/sizeof(units[0]))-1]TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
265 | | - |
266 | (*__errno_location ()) | - |
267 | = | - |
268 | 34 | - |
269 | ; | - |
270 | return never executed: return -1; -1;never executed: return -1; | 0 |
271 | } | - |
272 | | - |
273 | | - |
274 | for (i = 0; i < (sizeof(units)/sizeof(units[0]))TRUE | never evaluated | FALSE | never evaluated |
; i++) { | 0 |
275 | if (abval/1024 < scale_factors[i]TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
276 | unit = units[i]; | - |
277 | fract = (TRUE | never evaluated | FALSE | never evaluated |
i == 0)TRUE | never evaluated | FALSE | never evaluated |
? 0 : abval % scale_factors[i]; | 0 |
278 | number /= scale_factors[i]; | - |
279 | if (i > 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
280 | fract /= scale_factors[i - 1]; never executed: fract /= scale_factors[i - 1]; | 0 |
281 | break; never executed: break; | 0 |
282 | } | - |
283 | } never executed: end of block | 0 |
284 | | - |
285 | fract = (10 * fract + 512) / 1024; | - |
286 | | - |
287 | if (fract >= 10TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
288 | if (number >= 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
289 | number++; never executed: number++; | 0 |
290 | else | - |
291 | number--; never executed: number--; | 0 |
292 | fract = 0; | - |
293 | } never executed: end of block else if (fract < 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
294 | | - |
295 | fract = 0; | - |
296 | } never executed: end of block | 0 |
297 | | - |
298 | if (number == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
299 | strlcpy(result, "0B", 7); never executed: strlcpy(result, "0B", 7); | 0 |
300 | else if (unit == NONETRUE | never evaluated | FALSE | never evaluated |
|| number >= 100TRUE | never evaluated | FALSE | never evaluated |
|| number <= -100TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
301 | if (fract >= 5TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
302 | if (number >= 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
303 | number++; never executed: number++; | 0 |
304 | else | - |
305 | number--; never executed: number--; | 0 |
306 | } | - |
307 | (void)snprintf(result, 7, "%lld%c", | - |
308 | number, scale_chars[unit]); | - |
309 | } never executed: end of block else | 0 |
310 | ( never executed: (void)snprintf(result, 7, "%lld.%1lld%c", number, fract, scale_chars[unit]); void)snprintf(result, 7, "%lld.%1lld%c",never executed: (void)snprintf(result, 7, "%lld.%1lld%c", number, fract, scale_chars[unit]); | 0 |
311 | number, fract, scale_chars[unit]); never executed: (void)snprintf(result, 7, "%lld.%1lld%c", number, fract, scale_chars[unit]); | 0 |
312 | | - |
313 | return never executed: return 0; 0;never executed: return 0; | 0 |
314 | } | - |
| | |