Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Fixed improper size displaying in 'df' utility
Browse files Browse the repository at this point in the history
'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
  • Loading branch information
Michal Frynas authored and Kenneth Andersson committed Oct 2, 2012
1 parent 1c0c525 commit 1f90dcd
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions toolbox/df.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ static int ok = EXIT_SUCCESS;
static void printsize(long long n)
{
char unit = 'K';
n /= 1024;
if (n > 1024) {
long long t;

n *= 10;

if (n > 1024*1024*10) {
n /= 1024;
unit = 'M';
}
if (n > 1024) {

if (n > 1024*1024*10) {
n /= 1024;
unit = 'G';
}
printf("%4lld%c", n, unit);

t = (n + 512) / 1024;
printf("%4lld.%1lld%c", t/10, t%10, unit);
}

static void df(char *s, int always) {
Expand All @@ -41,7 +47,7 @@ static void df(char *s, int always) {
}

int df_main(int argc, char *argv[]) {
printf("Filesystem Size Used Free Blksize\n");
printf("Filesystem Size Used Free Blksize\n");
if (argc == 1) {
char s[2000];
FILE *f = fopen("/proc/mounts", "r");
Expand Down

0 comments on commit 1f90dcd

Please sign in to comment.