xref: /netbsd-src/bin/dd/misc.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Keith Muller of the University of California, San Diego and Lance
7  * Visser of Convex Computer Corporation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #ifndef lint
39 /*static char sccsid[] = "from: @(#)misc.c	5.7 (Berkeley) 4/28/93";*/
40 static char rcsid[] = "$Id: misc.c,v 1.2 1993/08/01 19:00:07 mycroft Exp $";
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include "dd.h"
51 #include "extern.h"
52 
53 /* ARGSUSED */
54 void
55 summary(notused)
56 	int notused;
57 {
58 	time_t secs;
59 	char buf[100];
60 
61 	(void)time(&secs);
62 	if ((secs -= st.start) == 0)
63 		secs = 1;
64 	/* Use snprintf(3) so that we don't reenter stdio(3). */
65 	(void)snprintf(buf, sizeof(buf),
66 	    "%u+%u records in\n%u+%u records out\n",
67 	    st.in_full, st.in_part, st.out_full, st.out_part);
68 	(void)write(STDERR_FILENO, buf, strlen(buf));
69 	if (st.swab) {
70 		(void)snprintf(buf, sizeof(buf), "%u odd length swab %s\n",
71 		     st.swab, (st.swab == 1) ? "block" : "blocks");
72 		(void)write(STDERR_FILENO, buf, strlen(buf));
73 	}
74 	if (st.trunc) {
75 		(void)snprintf(buf, sizeof(buf), "%u truncated %s\n",
76 		     st.trunc, (st.trunc == 1) ? "block" : "blocks");
77 		(void)write(STDERR_FILENO, buf, strlen(buf));
78 	}
79 	(void)snprintf(buf, sizeof(buf),
80 	    "%u bytes transferred in %u secs (%u bytes/sec)\n",
81 	    st.bytes, secs, st.bytes / secs);
82 	(void)write(STDERR_FILENO, buf, strlen(buf));
83 }
84 
85 /* ARGSUSED */
86 void
87 terminate(notused)
88 	int notused;
89 {
90 	summary(0);
91 	exit(0);
92 }
93 
94 #if __STDC__
95 #include <stdarg.h>
96 #else
97 #include <varargs.h>
98 #endif
99 
100 void
101 #if __STDC__
102 err(const char *fmt, ...)
103 #else
104 err(fmt, va_alist)
105 	char *fmt;
106 	va_dcl
107 #endif
108 {
109 	extern int errstats;
110 	va_list ap;
111 #if __STDC__
112 	va_start(ap, fmt);
113 #else
114 	va_start(ap);
115 #endif
116 	(void)fprintf(stderr, "dd: ");
117 	(void)vfprintf(stderr, fmt, ap);
118 	va_end(ap);
119 	(void)fprintf(stderr, "\n");
120 	if (errstats)
121 		summary(0);
122 	exit(1);
123 	/* NOTREACHED */
124 }
125 
126 void
127 #if __STDC__
128 warn(const char *fmt, ...)
129 #else
130 warn(fmt, va_alist)
131 	char *fmt;
132 	va_dcl
133 #endif
134 {
135 	va_list ap;
136 #if __STDC__
137 	va_start(ap, fmt);
138 #else
139 	va_start(ap);
140 #endif
141 	(void)fprintf(stderr, "dd: ");
142 	(void)vfprintf(stderr, fmt, ap);
143 	va_end(ap);
144 	(void)fprintf(stderr, "\n");
145 }
146