Source-Changes-HG archive

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

[src/trunk]: src/bin/dd Add a new command to dd(1): msgfmt. The command modif...



details:   https://anonhg.NetBSD.org/src/rev/0489c8a53eb6
branches:  trunk
changeset: 771061:0489c8a53eb6
user:      jym <jym%NetBSD.org@localhost>
date:      Sun Nov 06 21:22:23 2011 +0000

description:
Add a new command to dd(1): msgfmt. The command modifies the
output of the information summary returned by dd(1). This can be used
to specify messages in a more usable (or parseable) format like
human-readable values.

My intent is to re-use this for building image files and quick I/O
benchmarking.

Reviewed by tsutsui@ on tech-userlevel. See also
http://mail-index.netbsd.org/tech-userlevel/2010/12/03/msg004179.html

Some examples:

 $ dd if=/dev/zero of=/dev/null bs=1m count=1 msgfmt=human
1+0 records in
1+0 records out
1048576 bytes (1,0 MB) transferred in 0.001 secs (1048576000 bytes/sec - 1,0 GB/sec)

 $ dd if=/dev/zero of=/dev/null count=1 msgfmt='
 > <speed>%E</speed>
 > <time>%s</time>
 > <bytes>%b</bytes>
 > '
<speed>500 KB/sec</speed>
<time>0.001</time>
<bytes>512</bytes>

diffstat:

 bin/dd/Makefile |    4 +-
 bin/dd/args.c   |   42 +++++++++--
 bin/dd/dd.1     |   88 ++++++++++++++++++++++-
 bin/dd/dd.c     |    5 +-
 bin/dd/extern.h |    3 +-
 bin/dd/misc.c   |  209 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 333 insertions(+), 18 deletions(-)

diffs (truncated from 505 to 300 lines):

diff -r eaf9b5f52a08 -r 0489c8a53eb6 bin/dd/Makefile
--- a/bin/dd/Makefile   Sun Nov 06 20:21:05 2011 +0000
+++ b/bin/dd/Makefile   Sun Nov 06 21:22:23 2011 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.15 2011/02/04 19:42:12 pooka Exp $
+#      $NetBSD: Makefile,v 1.16 2011/11/06 21:22:23 jym Exp $
 #      @(#)Makefile    8.1 (Berkeley) 5/31/93
 
 RUMPPRG=dd
@@ -8,7 +8,7 @@
 LDADD+=        -lutil
 
 .ifdef SMALLPROG
-CPPFLAGS+=     -DNO_CONV -DSMALL
+CPPFLAGS+=     -DNO_CONV -DNO_MSGFMT -DSMALL
 .else
 SRCS+=         conv_tab.c
 .ifndef CRUNCHEDPROG
diff -r eaf9b5f52a08 -r 0489c8a53eb6 bin/dd/args.c
--- a/bin/dd/args.c     Sun Nov 06 20:21:05 2011 +0000
+++ b/bin/dd/args.c     Sun Nov 06 21:22:23 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: args.c,v 1.35 2011/09/16 16:06:23 joerg Exp $  */
+/*     $NetBSD: args.c,v 1.36 2011/11/06 21:22:23 jym Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)args.c     8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: args.c,v 1.35 2011/09/16 16:06:23 joerg Exp $");
+__RCSID("$NetBSD: args.c,v 1.36 2011/11/06 21:22:23 jym Exp $");
 #endif
 #endif /* not lint */
 
@@ -56,15 +56,22 @@
 #include "extern.h"
 
 static int     c_arg(const void *, const void *);
-#ifndef        NO_CONV
+
+#ifdef NO_MSGFMT
+static void    f_msgfmt(char *) __dead;
+#else
+static void    f_msgfmt(char *);
+#endif /* NO_MSGFMT */
+
+#ifdef NO_CONV
+static void    f_conv(char *) __dead;
+#else
+static void    f_conv(char *);
 static int     c_conv(const void *, const void *);
-#endif
+#endif /* NO_CONV */
+
 static void    f_bs(char *);
 static void    f_cbs(char *);
