1 /**	$MirOS: src/bin/dd/misc.c,v 1.5 2007/08/08 11:47:18 tg Exp $ */
2 /*	$OpenBSD: misc.c,v 1.14 2005/05/27 04:14:24 millert Exp $	*/
3 /*	$NetBSD: misc.c,v 1.4 1995/03/21 09:04:10 cgd Exp $	*/
4 
5 /*-
6  * Copyright (c) 1991, 1993, 1994
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Keith Muller of the University of California, San Diego and Lance
11  * Visser of Convex Computer Corporation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/uio.h>
41 
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <errno.h>
47 #include <time.h>
48 #include <unistd.h>
49 
50 #include "dd.h"
51 #include "extern.h"
52 
53 __SCCSID("@(#)misc.c	8.3 (Berkeley) 4/2/94");
54 __RCSID("$MirOS: src/bin/dd/misc.c,v 1.5 2007/08/08 11:47:18 tg Exp $");
55 
56 void
summary(void)57 summary(void)
58 {
59 	struct timeval nowtv;
60 	char buf[4][100];
61 	struct iovec iov[4];
62 	double microsecs;
63 	int i = 0;
64 
65 	(void)gettimeofday(&nowtv, (struct timezone *)NULL);
66 	timersub(&nowtv, &st.startv, &nowtv);
67 	microsecs = ((double)nowtv.tv_sec * 1000000) + nowtv.tv_usec;
68 	if (microsecs == 0)
69 		microsecs = 1;
70 
71 	/* Use snprintf(3) so that we don't reenter stdio(3). */
72 	(void)snprintf(buf[0], sizeof(buf[0]),
73 	    "%zu+%zu records in\n%zu+%zu records out\n",
74 	    st.in_full, st.in_part, st.out_full, st.out_part);
75 	iov[i].iov_base = buf[0];
76 	iov[i++].iov_len = strlen(buf[0]);
77 
78 	if (st.swab) {
79 		(void)snprintf(buf[1], sizeof(buf[1]),
80 		    "%zu odd length swab %s\n",
81 		     st.swab, (st.swab == 1) ? "block" : "blocks");
82 		iov[i].iov_base = buf[1];
83 		iov[i++].iov_len = strlen(buf[1]);
84 	}
85 	if (st.trunc) {
86 		(void)snprintf(buf[2], sizeof(buf[2]),
87 		    "%zu truncated %s\n",
88 		     st.trunc, (st.trunc == 1) ? "block" : "blocks");
89 		iov[i].iov_base = buf[2];
90 		iov[i++].iov_len = strlen(buf[2]);
91 	}
92 	(void)snprintf(buf[3], sizeof(buf[3]),
93 	    "%qd bytes transferred in %lld.%03ld secs (%0.0f bytes/sec)\n",
94 	    (long long)st.bytes, (int64_t)nowtv.tv_sec, nowtv.tv_usec / 1000,
95 	    ((double)st.bytes * 1000000) / microsecs);
96 
97 	iov[i].iov_base = buf[3];
98 	iov[i++].iov_len = strlen(buf[3]);
99 
100 	(void)writev(STDERR_FILENO, iov, i);
101 }
102 
103 /* ARGSUSED */
104 void
summaryx(int notused)105 summaryx(int notused)
106 {
107 	int save_errno = errno;
108 
109 	summary();
110 	errno = save_errno;
111 }
112 
113 /* ARGSUSED */
114 void
terminate(int notused)115 terminate(int notused)
116 {
117 
118 	summary();
119 	_exit(0);
120 }
121