1 /* $NetBSD: misc.c,v 1.23 2011/11/07 22:24:23 jym Exp $ */
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94";
41 __RCSID("$NetBSD: misc.c,v 1.23 2011/11/07 22:24:23 jym Exp $");
45 #include <sys/param.h>
46 #include <sys/types.h>
60 #define tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000)
62 static void posix_summary(void);
64 static void custom_summary(void);
65 static void human_summary(void);
66 static void quiet_summary(void);
68 static void buffer_write(const char *, size_t, int);
69 #endif /* NO_MSGFMT */
76 (void)write(STDERR_FILENO
, "\n", 1);
79 return posix_summary();
81 if (strncmp(msgfmt
, "human", sizeof("human")) == 0)
82 return human_summary();
84 if (strncmp(msgfmt
, "posix", sizeof("posix")) == 0)
85 return posix_summary();
87 if (strncmp(msgfmt
, "quiet", sizeof("quiet")) == 0)
88 return quiet_summary();
90 return custom_summary();
91 #endif /* NO_MSGFMT */
102 (void)write(STDERR_FILENO
, "\n", 1);
104 (void)gettimeofday(&tv
, NULL
);
105 mS
= tv2mS(tv
) - tv2mS(st
.start
);
109 /* Use snprintf(3) so that we don't reenter stdio(3). */
110 (void)snprintf(buf
, sizeof(buf
),
111 "%llu+%llu records in\n%llu+%llu records out\n",
112 (unsigned long long)st
.in_full
, (unsigned long long)st
.in_part
,
113 (unsigned long long)st
.out_full
, (unsigned long long)st
.out_part
);
114 (void)write(STDERR_FILENO
, buf
, strlen(buf
));
116 (void)snprintf(buf
, sizeof(buf
), "%llu odd length swab %s\n",
117 (unsigned long long)st
.swab
,
118 (st
.swab
== 1) ? "block" : "blocks");
119 (void)write(STDERR_FILENO
, buf
, strlen(buf
));
122 (void)snprintf(buf
, sizeof(buf
), "%llu truncated %s\n",
123 (unsigned long long)st
.trunc
,
124 (st
.trunc
== 1) ? "block" : "blocks");
125 (void)write(STDERR_FILENO
, buf
, strlen(buf
));
128 (void)snprintf(buf
, sizeof(buf
), "%llu sparse output %s\n",
129 (unsigned long long)st
.sparse
,
130 (st
.sparse
== 1) ? "block" : "blocks");
131 (void)write(STDERR_FILENO
, buf
, strlen(buf
));
133 (void)snprintf(buf
, sizeof(buf
),
134 "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n",
135 (unsigned long long) st
.bytes
,
138 (unsigned long long) (st
.bytes
* 1000LL / mS
));
139 (void)write(STDERR_FILENO
, buf
, strlen(buf
));
144 summaryx(int notused
)
156 (void)raise_default_signal(signo
);
162 * Buffer write(2) calls
165 buffer_write(const char *str
, size_t size
, int flush
)
167 static char wbuf
[128];
168 static size_t cnt
= 0; /* Internal counter to allow wbuf to wrap */
172 for (i
= 0; i
< size
; i
++) {
174 wbuf
[cnt
++] = str
[i
];
176 if (cnt
>= sizeof(wbuf
)) {
177 (void)write(STDERR_FILENO
, wbuf
, cnt
);
183 (void)write(STDERR_FILENO
, wbuf
, cnt
);
189 * Write summary to stderr according to format 'fmt'. If 'enable' is 0, it
190 * will not attempt to write anything. Can be used to validate the
191 * correctness of the 'fmt' string.
194 dd_write_msg(const char *fmt
, int enable
)
196 char hbuf
[7], nbuf
[32];
201 (void)gettimeofday(&tv
, NULL
);
202 mS
= tv2mS(tv
) - tv2mS(st
.start
);
206 #define ADDC(c) do { if (enable != 0) buffer_write(&c, 1, 0); } \
207 while (/*CONSTCOND*/0)
208 #define ADDS(p) do { if (enable != 0) buffer_write(p, strlen(p), 0); } \
209 while (/*CONSTCOND*/0)
211 for (ptr
= fmt
; *ptr
; ptr
++) {
219 (void)snprintf(nbuf
, sizeof(nbuf
), "%llu",
220 (unsigned long long)st
.bytes
);
224 if (humanize_number(hbuf
, sizeof(hbuf
),
226 HN_AUTOSCALE
, HN_DECIMAL
) == -1)
227 warnx("humanize_number (bytes transferred)");
231 (void)snprintf(nbuf
, sizeof(nbuf
), "%llu",
232 (unsigned long long) (st
.bytes
* 1000LL / mS
));
236 if (humanize_number(hbuf
, sizeof(hbuf
),
237 st
.bytes
* 1000LL / mS
, "B",
238 HN_AUTOSCALE
, HN_DECIMAL
) == -1)
239 warnx("humanize_number (bytes per second)");
240 ADDS(hbuf
); ADDS("/sec");
243 (void)snprintf(nbuf
, sizeof(nbuf
), "%llu",
244 (unsigned long long)st
.in_part
);
248 (void)snprintf(nbuf
, sizeof(nbuf
), "%llu",
249 (unsigned long long)st
.in_full
);
253 (void)snprintf(nbuf
, sizeof(nbuf
), "%llu",
254 (unsigned long long)st
.out_part
);
258 (void)snprintf(nbuf
, sizeof(nbuf
), "%llu",
259 (unsigned long long)st
.out_full
);
263 (void)snprintf(nbuf
, sizeof(nbuf
), "%li.%03d",
264 (long) (mS
/ 1000), (int) (mS
% 1000));
268 (void)snprintf(nbuf
, sizeof(nbuf
), "%llu",
269 (unsigned long long)st
.sparse
);
273 (void)snprintf(nbuf
, sizeof(nbuf
), "%llu",
274 (unsigned long long)st
.trunc
);
278 (void)snprintf(nbuf
, sizeof(nbuf
), "%llu",
279 (unsigned long long)st
.swab
);
284 if (st
.sparse
!= 1) ADDS("s");
288 if (st
.trunc
!= 1) ADDS("s");
292 if (st
.swab
!= 1) ADDS("s");
300 errx(EXIT_FAILURE
, "unknown specifier '%c' in "
301 "msgfmt string", *ptr
);
308 buffer_write(NULL
, 0, 1);
316 dd_write_msg(msgfmt
, 1);
322 (void)dd_write_msg("%I+%i records in\n%O+%o records out\n", 1);
324 (void)dd_write_msg("%w odd length swab %W\n", 1);
327 (void)dd_write_msg("%t truncated %T\n", 1);
330 (void)dd_write_msg("%p sparse output %P\n", 1);
332 (void)dd_write_msg("%b bytes (%B) transferred in %s secs "
333 "(%e bytes/sec - %E)\n", 1);
342 #endif /* NO_MSGFMT */