File: fmt_human.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (28 lines) | stat: -rw-r--r-- 576 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "fmt.h"

size_t fmt_human(char* dest,unsigned long long l) {
  char unit;
  size_t i;
  if (l<1000) return fmt_ulong(dest,l);
  if (l>1000000000000ull) {
    /* dang!  overflow! */
    l/=1000;
    l=(l+50000000)/100000000;
    unit='T';
  } else if (l>1000000000) {
    l=(l+50000000)/100000000;
    unit='G';
  } else if (l>1000000) {
    l=(l+50000)/100000;
    unit='M';
  } else {
    l=(l+50)/100;
    unit='k';
  }
  if (!dest) return fmt_ulong(0,l)+2;
  i=fmt_ulong(dest,l/10);
  dest[i]='.';
  dest[i+1]=(char)((l%10)+'0');
  dest[i+2]=unit;
  return i+3;
}