misc.c revision 330897
1/*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 * 4. 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. 22 * 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 33 * SUCH DAMAGE. 34 */ 35 36#ifndef lint 37#if 0 38static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94"; 39#endif 40#endif /* not lint */ 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: stable/11/bin/dd/misc.c 330897 2018-03-14 03:19:51Z eadler $"); 43 44#include <sys/types.h> 45 46#include <err.h> 47#include <errno.h> 48#include <inttypes.h> 49#include <signal.h> 50#include <stdio.h> 51#include <stdlib.h> 52#include <string.h> 53#include <time.h> 54#include <unistd.h> 55 56#include "dd.h" 57#include "extern.h" 58 59double 60secs_elapsed(void) 61{ 62 struct timespec end, ts_res; 63 double secs, res; 64 65 if (clock_gettime(CLOCK_MONOTONIC, &end)) 66 err(1, "clock_gettime"); 67 if (clock_getres(CLOCK_MONOTONIC, &ts_res)) 68 err(1, "clock_getres"); 69 secs = (end.tv_sec - st.start.tv_sec) + \ 70 (end.tv_nsec - st.start.tv_nsec) * 1e-9; 71 res = ts_res.tv_sec + ts_res.tv_nsec * 1e-9; 72 if (secs < res) 73 secs = res; 74 75 return (secs); 76} 77 78void 79summary(void) 80{ 81 double secs; 82 83 if (ddflags & C_NOINFO) 84 return; 85 86 secs = secs_elapsed(); 87 88 (void)fprintf(stderr, 89 "%ju+%ju records in\n%ju+%ju records out\n", 90 st.in_full, st.in_part, st.out_full, st.out_part); 91 if (st.swab) 92 (void)fprintf(stderr, "%ju odd length swab %s\n", 93 st.swab, (st.swab == 1) ? "block" : "blocks"); 94 if (st.trunc) 95 (void)fprintf(stderr, "%ju truncated %s\n", 96 st.trunc, (st.trunc == 1) ? "block" : "blocks"); 97 if (!(ddflags & C_NOXFER)) { 98 (void)fprintf(stderr, 99 "%ju bytes transferred in %.6f secs (%.0f bytes/sec)\n", 100 st.bytes, secs, st.bytes / secs); 101 } 102 need_summary = 0; 103} 104 105/* ARGSUSED */ 106void 107siginfo_handler(int signo __unused) 108{ 109 110 need_summary = 1; 111} 112 113/* ARGSUSED */ 114void 115terminate(int sig) 116{ 117 118 summary(); 119 _exit(sig == 0 ? 0 : 1); 120} 121