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