Quoth POSIX Issue 7:
  <percentage used>
    The percentage of the normally available space that is currently
    allocated to all files on the file system. This shall be calculated
    using the fraction:
      <space used>/( <space used>+ <space free>)
    expressed as a percentage. This percentage may be greater than 100
    if <space free> is less than zero. The percentage value shall be
    expressed as a positive integer, with any fractional result causing
    it to be rounded to the next highest integer.

Nominally this only applies to -P and -Pk behaviour (the last hunk),
but for consistency it may be best to apply that everywhere
---
 bin/df/Makefile | 4 ++--
 bin/df/df.c     | 7 ++++---
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/bin/df/Makefile b/bin/df/Makefile
index 0a79ba29be6..099a4f0333b 100644
--- a/bin/df/Makefile
+++ b/bin/df/Makefile
@@ -2,7 +2,7 @@
 
 PROG=  df
 SRCS=  df.c ffs_df.c ext2fs_df.c
-LDADD= -lutil
-DPADD= ${LIBUTIL}
+LDADD= -lutil -lm
+DPADD= ${LIBUTIL} ${LIBM}
 
 .include <bsd.prog.mk>
diff --git a/bin/df/df.c b/bin/df/df.c
index fd51f906f89..d13f1d2d54a 100644
--- a/bin/df/df.c
+++ b/bin/df/df.c
@@ -41,6 +41,7 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -324,12 +325,12 @@ prtstat(struct statfs *sfsp, int maxwidth, int headerlen, 
int blocksize)
                    fsbtoblk(used, sfsp->f_bsize, blocksize),
                    fsbtoblk(sfsp->f_bavail, sfsp->f_bsize, blocksize));
        (void)printf(" %5.0f%%",
-           availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
+           availblks == 0 ? 100.0 : ceil((double)used / (double)availblks * 
100.0));
        if (iflag) {
                inodes = sfsp->f_files;
                used = inodes - sfsp->f_ffree;
                (void)printf(" %7llu %7llu %5.0f%% ", used, sfsp->f_ffree,
-                  inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
+                  inodes == 0 ? 100.0 : ceil((double)used / (double)inodes * 
100.0));
        } else
                (void)printf("  ");
        (void)printf("  %s\n", sfsp->f_mntonname);
@@ -404,7 +405,7 @@ posixprint(struct statfs *mntbuf, long mntsize, int 
maxwidth)
                if (avail == 0)
                        percentused = 100.0;
                else
-                       percentused = (double)used / (double)avail * 100.0;
+                       percentused = ceil((double)used / (double)avail * 
100.0);
 
                (void) printf ("%-*.*s %*lld %10lld %11lld %5.0f%%   %s\n",
                        maxwidth, maxwidth, sfsp->f_mntfromname,
-- 
2.30.2

Attachment: signature.asc
Description: PGP signature

Reply via email to