-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbyte-size-english.c
executable file
·81 lines (65 loc) · 1.41 KB
/
byte-size-english.c
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Synopsis:
* Convert byte count to brief english K,M,G,T,P,E,Z,Y,R,Q in 1024 powers.
*/
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include "jmscott/libjmscott.h"
char *jmscott_progname = "byte-size-english";
extern int errno;
static char english[] = {
'B', // bytes
'K', // kilo
'M', // mega
'G', // giga
'T', // tera
'P', // peta
'E', // exa
'Z', // zetta
'Y', // yotta
'R', // ronna
'Q' // quetta
};
#define ROUND(n, d) (((n) + (d)/2)/(d))
static char *usage = "usage: byte-size-english <byte size>";
static void
die(char *msg)
{
jmscott_die(1, msg);
}
static void
die2(char *msg1, char *msg2)
{
jmscott_die2(1, msg1, msg2);
}
int
main(int argc, char **argv)
{
if (argc != 2)
jmscott_die_argc(1, 1, argc -1, usage);
if (*argv[1] == 0)
die("empty byte size");
// extract byte size
unsigned long long bs;
char *err = jmscott_a2ui63(argv[1], &bs);
if (err)
die2("can not parse byte size", err);
unsigned long long r = 1024;
for (int i = 0; i < 11; i++, r *= 1024) {
if (bs >= r)
continue;
char buf[30];
char *p = jmscott_ulltoa(ROUND(bs, r / 1024), buf);
*p++ = english[i];
if (i == 0)
*p++ = 's';
else
*p++ = 'b';
*p = '\n';
if (jmscott_write(1, buf, p - buf) < 0)
die2("write(1) failed", strerror(errno));
_exit(0);
}
die2("byte size too big", argv[1]);
}