aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-11-08 15:03:03 -0600
committerRob Landley <rob@landley.net>2012-11-08 15:03:03 -0600
commitc65e89b70b5892d40d4fcab567e615aafcda688a (patch)
tree1ecef1a1473276a94cb852c4d72a542c5fc5812d /toys
parentabb8ca2455f3efd6f8f0eed78c54829bf0a9001e (diff)
downloadtoybox-c65e89b70b5892d40d4fcab567e615aafcda688a.tar.gz
Change df percentage calculation to match posix, spotted by Roy Tam.
Diffstat (limited to 'toys')
-rw-r--r--toys/posix/df.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/toys/posix/df.c b/toys/posix/df.c
index adde58c6..0625946a 100644
--- a/toys/posix/df.c
+++ b/toys/posix/df.c
@@ -49,8 +49,7 @@ GLOBALS(
static void show_mt(struct mtab_list *mt)
{
int len;
- long size, used, avail, percent;
- uint64_t block;
+ uint64_t size, used, avail, percent, block;
char *device;
// Return if it wasn't found (should never happen, but with /etc/mtab...)
@@ -72,13 +71,15 @@ static void show_mt(struct mtab_list *mt)
// Figure out how much total/used/free space this filesystem has,
// forcing 64-bit math because filesystems are big now.
block = mt->statvfs.f_bsize ? mt->statvfs.f_bsize : 1;
- size = (long)((block * mt->statvfs.f_blocks) / TT.units);
- used = (long)((block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree))
- / TT.units);
- avail = (long)((block
- * (getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree))
- / TT.units);
- percent = size ? 100-(long)((100*(uint64_t)avail)/size) : 0;
+ size = (block * mt->statvfs.f_blocks) / TT.units;
+ used = (block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) / TT.units;
+ avail = (block * (getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree))
+ / TT.units;
+ if (!(used+avail)) percent = 0;
+ else {
+ percent = (used*100)/(used+avail);
+ if (used*100 != percent*(used+avail)) percent++;
+ }
device = *mt->device == '/' ? realpath(mt->device, NULL) : NULL;
if (!device) device = mt->device;