]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/dd/misc.c
Merge ^/head r337286 through r337585.
[FreeBSD/FreeBSD.git] / bin / dd / misc.c
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  * 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.
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
38 static char sccsid[] = "@(#)misc.c      8.3 (Berkeley) 4/2/94";
39 #endif
40 #endif /* not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
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
59 static int need_newline;
60
61 double
62 secs_elapsed(void)
63 {
64         struct timespec end, ts_res;
65         double secs, res;
66
67         if (clock_gettime(CLOCK_MONOTONIC, &end))
68                 err(1, "clock_gettime");
69         if (clock_getres(CLOCK_MONOTONIC, &ts_res))
70                 err(1, "clock_getres");
71         secs = (end.tv_sec - st.start.tv_sec) + \
72                (end.tv_nsec - st.start.tv_nsec) * 1e-9;
73         res = ts_res.tv_sec + ts_res.tv_nsec * 1e-9;
74         if (secs < res)
75                 secs = res;
76
77         return (secs);
78 }
79
80 void
81 summary(void)
82 {
83         double secs;
84
85         if (ddflags & C_NOINFO)
86                 return;
87
88         if (need_newline && !need_summary)
89                 fprintf(stderr, "\n");
90
91         secs = secs_elapsed();
92
93         (void)fprintf(stderr,
94             "%ju+%ju records in\n%ju+%ju records out\n",
95             st.in_full, st.in_part, st.out_full, st.out_part);
96         if (st.swab)
97                 (void)fprintf(stderr, "%ju odd length swab %s\n",
98                      st.swab, (st.swab == 1) ? "block" : "blocks");
99         if (st.trunc)
100                 (void)fprintf(stderr, "%ju truncated %s\n",
101                      st.trunc, (st.trunc == 1) ? "block" : "blocks");
102         if (!(ddflags & C_NOXFER)) {
103                 (void)fprintf(stderr,
104                     "%ju bytes transferred in %.6f secs (%.0f bytes/sec)\n",
105                     st.bytes, secs, st.bytes / secs);
106         }
107         need_summary = 0;
108 }
109
110 void
111 progress(void)
112 {
113         double secs;
114         static int lastlen;
115         int len;
116
117         secs = secs_elapsed();
118         len = fprintf(stderr,
119             "\r%ju bytes transferred in %.0f secs (%.0f bytes/sec)",
120             st.bytes, secs, st.bytes / secs);
121
122         if (len > 0) {
123                 if (len < lastlen)
124                         (void)fprintf(stderr, "%*s", len - lastlen, "");
125                 lastlen = len;
126         }
127
128         need_newline = 1;
129         need_progress = 0;
130 }
131
132 /* ARGSUSED */
133 void
134 siginfo_handler(int signo __unused)
135 {
136
137         need_summary = 1;
138 }
139
140 /* ARGSUSED */
141 void
142 sigalrm_handler(int signo __unused)
143 {
144
145         need_progress = 1;
146 }
147
148 /* ARGSUSED */
149 void
150 terminate(int sig)
151 {
152
153         summary();
154         _exit(sig == 0 ? 0 : 1);
155 }