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