misc.c revision 1.19 1 /* $NetBSD: misc.c,v 1.19 2004/01/17 20:48:57 dbj 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.19 2004/01/17 20:48:57 dbj 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 <inttypes.h>
55
56 #include "dd.h"
57 #include "extern.h"
58
59 #define tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000)
60
61 void
62 summary(void)
63 {
64 char buf[100];
65 int64_t mS;
66 struct timeval tv;
67
68 if (progress)
69 (void)write(STDERR_FILENO, "\n", 1);
70
71 (void)gettimeofday(&tv, NULL);
72 mS = tv2mS(tv) - tv2mS(st.start);
73 if (mS == 0)
74 mS = 1;
75 /* Use snprintf(3) so that we don't reenter stdio(3). */
76 (void)snprintf(buf, sizeof(buf),
77 "%llu+%llu records in\n%llu+%llu records out\n",
78 (unsigned long long)st.in_full, (unsigned long long)st.in_part,
79 (unsigned long long)st.out_full, (unsigned long long)st.out_part);
80 (void)write(STDERR_FILENO, buf, strlen(buf));
81 if (st.swab) {
82 (void)snprintf(buf, sizeof(buf), "%llu odd length swab %s\n",
83 (unsigned long long)st.swab,
84 (st.swab == 1) ? "block" : "blocks");
85 (void)write(STDERR_FILENO, buf, strlen(buf));
86 }
87 if (st.trunc) {
88 (void)snprintf(buf, sizeof(buf), "%llu truncated %s\n",
89 (unsigned long long)st.trunc,
90 (st.trunc == 1) ? "block" : "blocks");
91 (void)write(STDERR_FILENO, buf, strlen(buf));
92 }
93 if (st.sparse) {
94 (void)snprintf(buf, sizeof(buf), "%llu sparse output %s\n",
95 (unsigned long long)st.sparse,
96 (st.sparse == 1) ? "block" : "blocks");
97 (void)write(STDERR_FILENO, buf, strlen(buf));
98 }
99 (void)snprintf(buf, sizeof(buf),
100 "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n",
101 (unsigned long long) st.bytes,
102 (long) (mS / 1000),
103 (int) (mS % 1000),
104 (unsigned long long) (st.bytes * 1000LL / mS));
105 (void)write(STDERR_FILENO, buf, strlen(buf));
106 }
107
108 /* ARGSUSED */
109 void
110 summaryx(int notused)
111 {
112
113 summary();
114 }
115
116 /* ARGSUSED */
117 void
118 terminate(int notused)
119 {
120
121 exit(0);
122 /* NOTREACHED */
123 }
124