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