/* If true, print file system type as well. */
static bool print_type;
+/* If true, print a grand total at the end. */
+static bool print_grand_total;
+
+/* Grand total data. */
+static struct fs_usage grand_fsu;
+
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
enum
{"print-type", no_argument, NULL, 'T'},
{"sync", no_argument, NULL, SYNC_OPTION},
{"no-sync", no_argument, NULL, NO_SYNC_OPTION},
+ {"total", no_argument, NULL, 'c'},
{"type", required_argument, NULL, 't'},
{"exclude-type", required_argument, NULL, 'x'},
{GETOPT_HELP_OPTION_DECL},
}
}
+/* Logical equivalence */
+#define LOG_EQ(a, b) (!(a) == !(b))
+
+/* Add integral value while using uintmax_t for value part and separate
+ negation flag. It adds value of SRC and SRC_NEG to DEST and DEST_NEG.
+ The result will be in DEST and DEST_NEG. See df_readable to understand
+ how the negation flag is used. */
+static void
+add_uint_with_neg_flag (uintmax_t *dest, bool *dest_neg,
+ uintmax_t src, bool src_neg)
+{
+ if (LOG_EQ (*dest_neg, src_neg))
+ {
+ *dest += src;
+ return;
+ }
+
+ if (*dest_neg)
+ *dest = -*dest;
+
+ if (src_neg)
+ src = -src;
+
+ if (src < *dest)
+ *dest -= src;
+ else
+ {
+ *dest = src - *dest;
+ *dest_neg = src_neg;
+ }
+
+ if (*dest_neg)
+ *dest = -*dest;
+}
+
/* Display a space listing for the disk device with absolute file name DISK.
If MOUNT_POINT is non-NULL, it is the name of the root of the
file system on DISK.
static void
show_dev (char const *disk, char const *mount_point,
char const *stat_file, char const *fstype,
- bool me_dummy, bool me_remote)
+ bool me_dummy, bool me_remote,
+ const struct fs_usage *force_fsu)
{
struct fs_usage fsu;
char buf[3][LONGEST_HUMAN_READABLE + 2];
if (!stat_file)
stat_file = mount_point ? mount_point : disk;
- if (get_fs_usage (stat_file, disk, &fsu))
+ if (force_fsu)
+ fsu = *force_fsu;
+ else if (get_fs_usage (stat_file, disk, &fsu))
{
error (0, errno, "%s", quote (stat_file));
exit_status = EXIT_FAILURE;
available = fsu.fsu_ffree;
negate_available = false;
available_to_root = available;
+
+ grand_fsu.fsu_files += total;
+ grand_fsu.fsu_ffree += available;
}
else
{
negate_available = (fsu.fsu_bavail_top_bit_set
& (available != UINTMAX_MAX));
available_to_root = fsu.fsu_bfree;
+
+ grand_fsu.fsu_blocks += input_units * total;
+ grand_fsu.fsu_bfree += input_units * available_to_root;
+ add_uint_with_neg_flag (&grand_fsu.fsu_bavail,
+ &grand_fsu.fsu_bavail_top_bit_set,
+ input_units * available, negate_available);
}
used = UINTMAX_MAX;
{
show_dev (best_match->me_devname, best_match->me_mountdir, NULL,
best_match->me_type, best_match->me_dummy,
- best_match->me_remote);
+ best_match->me_remote, NULL);
return true;
}
if (best_match)
show_dev (best_match->me_devname, best_match->me_mountdir, point,
- best_match->me_type, best_match->me_dummy, best_match->me_remote);
+ best_match->me_type, best_match->me_dummy, best_match->me_remote,
+ NULL);
else
{
/* We couldn't find the mount entry corresponding to POINT. Go ahead and
char *mp = find_mount_point (point, statp);
if (mp)
{
- show_dev (NULL, mp, NULL, NULL, false, false);
+ show_dev (NULL, mp, NULL, NULL, false, false, NULL);
free (mp);
}
}
for (me = mount_list; me; me = me->me_next)
show_dev (me->me_devname, me->me_mountdir, NULL, me->me_type,
- me->me_dummy, me->me_remote);
+ me->me_dummy, me->me_remote, NULL);
}
/* Add FSTYPE to the list of file system types to display. */
fputs (_("\
-a, --all include dummy file systems\n\
-B, --block-size=SIZE use SIZE-byte blocks\n\
+ --total produce a grand total\n\
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)\n\
-H, --si likewise, but use powers of 1000 not 1024\n\
"), stdout);
file_systems_processed = false;
posix_format = false;
exit_status = EXIT_SUCCESS;
+ print_grand_total = false;
+ grand_fsu.fsu_blocksize = 1;
for (;;)
{
add_excluded_fs_type (optarg);
break;
+ case 'c':
+ print_grand_total = true;
+ break;
+
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
else
show_all_entries ();
+ if (print_grand_total)
+ {
+ if (inode_format)
+ grand_fsu.fsu_blocks = 1;
+ show_dev ("total", NULL, NULL, NULL, false, false, &grand_fsu);
+ }
+
if (! file_systems_processed)
error (EXIT_FAILURE, 0, _("no file systems processed"));
--- /dev/null
+#!/bin/sh
+# Ensure "df --total" computes well summary statistics
+
+# Copyright (C) 2008 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ ls --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+# Don't let a different umask perturb the results.
+umask 22
+
+echo '
+BEGIN {
+ total = 0
+ used = 0
+ available = 0
+}
+{
+ if (NR==1 || $0==$1 || $0~/^total +(-?[0-9]+|-) +(-?[0-9]+|-) +(-?[0-9]+|-) +-?[0-9]+%$/)
+ next
+ if ($1~/^[0-9]/)
+ {
+ total += $1
+ used += $2
+ available += $3
+ }
+ else
+ {
+ total += $2
+ used += $3
+ available += $4
+ }
+}
+END {
+ print total
+ print used
+ print available
+}
+' > compute_sum.awk || fail=1
+
+echo '
+/^total +(-?[0-9]+|-) +(-?[0-9]+|-) +(-?[0-9]+|-) +-?[0-9]+%$/ {
+ print $2;
+ print $3;
+ print $4
+}
+' > parse_total.awk || fail=1
+
+df --total > tmp || fail=1
+$AWK -f compute_sum.awk tmp > out1 || fail=1
+$AWK -f parse_total.awk tmp > out2 || fail=1
+compare out1 out2 || fail=1
+
+df -i --total > tmp || fail=1
+$AWK -f compute_sum.awk tmp > out1 || fail=1
+$AWK -f parse_total.awk tmp > out2 || fail=1
+compare out1 out2 || fail=1
+
+(exit $fail); exit $fail