-#ifdef NO_CONV
-__dead
-#endif
-static void    f_conv(char *);
 static void    f_count(char *);
 static void    f_files(char *);
 static void    f_ibs(char *);
@@ -90,6 +97,7 @@
        { "ibs",        f_ibs,          C_IBS,   C_BS|C_IBS },
        { "if",         f_if,           C_IF,    C_IF },
        { "iseek",      f_skip,         C_SKIP,  C_SKIP },
+       { "msgfmt",     f_msgfmt,       C_SKIP,  C_SKIP },
        { "obs",        f_obs,          C_OBS,   C_BS|C_OBS },
        { "of",         f_of,           C_OF,    C_OF },
        { "oseek",      f_seek,         C_SEEK,  C_SEEK },
@@ -252,6 +260,24 @@
        in.name = arg;
 }
 
+#ifdef NO_MSGFMT
+/* Build a small version (i.e. for a ramdisk root) */
+static void
+f_msgfmt(char *arg)
+{
+
+       errx(EXIT_FAILURE, "msgfmt option disabled");
+       /* NOTREACHED */
+}
+#else  /* NO_MSGFMT */
+static void
+f_msgfmt(char *arg)
+{
+
+       msgfmt = arg;
+}
+#endif /* NO_MSGFMT */
+
 static void
 f_obs(char *arg)
 {
diff -r eaf9b5f52a08 -r 0489c8a53eb6 bin/dd/dd.1
--- a/bin/dd/dd.1       Sun Nov 06 20:21:05 2011 +0000
+++ b/bin/dd/dd.1       Sun Nov 06 21:22:23 2011 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: dd.1,v 1.23 2010/12/22 09:42:53 enami Exp $
+.\"    $NetBSD: dd.1,v 1.24 2011/11/06 21:22:23 jym Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -32,7 +32,7 @@
 .\"
 .\"    @(#)dd.1        8.2 (Berkeley) 1/13/94
 .\"
-.Dd December 22, 2010
+.Dd November 6, 2011
 .Dt DD 1
 .Os
 .Sh NAME
@@ -97,6 +97,74 @@
 blocks.
 This is synonymous with
 .Cm skip= Ns Ar n .
+.It Cm msgfmt= Ns Ar fmt
+Specify the message format
+.Ar fmt
+to be used when writing information to standard output.
+Possible values are:
+.Bl -tag -width xxxxx -offset indent -compact
+.It quiet
+turns off information summary report except for errors and
+.Cm progress .
+.It posix
+default information summary report as specified by POSIX.
+.It human
+default information summary report extended with human-readable
+values.
+.El
+.Pp
+When
+.Ar fmt
+does not correspond to any value given above,
+it contains a string that will be used as format specifier
+for the information summary output.
+Each conversion specification is introduced by the character
+.Cm % .
+The following ones are available:
+.Bl -tag -width xx -offset indent -compact
+.It b
+total number of bytes transferred
+.It B
+total number of bytes transferred in
+.Xr humanize_number 3
+format
+.It e
+speed transfer
+.It E
+speed transfer in
+.Xr humanize_number 3
+format
+.It i
+number of partial input block(s)
+.It I
+number of full input block(s)
+.It o
+number of partial output block(s)
+.It O
+number of full output block(s)
+.It s
+time elapsed since the beginning in
+.Do seconds.ms Dc
+format
+.It p
+number of sparse output blocks
+.It t
+number of truncated blocks
+.It w
+number of odd-length swab blocks
+.It P
+singular/plural of
+.Do block Dc
+depending on number of sparse blocks
+.It T
+singular/plural of
+.Do block Dc
+depending on number of truncated blocks
+.It W
+singular/plural of
+.Do block Dc
+depending on number of swab blocks
+.El
 .It Cm obs= Ns Ar n
 Set the output block size to
 .Va n
@@ -370,6 +438,18 @@
 The
 .Nm
 utility exits 0 on success and \*[Gt]0 if an error occurred.
+.Sh EXAMPLES
+To print summary information in human-readable form:
+.Pp
+.Dl dd if=/dev/zero of=/dev/null count=1 msgfmt=human
+.Pp
+To customize the information summary output and print it through
+.Xr unvis 3 :
+.Pp
+.Bd -literal -offset indent
+dd if=/dev/zero of=/dev/null count=1 \e
+     msgfmt='speed:%E, in %s seconds\en' 2\*[Gt]\*[Am]1 | unvis
+.Ed
 .Sh SEE ALSO
 .Xr cp 1 ,
 .Xr mt 1 ,
@@ -382,7 +462,9 @@
 standard.
 The
 .Cm files
-operand and the
+and
+.Cm msgfmt
+operands and the
 .Cm ascii ,
 .Cm ebcdic ,
 .Cm ibm ,
diff -r eaf9b5f52a08 -r 0489c8a53eb6 bin/dd/dd.c
--- a/bin/dd/dd.c       Sun Nov 06 20:21:05 2011 +0000
+++ b/bin/dd/dd.c       Sun Nov 06 21:22:23 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dd.c,v 1.47 2011/02/04 19:42:12 pooka Exp $    */
+/*     $NetBSD: dd.c,v 1.48 2011/11/06 21:22:23 jym Exp $      */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -43,7 +43,7 @@
 #if 0
 static char sccsid[] = "@(#)dd.c       8.5 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: dd.c,v 1.47 2011/02/04 19:42:12 pooka Exp $");
