bug-coreutils
[Top][All Lists]
Advanced

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

A dd display patch.


From: Olivier Delhomme
Subject: A dd display patch.
Date: Sun, 2 Nov 2003 18:01:38 +0100

Hello,

I added 2 new options to dd :

'display' and 'dbs' :

'display' lets you choose 2 ways for displaying stats : 

 - 'quiet' that will never display any stat
 - 'human' that will display stats in a human readable format 
         according to the 'dbs' option :

'dbs' stands for display_block_size and lets you choose what
will be you favorite display block size. You may say :

 - 'human' to display sizes with powers of 1024 and add letters 
   (K, M, G, ...)
 - 'si' to display sizes with powers of 1000 and add letters
 - BYTES likewise but with powers of BYTES and without letters

 BYTES may be followed by the following multiplicative suffixes:
xM M, c 1, w 2, b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

this 'dbs' option can be set by an environnement variable named 
'DD_DISPLAY_BLOCK_SIZE'.

here is some examples :

1. normal behavior

address@hidden:/vol/projets/coreutils/coreutils-m/src$ ./dd if=/dev/zero
of=/dev/null count=100                     
100+0 records in
100+0 records out
address@hidden:/vol/projets/coreutils/coreutils-m/src$

2. quiet mode 

address@hidden:/vol/projets/coreutils/coreutils-m/src$ ./dd if=/dev/zero
of=/dev/null count=100 display=quiet
address@hidden:/vol/projets/coreutils/coreutils-m/src$ 

3. human readable mode

address@hidden:/vol/projets/coreutils/coreutils-m/src$ ./dd if=/dev/zero
of=/dev/null count=100 display=human dbs=human 
50K+0 records in
50K+0 records out
address@hidden:/vol/projets/coreutils/coreutils-m/src$ 

Here is the patch, any feedback would be much appreciated :

--- coreutils/src/dd.c  2003-10-31 21:48:54.000000000 +0100
+++ coreutils-m/src/dd.c        2003-11-02 17:41:33.000000000 +0100
@@ -35,6 +35,7 @@
 #include "quote.h"
 #include "safe-read.h"
 #include "xstrtol.h"
+#include "human.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "dd"
@@ -143,6 +144,15 @@
 /* Index into current line, for `conv=block' and `conv=unblock'.  */
 static size_t col = 0;
 
+/* This is aimed to choose diplay type */
+static char const *display = "default";
+
+/* Human-readable options for output.  */
+static int human_output_opts;
+
+/* The units to use when printing sizes.  */
+static uintmax_t display_block_size = 0;
+
 struct conversion
 {
   char *convname;
@@ -300,11 +310,24 @@
   of=FILE         write to FILE instead of stdout\n\
   seek=BLOCKS     skip BLOCKS obs-sized blocks at start of output\n\
   skip=BLOCKS     skip BLOCKS ibs-sized blocks at start of input\n\
+  display=MODE    uses display mode according to MODE\n\
+  dbs=SIZE        uses SIZE-byte blocks to display statistics\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
       fputs (_("\
 \n\
+MODE may be:\n\
+  quiet          the statistics will not be displayed\n\
+  human          they will be displayed in a human readable format\n\
+\n\
+SIZE may be:\n\
+  human          prints all sizes in human readable format (e.g. 1K,
234M)\n\+  si             likewise, but uses powers of 1000 instead of
1024\n\+  BYTES          likewise, but use powers of BYTES\n\
+"),stdout);
+      fputs (_("\
+\n\
 BLOCKS and BYTES may be followed by the following multiplicative
suffixes:\n\ xM M, c 1, w 2, b 512, kB 1000, K 1024, MB 1000*1000, M
1024*1024,\n\ GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P,
E, Z, Y.\n\@@ -366,21 +389,51 @@
 }
 
 static void
-print_stats (void)
+print_human_stats(void)
 {
-  char buf[2][INT_BUFSIZE_BOUND (uintmax_t)];
+  char display_buf[5][MAX (LONGEST_HUMAN_READABLE + 1,
INT_BUFSIZE_BOUND (uintmax_t))];+
   fprintf (stderr, _("%s+%s records in\n"),
-          umaxtostr (r_full, buf[0]), umaxtostr (r_partial, buf[1]));
+          human_readable (r_full, display_buf[0], human_output_opts,
input_blocksize, display_block_size), +    human_readable (r_partial,
display_buf[1], human_output_opts, input_blocksize,
display_block_size));+     
   fprintf (stderr, _("%s+%s records out\n"),
-          umaxtostr (w_full, buf[0]), umaxtostr (w_partial, buf[1]));
+          human_readable (w_full, display_buf[2],  human_output_opts,
output_blocksize , display_block_size), +          human_readable
(w_partial, display_buf[3], human_output_opts, output_blocksize,
display_block_size));+     
   if (r_truncate > 0)
     {
       fprintf (stderr, "%s %s\n",
-              umaxtostr (r_truncate, buf[0]),
-              (r_truncate == 1
-               ? _("truncated record")
+              human_readable (r_truncate, display_buf[5],
human_output_opts, output_blocksize, display_block_size),+             
(r_truncate == 1         
                                                                                
        ? _("truncated
record")                : _("truncated records")));
-    }
+    } 
+}
+
+static void
+print_stats (void)
+{
+  char buf[2][INT_BUFSIZE_BOUND (uintmax_t)];
+  
+  if (STREQ (display,"human"))
+    { /* human readable format */
+       print_human_stats();
+    }
+  else if (!STREQ (display,"quiet"))
+   { /* in case dd should not be quiet */        
+     fprintf (stderr, _("%s+%s records in\n"),
+            umaxtostr (r_full, buf[0]), umaxtostr (r_partial,
buf[1]));+     fprintf (stderr, _("%s+%s records out\n"),
+            umaxtostr (w_full, buf[0]), umaxtostr (w_partial, buf[1]));
+     if (r_truncate > 0)
+       {
+           fprintf (stderr, "%s %s\n",
+                  umaxtostr (r_truncate, buf[0]),
+                  (r_truncate == 1
+                  ?_("truncated record")
+                  : _("truncated records")));
+       }
+   }
 }
 
 static void
@@ -575,6 +628,10 @@
        output_file = val;
       else if (STREQ (name, "conv"))
        parse_conversion (val);
+      else if (STREQ (name, "display")) // choose your display mode
(quiet, human, normal)+ display = val;
+      else if (STREQ (name, "dbs")) // display block size 
+       human_output_opts = human_options (val, true, &display_block_size);
       else
        {
          int invalid = 0;
@@ -1163,6 +1220,9 @@
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
                      usage, AUTHORS, NULL);
 
+  human_output_opts = human_options (getenv
("DD_DISPLAY_BLOCK_SIZE"), false,+                                              
    
&display_block_size); +
   /* Don't close stdout on exit from here on.  */
   closeout_func = NULL;








reply via email to

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