bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

patch for 'df', --total from TODO


From: Chris Van Nuys
Subject: patch for 'df', --total from TODO
Date: Sun, 14 Dec 2003 13:28:08 -0800

Hello,
(for Jim: I've decided to give up on the 'ls --format', you're right, it's a big project ;)

Browsing through the TODO list for coreutils I found the request that a --total option be added to 'df' to summarize the total blocksize, Used, and Available. I've written a patch to do that, and I'd like your input as to its design. It seems to work fine (for me). Please let me know if it's what you're looking for, or if it's not.

An example of the output (of course, it all lines up in the Unix shell, doesn't paste well into Eudora though):

$ ./df -kT /dev/sda1 /dev/sda2 --total
Filesystem    Type   1K-blocks      Used Available Use% Mounted on
/dev/sda1       ext2       996116    919992     25524  98% /
/dev/sda2       ext2     8064304   1559644 6095004  21% /var
(total)                        9060420   2479636 6120528

Thanks for your time again,
Chris Van Nuys

Here is the patch I've written as a unified diff:

--- df.c        Sun Dec 14 12:47:25 2003
+++ newdf.c     Sun Dec 14 13:16:58 2003
@@ -64,6 +64,9 @@
 /* Human-readable options for output.  */
 static int human_output_opts;

+/* Nonzero means to calculate totals for block size, etc. --total turns this on */
+static int calculate_total = 0;
+
 /* The units to use when printing sizes.  */
 static uintmax_t output_block_size;

@@ -111,12 +114,21 @@
 /* If nonzero, print filesystem type as well.  */
 static int print_type;

+/* Prints the total kilo, avail, and used, if --total is used */
+char *print_total[1024];
+
+/* Initialize variables used to calculate totals */
+static uintmax_t total_used = 0;
+static uintmax_t total_available = 0;
+static uintmax_t total_kbytes = 0;
+
 /* For long options that have no equivalent short option, use a
    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
 enum
 {
   SYNC_OPTION = CHAR_MAX + 1,
-  NO_SYNC_OPTION
+  NO_SYNC_OPTION,
+  CALC_TOTAL
 };

 static struct option const long_options[] =
@@ -135,6 +147,7 @@
   {"no-sync", no_argument, NULL, NO_SYNC_OPTION},
   {"type", required_argument, NULL, 't'},
   {"exclude-type", required_argument, NULL, 'x'},
+  {"total", no_argument, NULL, CALC_TOTAL},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -268,6 +281,7 @@
   struct fs_usage fsu;
   const char *stat_file;
   char buf[3][LONGEST_HUMAN_READABLE + 2];
+  char *total_str[1024];
   int width;
   int use_width;
   uintmax_t input_units;
@@ -369,6 +383,23 @@
        }
     }

+       if (calculate_total)
+        {
+           total_used += used;
+          total_kbytes += total;
+          total_available += available;
+
+          strcpy (total_str, "(total)  ");
+
+          sprintf (print_total, "%-20s %*s %*s %*s\n", total_str,
+           width, df_readable (0, total_kbytes,
+                               buf[0], input_units, output_units),
+           width, df_readable (negate_used, total_used,
+                               buf[1], input_units, output_units),
+           width, df_readable (negate_available, total_available,
+                               buf[2], input_units, output_units));
+         }
+
   printf (" %*s %*s %*s ",
          width, df_readable (0, total,
                              buf[0], input_units, output_units),
@@ -429,6 +460,7 @@
 #endif
       printf (" %s", mount_point);
     }
+
   putchar ('\n');
 }

@@ -750,6 +782,7 @@
       --sync            invoke sync before getting usage info\n\
   -t, --type=TYPE       limit listing to filesystems of type TYPE\n\
   -T, --print-type      print filesystem type\n\
+ --total print grand total of block size, used, and available\n\
   -x, --exclude-type=TYPE   limit listing to filesystems not of type TYPE\n\
   -v                    (ignored)\n\
 "), stdout);
@@ -839,6 +872,9 @@
        case NO_SYNC_OPTION:
          require_sync = 0;
          break;
+       case CALC_TOTAL:
+         calculate_total = 1;
+         break;

        case 'F':
          /* Accept -F as a synonym for -t for compatibility with Solaris.  */
@@ -946,6 +982,8 @@
       print_header ();
       show_all_entries ();
     }
+  if (calculate_total)
+       printf("%s", print_total);

   exit (exit_status);
 }





reply via email to

[Prev in Thread] Current Thread [Next in Thread]