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