Bruce says: "You have been programming in the kernel for too long :-)."
and he's right ... I forgot about this floating point stuff you can use in user-land :-) Increase precision of duration to microseconds. No heuristics to avoid overflow in calculation needed - just depend on DBL_MAX being a bit larger than LONG_MAX. Use double instead of `struct timeval' in dd.h so that everything doesn't have to include <sys/time.h>. Fixed style bugs in recent and old FreeBSD changes. Reviewed by: phk Submitted by: bde
This commit is contained in:
parent
ac55eca46c
commit
ad66f7ee71
@ -34,7 +34,7 @@
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $Id: args.c,v 1.3 1994/09/24 02:54:42 davidg Exp $
|
||||
* $Id: args.c,v 1.4 1996/11/12 23:08:58 phk Exp $
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
@ -49,7 +49,6 @@ static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "dd.h"
|
||||
#include "extern.h"
|
||||
@ -101,7 +100,7 @@ jcl(argv)
|
||||
|
||||
in.dbsz = out.dbsz = 512;
|
||||
|
||||
while ((oper = *++argv)) {/* JEAG */
|
||||
while ((oper = *++argv) != NULL) {
|
||||
if ((arg = strchr(oper, '=')) == NULL)
|
||||
errx(1, "unknown operand %s", oper);
|
||||
*arg++ = '\0';
|
||||
|
@ -34,7 +34,7 @@
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $Id: conv.c,v 1.3 1994/09/24 02:54:44 davidg Exp $
|
||||
* $Id: conv.c,v 1.4 1996/11/12 23:09:04 phk Exp $
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
@ -45,7 +45,6 @@ static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
|
||||
|
||||
#include <err.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "dd.h"
|
||||
#include "extern.h"
|
||||
@ -62,7 +61,7 @@ def()
|
||||
int cnt;
|
||||
u_char *inp, *t;
|
||||
|
||||
if ((t = ctab))
|
||||
if ((t = ctab) != NULL)
|
||||
for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
|
||||
*inp = t[*inp];
|
||||
|
||||
@ -106,7 +105,6 @@ block()
|
||||
int ch, cnt, maxlen;
|
||||
u_char *inp, *outp, *t;
|
||||
|
||||
ch = 0;
|
||||
/*
|
||||
* Record truncation can cross block boundaries. If currently in a
|
||||
* truncation state, keep tossing characters until reach a newline.
|
||||
@ -131,9 +129,10 @@ block()
|
||||
* Copy records (max cbsz size chunks) into the output buffer. The
|
||||
* translation is done as we copy into the output buffer.
|
||||
*/
|
||||
ch = 0; /* Help the compiler. */
|
||||
for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
|
||||
maxlen = MIN(cbsz, in.dbcnt);
|
||||
if ((t = ctab))
|
||||
if ((t = ctab) != NULL)
|
||||
for (cnt = 0;
|
||||
cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
|
||||
*outp++ = t[ch];
|
||||
@ -217,7 +216,7 @@ unblock()
|
||||
u_char *inp, *t;
|
||||
|
||||
/* Translation and case conversion. */
|
||||
if ((t = ctab))
|
||||
if ((t = ctab) != NULL)
|
||||
for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
|
||||
*--inp = t[*inp];
|
||||
/*
|
||||
|
10
bin/dd/dd.c
10
bin/dd/dd.c
@ -34,7 +34,7 @@
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $Id: dd.c,v 1.5 1995/10/23 21:31:48 ache Exp $
|
||||
* $Id: dd.c,v 1.6 1996/11/12 23:09:09 phk Exp $
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
@ -56,12 +56,12 @@ static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <locale.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <locale.h>
|
||||
|
||||
#include "dd.h"
|
||||
#include "extern.h"
|
||||
@ -85,7 +85,7 @@ main(argc, argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
{
|
||||
(void) setlocale(LC_CTYPE, "");
|
||||
(void)setlocale(LC_CTYPE, "");
|
||||
jcl(argv);
|
||||
setup();
|
||||
|
||||
@ -105,6 +105,7 @@ static void
|
||||
setup()
|
||||
{
|
||||
u_int cnt;
|
||||
struct timeval tv;
|
||||
|
||||
if (in.name == NULL) {
|
||||
in.name = "stdin";
|
||||
@ -213,7 +214,8 @@ setup()
|
||||
ctab[cnt] = cnt;
|
||||
}
|
||||
}
|
||||
(void)gettimeofday(&st.start, 0); /* Statistics timestamp. */
|
||||
(void)gettimeofday(&tv, (struct timezone *)NULL);
|
||||
st.start = tv.tv_sec + tv.tv_usec * 1e-6;
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -35,7 +35,7 @@
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)dd.h 8.3 (Berkeley) 4/2/94
|
||||
* $Id: dd.h,v 1.2 1994/09/24 02:54:54 davidg Exp $
|
||||
* $Id: dd.h,v 1.3 1996/11/12 23:09:12 phk Exp $
|
||||
*/
|
||||
|
||||
/* Input/output stream state. */
|
||||
@ -70,7 +70,7 @@ typedef struct {
|
||||
u_long trunc; /* # of truncated records */
|
||||
u_long swab; /* # of odd-length swab blocks */
|
||||
u_long bytes; /* # of bytes written */
|
||||
struct timeval start; /* start time of dd */
|
||||
double start; /* start time of dd */
|
||||
} STAT;
|
||||
|
||||
/* Flags (in ddflags). */
|
||||
|
@ -34,7 +34,7 @@
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $Id: misc.c,v 1.2 1994/09/24 02:55:01 davidg Exp $
|
||||
* $Id: misc.c,v 1.3 1996/11/12 23:09:15 phk Exp $
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
@ -42,6 +42,7 @@ static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94";
|
||||
#endif /* not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include <err.h>
|
||||
#include <stdio.h>
|
||||
@ -49,7 +50,6 @@ static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94";
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "dd.h"
|
||||
#include "extern.h"
|
||||
@ -58,22 +58,13 @@ void
|
||||
summary()
|
||||
{
|
||||
struct timeval tv;
|
||||
long msec;
|
||||
double secs;
|
||||
char buf[100];
|
||||
|
||||
(void)gettimeofday(&tv, 0);
|
||||
tv.tv_sec -= st.start.tv_sec;
|
||||
tv.tv_usec -= st.start.tv_usec;
|
||||
if (tv.tv_usec < 0) {
|
||||
tv.tv_usec += 1000000;
|
||||
tv.tv_sec--;
|
||||
}
|
||||
|
||||
msec = tv.tv_sec * 1000;
|
||||
msec += tv.tv_usec / 1000;
|
||||
|
||||
if (msec == 0)
|
||||
msec = 1;
|
||||
(void)gettimeofday(&tv, (struct timezone *)NULL);
|
||||
secs = tv.tv_sec + tv.tv_usec * 1e-6 - st.start;
|
||||
if (secs < 1e-6)
|
||||
secs = 1e-6;
|
||||
/* Use snprintf(3) so that we don't reenter stdio(3). */
|
||||
(void)snprintf(buf, sizeof(buf),
|
||||
"%u+%u records in\n%u+%u records out\n",
|
||||
@ -89,15 +80,9 @@ summary()
|
||||
st.trunc, (st.trunc == 1) ? "block" : "blocks");
|
||||
(void)write(STDERR_FILENO, buf, strlen(buf));
|
||||
}
|
||||
if (msec > 1000000) {
|
||||
(void)snprintf(buf, sizeof(buf),
|
||||
"%u bytes transferred in %u.%03d secs (%u bytes/sec)\n",
|
||||
st.bytes, tv.tv_sec, 0, st.bytes / tv.tv_sec);
|
||||
} else {
|
||||
(void)snprintf(buf, sizeof(buf),
|
||||
"%u bytes transferred in %u.%03d secs (%u bytes/sec)\n",
|
||||
st.bytes, msec/1000, msec%1000, st.bytes*1000 / msec);
|
||||
}
|
||||
(void)snprintf(buf, sizeof(buf),
|
||||
"%u bytes transferred in %.6f secs (%.0f bytes/sec)\n",
|
||||
st.bytes, secs, st.bytes / secs);
|
||||
(void)write(STDERR_FILENO, buf, strlen(buf));
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user