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