misc.c revision 78985
1193323Sed/*-
2193323Sed * Copyright (c) 1991, 1993, 1994
3193323Sed *	The Regents of the University of California.  All rights reserved.
4193323Sed *
5193323Sed * This code is derived from software contributed to Berkeley by
6193323Sed * Keith Muller of the University of California, San Diego and Lance
7193323Sed * Visser of Convex Computer Corporation.
8193323Sed *
9193323Sed * Redistribution and use in source and binary forms, with or without
10193323Sed * modification, are permitted provided that the following conditions
11193323Sed * are met:
12193323Sed * 1. Redistributions of source code must retain the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer.
14193323Sed * 2. Redistributions in binary form must reproduce the above copyright
15249423Sdim *    notice, this list of conditions and the following disclaimer in the
16249423Sdim *    documentation and/or other materials provided with the distribution.
17218893Sdim * 3. All advertising materials mentioning features or use of this software
18193323Sed *    must display the following acknowledgement:
19193323Sed *	This product includes software developed by the University of
20198090Srdivacky *	California, Berkeley and its contributors.
21249423Sdim * 4. Neither the name of the University nor the names of its contributors
22249423Sdim *    may be used to endorse or promote products derived from this software
23249423Sdim *    without specific prior written permission.
24234353Sdim *
25203954Srdivacky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26208599Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27198090Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28198090Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35193323Sed * SUCH DAMAGE.
36218893Sdim */
37218893Sdim
38218893Sdim#ifndef lint
39193323Sed#if 0
40218893Sdimstatic char sccsid[] = "@(#)misc.c	8.3 (Berkeley) 4/2/94";
41218893Sdim#endif
42218893Sdimstatic const char rcsid[] =
43218893Sdim  "$FreeBSD: head/bin/dd/misc.c 78985 2001-06-29 20:06:47Z green $";
44193323Sed#endif /* not lint */
45193323Sed
46193323Sed#include <sys/types.h>
47193323Sed#include <sys/time.h>
48193323Sed
49193323Sed#include <errno.h>
50193323Sed#include <stdio.h>
51193323Sed#include <stdlib.h>
52193323Sed#include <strings.h>
53193323Sed#include <unistd.h>
54193323Sed
55193323Sed#include "dd.h"
56193323Sed#include "extern.h"
57193323Sed
58193323Sedvoid
59193323Sedsummary()
60198090Srdivacky{
61198090Srdivacky	struct timeval tv;
62198090Srdivacky	double secs;
63198090Srdivacky	char buf[100];
64198090Srdivacky
65193323Sed	(void)gettimeofday(&tv, (struct timezone *)NULL);
66198090Srdivacky	secs = tv.tv_sec + tv.tv_usec * 1e-6 - st.start;
67198090Srdivacky	if (secs < 1e-6)
68198090Srdivacky		secs = 1e-6;
69198090Srdivacky	/* Use snprintf(3) so that we don't reenter stdio(3). */
70193323Sed	(void)snprintf(buf, sizeof(buf),
71193323Sed	    "%qu+%qu records in\n%qu+%qu records out\n",
72193323Sed	    st.in_full, st.in_part, st.out_full, st.out_part);
73201360Srdivacky	(void)write(STDERR_FILENO, buf, strlen(buf));
74198090Srdivacky	if (st.swab) {
75198090Srdivacky		(void)snprintf(buf, sizeof(buf), "%qu odd length swab %s\n",
76198090Srdivacky		     st.swab, (st.swab == 1) ? "block" : "blocks");
77198090Srdivacky		(void)write(STDERR_FILENO, buf, strlen(buf));
78198090Srdivacky	}
79198090Srdivacky	if (st.trunc) {
80198090Srdivacky		(void)snprintf(buf, sizeof(buf), "%qu truncated %s\n",
81198090Srdivacky		     st.trunc, (st.trunc == 1) ? "block" : "blocks");
82198090Srdivacky		(void)write(STDERR_FILENO, buf, strlen(buf));
83198090Srdivacky	}
84198090Srdivacky	(void)snprintf(buf, sizeof(buf),
85198090Srdivacky	    "%qu bytes transferred in %.6f secs (%.0f bytes/sec)\n",
86198090Srdivacky	    st.bytes, secs, st.bytes / secs);
87206083Srdivacky	(void)write(STDERR_FILENO, buf, strlen(buf));
88226633Sdim}
89206083Srdivacky
90198090Srdivacky/* ARGSUSED */
91198090Srdivackyvoid
92198090Srdivackysummaryx(notused)
93198090Srdivacky	int notused __unused;
94198090Srdivacky{
95198090Srdivacky	int save_errno = errno;
96198090Srdivacky
97198090Srdivacky	summary();
98198090Srdivacky	errno = save_errno;
99198090Srdivacky}
100198090Srdivacky
101198090Srdivacky/* ARGSUSED */
102198090Srdivackyvoid
103198090Srdivackyterminate(sig)
104198090Srdivacky	int sig;
105198090Srdivacky{
106193323Sed
107193323Sed	summary();
108193323Sed	_exit(sig == 0 ? 0 : 1);
109193323Sed}
110206083Srdivacky