aboutsummaryrefslogtreecommitdiffstats
path: root/fmt.c
blob: 4c931dd4ad74bb0e16f99b997e028d82e0b7a091 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* See LICENSE file for copyright and license details. */
#include "common.h"


static const char *units_big = "KMGTPEZYRQ";
static const char *units_small = "munpfazyrq"; /* 'u' should be "µ" */


const char *
humansize1000(off_t s, char *buf)
{
	size_t unit = 0;
	if (s < 1000) {
		sprintf(buf, "%u B", (unsigned)s);
		return buf;
	}
	s /= 100;
	while (units_big[unit + 1U] && s >= 10000) {
		s /= 1000;
		unit++;
	}
	sprintf(buf, "%u.%u %cB", (unsigned)s / 10U, (unsigned)s % 10U, units_big[unit]);
	return buf;
}


const char *
humansize1024(off_t s, char *buf)
{
	size_t unit = 0;
	if (s < 1024) {
		sprintf(buf, "%u B", (unsigned)s);
		return buf;
	}
	while (units_big[unit + 1U] && s >= 1024 * 1024) {
		s /= 1024;
		unit++;
	}
	sprintf(buf, "%lu.%lu %ciB", (unsigned long int)s / 1024UL, (unsigned long int)(s * 10 % 10240) / 1024UL, units_big[unit]);
	return buf;
}


off_t
unhumansize(const char *s, char flag)
{
	off_t sum = 0, term, digit, divisor, power, base;

	if (!isdigit(s[0]) && !(s[0] == '-' && isdigit(s[1])))
		eprintf("value of -%c is not a file size", flag);

	do {
		divisor = 1;
		term = 0;
		while (isdigit(*s)) {
			digit = (*s++ & 15);
			if (term > (OFF_MAX - digit) / 10)
				eprintf("value of -%c option is too large", flag);
			term = term * 10 + digit;
		}
		if (*s == '.') {
			s++;
			while (isdigit(*s)) {
				digit = (*s++ & 15);
				if (term > (OFF_MAX - digit) / 10)
					eprintf("value of -%c option is too large", flag);
				term = term * 10 + digit;
				divisor *= 10;
			}
		}

		power = 0;
		switch (*s) {
		case 'Q': power++; /* fall through */
		case 'R': power++; /* fall through */
		case 'Y': power++; /* fall through */
		case 'Z': power++; /* fall through */
		case 'E': power++; /* fall through */
		case 'P': power++; /* fall through */
		case 'T': power++; /* fall through */
		case 'G': power++; /* fall through */
		case 'M': power++; /* fall through */
		case 'k': case 'K': power++;
			if (s[1] == 'i' || s[2] == 'B') {
				base = 1024;
				s = &s[3];
			} else if (s[1] == 'B') {
				base = 1000;
				s = &s[2];
			} else {
				base = 1024;
				s = &s[1];
			}
			while (power) {
				term *= base;
				power--;
			}
			break;
		case 'B':
			s++;
			/* fall through */
		default:
			if (divisor > 1)
				eprintf("value of -%c contains non-integer exact byte count", flag);
			break;
		}
		sum += term /= divisor;

		while (*s == ' ' || *s == ',' || *s == '+')
			s++;

	} while (isdigit(s[0]) || (s[0] == '.' && isdigit(s[1])));

	return sum;
}


const char *
durationstr(const struct timespec *dur, char *buf, int second_decimals)
{
	uintmax_t ss, s, m, h, d, ss_div = (uintmax_t)WHOLE_SECOND;
	char *p;
	const char *unit;
	int i;

	if (dur->tv_sec < 0 || dur->tv_nsec < 0)
		return "-";

	if (second_decimals < 0)
		second_decimals = 0;
	else if (second_decimals > 9)
		second_decimals = 9;

	for (i = 0; i < second_decimals; i++)
		ss_div /= 10U;
	ss = (uintmax_t)dur->tv_nsec / ss_div;
	s = (uintmax_t)dur->tv_sec % 60U;
	m = (uintmax_t)dur->tv_sec / 60U % 60U;
	h = (uintmax_t)dur->tv_sec / 60U / 60U % 24U;
	d = (uintmax_t)dur->tv_sec / 60U / 60U / 24U;

	p = buf;
	if (d)
		p += sprintf(p, "%ju days, ", d);
	if (h) {
		p += sprintf(p, "%ju:%02ju:%02ju", h, m, s);
		unit = "hours";
	} else if (m) {
		p += sprintf(p, "%ju:%02ju", m, s);
		unit = "minutes";
	} else {
		p += sprintf(p, "%ju", s);
		unit = "seconds";
	}
	if (second_decimals)
		p += sprintf(p, ".%0*ju", second_decimals, ss);
	p += sprintf(p, " %s", unit);

	return buf;
}


const char *
humanbytespersecond(double bytes_per_second, char *buf)
{
	int unit = -1;

	if (bytes_per_second < (double)0.01f) {
		do {
			bytes_per_second *= 1000;
			unit++;
		} while (units_small[unit + 1] && bytes_per_second < (double)0.01f);
		if (units_small[unit] == 'u' && have_micro_symbol())
			sprintf(buf, "%.02lf µB/s", bytes_per_second);
		else
			sprintf(buf, "%.02lf %cB/s", bytes_per_second, units_small[unit]);
	} else {
		while (units_big[unit + 1] && bytes_per_second >= 1000) {
			bytes_per_second /= 1000;
			unit++;
		}
		if (unit < 0)
			sprintf(buf, "%.02lf B/s", bytes_per_second);
		else
			sprintf(buf, "%.02lf %cB/s", bytes_per_second, units_big[unit]);
	}

	return buf;
}