Skip to content

Commit d7e08dc

Browse files
Michal Frynaschirayudesai
authored andcommitted
Fixed improper size displaying in 'df' utility
'df' command used to display filesystem usage statistics as integer values, in most cases rounding the actual value down. Because of that 'df' tended to display faulty size values. This fix to 'df' utility calculates the fractional part of the size, then it rounds it when needed to the nearest one-digit integer value and displays after decimal dot. Change-Id: I9bc52635d45d3e55ce61b3b1c6b80d1267516e75 Signed-off-by: Chirayu Desai <[email protected]>
1 parent d8aa8ab commit d7e08dc

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

toolbox/df.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,27 @@ static int ok = EXIT_SUCCESS;
99
static void printsize(long long n)
1010
{
1111
char unit = 'K';
12-
n /= 1024;
13-
if (n > 1024) {
12+
long long t;
13+
14+
n *= 10;
15+
16+
if (n > 1024*1024*10) {
1417
n /= 1024;
1518
unit = 'M';
1619
}
17-
if (n > 1024) {
20+
21+
if (n > 1024*1024*10) {
1822
n /= 1024;
1923
unit = 'G';
2024
}
21-
printf("%4lld%c", n, unit);
25+
26+
t = (n + 512) / 1024;
27+
if (t%10 != 0) {
28+
printf("%4lld.%1lld%c", t/10, t%10, unit);
29+
}
30+
else {
31+
printf("%4lld%c", t/10, unit);
32+
}
2233
}
2334

2435
static void df(char *s, int always) {
@@ -41,7 +52,7 @@ static void df(char *s, int always) {
4152
}
4253

4354
int df_main(int argc, char *argv[]) {
44-
printf("Filesystem Size Used Free Blksize\n");
55+
printf("Filesystem Size Used Free Blksize\n");
4556
if (argc == 1) {
4657
char s[2000];
4758
FILE *f = fopen("/proc/mounts", "r");

0 commit comments

Comments
 (0)