xref: /original-bsd/bin/dd/misc.c (revision 68d9582f)
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  * %sccs.include.redist.c%
10  */
11 
12 #ifndef lint
13 static char sccsid[] = "@(#)misc.c	5.4 (Berkeley) 06/02/92";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include "dd.h"
21 #include "extern.h"
22 
23 /* ARGSUSED */
24 void
25 summary(notused)
26 	int notused;
27 {
28 	time_t secs;
29 	int len;
30 	char buf[100];
31 
32 	(void)time(&secs);
33 	if ((secs -= st.start) == 0)
34 		secs = 1;
35 	/* Use snprintf(3) so that we don't reenter stdio(3). */
36 	len = snprintf(buf, sizeof(buf),
37 	    "%u+%u records in\n%u+%u records out\n%u bytes transferred in %u secs (%u bytes/sec)\n",
38 	    st.in_full, st.in_part, st.out_full, st.out_part, st.bytes,
39 	    secs, st.bytes / secs);
40 	(void)write(STDERR_FILENO, buf, len);
41 	if (st.swab) {
42 		len = snprintf(buf, sizeof(buf), "%u odd length swab %s\n",
43 		     st.swab, (st.swab == 1) ? "block" : "blocks");
44 		(void)write(STDERR_FILENO, buf, len);
45 	}
46 	if (st.trunc) {
47 		len = snprintf(buf, sizeof(buf), "%u truncated %s\n",
48 		     st.trunc, (st.trunc == 1) ? "block" : "blocks");
49 		(void)write(STDERR_FILENO, buf, len);
50 	}
51 }
52 
53 /* ARGSUSED */
54 void
55 terminate(notused)
56 	int notused;
57 {
58 	summary(0);
59 	exit(0);
60 }
61 
62 #if __STDC__
63 #include <stdarg.h>
64 #else
65 #include <varargs.h>
66 #endif
67 
68 void
69 #if __STDC__
70 err(const char *fmt, ...)
71 #else
72 err(fmt, va_alist)
73 	char *fmt;
74 	va_dcl
75 #endif
76 {
77 	extern int errstats;
78 	va_list ap;
79 #if __STDC__
80 	va_start(ap, fmt);
81 #else
82 	va_start(ap);
83 #endif
84 	(void)fprintf(stderr, "dd: ");
85 	(void)vfprintf(stderr, fmt, ap);
86 	va_end(ap);
87 	(void)fprintf(stderr, "\n");
88 	if (errstats)
89 		summary(0);
90 	exit(1);
91 	/* NOTREACHED */
92 }
93 
94 void
95 #if __STDC__
96 warn(const char *fmt, ...)
97 #else
98 warn(fmt, va_alist)
99 	char *fmt;
100 	va_dcl
101 #endif
102 {
103 	va_list ap;
104 #if __STDC__
105 	va_start(ap, fmt);
106 #else
107 	va_start(ap);
108 #endif
109 	(void)fprintf(stderr, "dd: ");
110 	(void)vfprintf(stderr, fmt, ap);
111 	va_end(ap);
112 	(void)fprintf(stderr, "\n");
113 }
114