+__RCSID("$NetBSD: dd.c,v 1.48 2011/11/06 21:22:23 jym Exp $");
 #endif
 #endif /* not lint */
 
@@ -86,6 +86,7 @@
 uint64_t       progress = 0;           /* display sign of life */
 const u_char   *ctab;                  /* conversion table */
 sigset_t       infoset;                /* a set blocking SIGINFO */
+const char     *msgfmt = "posix";      /* default summary() message format */
 
 /*
  * Ops for stdin/stdout and crunch'd dd.  These are always host ops.
diff -r eaf9b5f52a08 -r 0489c8a53eb6 bin/dd/extern.h
--- a/bin/dd/extern.h   Sun Nov 06 20:21:05 2011 +0000
+++ b/bin/dd/extern.h   Sun Nov 06 21:22:23 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.20 2011/09/16 16:06:23 joerg Exp $        */
+/*     $NetBSD: extern.h,v 1.21 2011/11/06 21:22:23 jym Exp $  */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -74,3 +74,4 @@
 extern const u_char    e2a_32V[], e2a_POSIX[];
 extern const u_char    a2ibm_32V[], a2ibm_POSIX[];
 extern u_char          casetab[];
+extern const char      *msgfmt;
diff -r eaf9b5f52a08 -r 0489c8a53eb6 bin/dd/misc.c
--- a/bin/dd/misc.c     Sun Nov 06 20:21:05 2011 +0000
+++ b/bin/dd/misc.c     Sun Nov 06 21:22:23 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: misc.c,v 1.21 2007/10/05 07:23:09 lukem Exp $  */
+/*     $NetBSD: misc.c,v 1.22 2011/11/06 21:22:23 jym Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)misc.c     8.3 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: misc.c,v 1.21 2007/10/05 07:23:09 lukem Exp $");
+__RCSID("$NetBSD: misc.c,v 1.22 2011/11/06 21:22:23 jym Exp $");
 #endif
 #endif /* not lint */
 
@@ -59,9 +59,42 @@
 
 #define        tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000)
 
+static void posix_summary(void);
+#ifndef NO_MSGFMT
+static void custom_summary(void);
+static void human_summary(void);
+static void quiet_summary(void);
+
+static void buffer_write(const char *, size_t, int);
+static int  dd_write_msg(const char *);
+#endif /* NO_MSGFMT */
+
 void
 summary(void)
 {
+
+       if (progress)
+               (void)write(STDERR_FILENO, "\n", 1);
+



Home | Main Index | Thread Index | Old Index