xref: /openbsd-src/bin/dd/misc.c (revision ba080721f3fe881c525a96bf32265471b142c251)
1*ba080721Sflorian /*	$OpenBSD: misc.c,v 1.26 2024/07/12 19:11:25 florian Exp $	*/
2df930be7Sderaadt /*	$NetBSD: misc.c,v 1.4 1995/03/21 09:04:10 cgd Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*-
5df930be7Sderaadt  * Copyright (c) 1991, 1993, 1994
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * This code is derived from software contributed to Berkeley by
9df930be7Sderaadt  * Keith Muller of the University of California, San Diego and Lance
10df930be7Sderaadt  * Visser of Convex Computer Corporation.
11df930be7Sderaadt  *
12df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
13df930be7Sderaadt  * modification, are permitted provided that the following conditions
14df930be7Sderaadt  * are met:
15df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
16df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
17df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
18df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
19df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
2029295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
21df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
22df930be7Sderaadt  *    without specific prior written permission.
23df930be7Sderaadt  *
24df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df930be7Sderaadt  * SUCH DAMAGE.
35df930be7Sderaadt  */
36df930be7Sderaadt 
37df930be7Sderaadt #include <sys/types.h>
3835fbec4fSmillert #include <sys/time.h>
39df930be7Sderaadt 
40b84e6683Sderaadt #include <errno.h>
41101d85deScheloha #include <stdio.h>
42df930be7Sderaadt #include <time.h>
43df930be7Sderaadt #include <unistd.h>
44df930be7Sderaadt 
45df930be7Sderaadt #include "dd.h"
46df930be7Sderaadt #include "extern.h"
47df930be7Sderaadt 
48f901c358Sderaadt /* SIGINFO handler */
49df930be7Sderaadt void
sig_summary(int notused)50f901c358Sderaadt sig_summary(int notused)
51df930be7Sderaadt {
52f901c358Sderaadt 	int save_errno = errno;
53c483b058Stedu 	struct timespec elapsed, now;
54*ba080721Sflorian 	unsigned long long bps, msec;
55df930be7Sderaadt 
561f29dce2Sbluhm 	if (ddflags & C_NOINFO)
571f29dce2Sbluhm 		return;
581f29dce2Sbluhm 
59c483b058Stedu 	clock_gettime(CLOCK_MONOTONIC, &now);
60c483b058Stedu 	timespecsub(&now, &st.start, &elapsed);
61*ba080721Sflorian 
62*ba080721Sflorian 	if (elapsed.tv_sec > 600)
63*ba080721Sflorian 		bps = st.bytes / elapsed.tv_sec;
64*ba080721Sflorian 	else if (elapsed.tv_sec > 0) {
65*ba080721Sflorian 		/* will overflow at ~ 30 exabytes / second */
66*ba080721Sflorian 		msec = elapsed.tv_sec * 1000 + elapsed.tv_nsec / 1000000;
67*ba080721Sflorian 		if (msec == 0)
68*ba080721Sflorian 			msec = 1;
69*ba080721Sflorian 		bps = st.bytes * 1000 / msec;
70*ba080721Sflorian 	} else if (elapsed.tv_nsec > 0)
71*ba080721Sflorian 		bps = st.bytes * 1000000000 / elapsed.tv_nsec;
72*ba080721Sflorian 	else
73*ba080721Sflorian 		bps = st.bytes;
74e4cb6409Shugh 
75101d85deScheloha 	/* Be async safe: use dprintf(3). */
76101d85deScheloha 	dprintf(STDERR_FILENO, "%zu+%zu records in\n%zu+%zu records out\n",
77df930be7Sderaadt 	    st.in_full, st.in_part, st.out_full, st.out_part);
78a5ae3385Sderaadt 
79df930be7Sderaadt 	if (st.swab) {
80101d85deScheloha 		dprintf(STDERR_FILENO, "%zu odd length swab %s\n",
81df930be7Sderaadt 		    st.swab, (st.swab == 1) ? "block" : "blocks");
82df930be7Sderaadt 	}
83df930be7Sderaadt 	if (st.trunc) {
84101d85deScheloha 		dprintf(STDERR_FILENO, "%zu truncated %s\n",
85df930be7Sderaadt 		    st.trunc, (st.trunc == 1) ? "block" : "blocks");
86df930be7Sderaadt 	}
871f29dce2Sbluhm 	if (!(ddflags & C_NOXFER)) {
88101d85deScheloha 		dprintf(STDERR_FILENO,
892af3a314Skrw 		    "%lld bytes transferred in %lld.%03ld secs "
90*ba080721Sflorian 		    "(%llu bytes/sec)\n", (long long)st.bytes,
91*ba080721Sflorian 		    (long long)elapsed.tv_sec, elapsed.tv_nsec / 1000000, bps);
921f29dce2Sbluhm 	}
93a3dc4760Sderaadt 	errno = save_errno;
94df930be7Sderaadt }
95df930be7Sderaadt 
96f901c358Sderaadt /* SIGINT handler */
97df930be7Sderaadt void
sig_terminate(int signo)98f901c358Sderaadt sig_terminate(int signo)
99df930be7Sderaadt {
100f901c358Sderaadt 	sig_summary(0);
101d88e47e1Sschwarze 	_exit(128 + signo);
102df930be7Sderaadt }
103f901c358Sderaadt 
104f901c358Sderaadt /* atexit variation to summarize */
105f901c358Sderaadt void
exit_summary(void)106f901c358Sderaadt exit_summary(void)
107f901c358Sderaadt {
108f901c358Sderaadt 	sig_summary(0);
109f901c358Sderaadt }
110