misc.c revision 1.15 1 /* $NetBSD: misc.c,v 1.15 2003/08/04 22:31:23 jschauma 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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94";
44 #else
45 __RCSID("$NetBSD: misc.c,v 1.15 2003/08/04 22:31:23 jschauma Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/types.h>
51 #include <sys/time.h>
52
53 #include <err.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <inttypes.h>
59 #include <vis.h>
60
61 #include "dd.h"
62 #include "extern.h"
63
64 #define tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000)
65
66 void
67 summary(void)
68 {
69 char buf[100];
70 int64_t mS;
71 struct timeval tv;
72
73 if (progress)
74 (void)write(STDERR_FILENO, "\n", 1);
75
76 (void)gettimeofday(&tv, NULL);
77 mS = tv2mS(tv) - tv2mS(st.start);
78 if (mS == 0)
79 mS = 1;
80 /* Use snprintf(3) so that we don't reenter stdio(3). */
81 (void)snprintf(buf, sizeof(buf),
82 "%llu+%llu records in\n%llu+%llu records out\n",
83 (unsigned long long)st.in_full, (unsigned long long)st.in_part,
84 (unsigned long long)st.out_full, (unsigned long long)st.out_part);
85 (void)write(STDERR_FILENO, buf, strlen(buf));
86 if (st.swab) {
87 (void)snprintf(buf, sizeof(buf), "%llu odd length swab %s\n",
88 (unsigned long long)st.swab,
89 (st.swab == 1) ? "block" : "blocks");
90 (void)write(STDERR_FILENO, buf, strlen(buf));
91 }
92 if (st.trunc) {
93 (void)snprintf(buf, sizeof(buf), "%llu truncated %s\n",
94 (unsigned long long)st.trunc,
95 (st.trunc == 1) ? "block" : "blocks");
96 (void)write(STDERR_FILENO, buf, strlen(buf));
97 }
98 (void)snprintf(buf, sizeof(buf),
99 "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n",
100 (unsigned long long) st.bytes,
101 (long) (mS / 1000),
102 (int) (mS % 1000),
103 (unsigned long long) (st.bytes * 1000LL / mS));
104 (void)write(STDERR_FILENO, buf, strlen(buf));
105 }
106
107 /* ARGSUSED */
108 void
109 summaryx(int notused)
110 {
111
112 summary();
113 }
114
115 /* ARGSUSED */
116 void
117 terminate(int notused)
118 {
119
120 exit(0);
121 /* NOTREACHED */
122 }
123
124 char *
125 printescaped(const char *src)
126 {
127 size_t len;
128 char *retval;
129
130 len = strlen(src);
131 if (len != 0 && SIZE_T_MAX/len <= 4) {
132 errx(EXIT_FAILURE, "%s: name too long", src);
133 /* NOTREACHED */
134 }
135
136 retval = (char *)malloc(4*len+1);
137 if (retval != NULL) {
138 if (isatty(STDOUT_FILENO))
139 (void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
140 else
141 (void)strcpy(retval, src);
142 return retval;
143 } else
144 errx(EXIT_FAILURE, "out of memory!");
145 /* NOTREACHED */
146 }
147