Return the int64 number as a string. If the human_flag arg is non-zero, * we may output the number in K, M, G, or T units. If we don't add a unit * suffix, we will append the fract string, if it is non-NULL. We can * return up to 4 buffers at a time. */
| 168 | * suffix, we will append the fract string, if it is non-NULL. We can |
| 169 | * return up to 4 buffers at a time. */ |
| 170 | char *do_big_num(int64 num, int human_flag, const char *fract) |
| 171 | { |
| 172 | static char bufs[4][128]; /* more than enough room */ |
| 173 | static unsigned int n; |
| 174 | char *s; |
| 175 | int len, negated; |
| 176 | |
| 177 | if (human_flag && !number_separator) |
| 178 | (void)get_number_separator(); |
| 179 | |
| 180 | n = (n + 1) % (sizeof bufs / sizeof bufs[0]); |
| 181 | |
| 182 | if (human_flag > 1) { |
| 183 | int mult = human_flag == 2 ? 1000 : 1024; |
| 184 | if (num >= mult || num <= -mult) { |
| 185 | double dnum = (double)num / mult; |
| 186 | char units; |
| 187 | if (num < 0) |
| 188 | dnum = -dnum; |
| 189 | if (dnum < mult) |
| 190 | units = 'K'; |
| 191 | else if ((dnum /= mult) < mult) |
| 192 | units = 'M'; |
| 193 | else if ((dnum /= mult) < mult) |
| 194 | units = 'G'; |
| 195 | else if ((dnum /= mult) < mult) |
| 196 | units = 'T'; |
| 197 | else { |
| 198 | dnum /= mult; |
| 199 | units = 'P'; |
| 200 | } |
| 201 | if (num < 0) |
| 202 | dnum = -dnum; |
| 203 | snprintf(bufs[n], sizeof bufs[0], "%.2f%c", dnum, units); |
| 204 | return bufs[n]; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | s = bufs[n] + sizeof bufs[0] - 1; |
| 209 | if (fract) { |
| 210 | len = strlen(fract); |
| 211 | s -= len; |
| 212 | strlcpy(s, fract, len + 1); |
| 213 | } else |
| 214 | *s = '\0'; |
| 215 | |
| 216 | len = 0; |
| 217 | |
| 218 | if (!num) |
| 219 | *--s = '0'; |
| 220 | if (num < 0) { |
| 221 | /* A maximum-size negated number can't fit as a positive, |
| 222 | * so do one digit in negated form to start us off. */ |
| 223 | *--s = (char)(-(num % 10)) + '0'; |
| 224 | num = -(num / 10); |
| 225 | len++; |
| 226 | negated = 1; |
| 227 | } else |
no test coverage detected