xref: /csrg-svn/bin/dd/misc.c (revision 59464)
150567Sbostic /*-
250567Sbostic  * Copyright (c) 1991 The Regents of the University of California.
350567Sbostic  * All rights reserved.
450567Sbostic  *
550567Sbostic  * This code is derived from software contributed to Berkeley by
650588Sbostic  * Keith Muller of the University of California, San Diego and Lance
750588Sbostic  * Visser of Convex Computer Corporation.
850567Sbostic  *
950567Sbostic  * %sccs.include.redist.c%
1050567Sbostic  */
1150567Sbostic 
1250567Sbostic #ifndef lint
13*59464Sbostic static char sccsid[] = "@(#)misc.c	5.7 (Berkeley) 04/28/93";
1450567Sbostic #endif /* not lint */
1550567Sbostic 
1650567Sbostic #include <sys/types.h>
1759462Sbostic 
1859462Sbostic #include <stdio.h>
1959462Sbostic #include <stdlib.h>
2059462Sbostic #include <string.h>
2150567Sbostic #include <unistd.h>
2259462Sbostic 
2350567Sbostic #include "dd.h"
2450567Sbostic #include "extern.h"
2550567Sbostic 
2650567Sbostic /* ARGSUSED */
2750567Sbostic void
2850567Sbostic summary(notused)
2950567Sbostic 	int notused;
3050567Sbostic {
3153813Smarc 	time_t secs;
3250567Sbostic 	char buf[100];
3350567Sbostic 
3453813Smarc 	(void)time(&secs);
3553813Smarc 	if ((secs -= st.start) == 0)
3653813Smarc 		secs = 1;
3750567Sbostic 	/* Use snprintf(3) so that we don't reenter stdio(3). */
3856590Sbostic 	(void)snprintf(buf, sizeof(buf),
39*59464Sbostic 	    "%u+%u records in\n%u+%u records out\n",
40*59464Sbostic 	    st.in_full, st.in_part, st.out_full, st.out_part);
4156590Sbostic 	(void)write(STDERR_FILENO, buf, strlen(buf));
4250644Sbostic 	if (st.swab) {
4356590Sbostic 		(void)snprintf(buf, sizeof(buf), "%u odd length swab %s\n",
4450644Sbostic 		     st.swab, (st.swab == 1) ? "block" : "blocks");
4556590Sbostic 		(void)write(STDERR_FILENO, buf, strlen(buf));
4650644Sbostic 	}
4750644Sbostic 	if (st.trunc) {
4856590Sbostic 		(void)snprintf(buf, sizeof(buf), "%u truncated %s\n",
4950644Sbostic 		     st.trunc, (st.trunc == 1) ? "block" : "blocks");
5056590Sbostic 		(void)write(STDERR_FILENO, buf, strlen(buf));
5150567Sbostic 	}
52*59464Sbostic 	(void)snprintf(buf, sizeof(buf),
53*59464Sbostic 	    "%u bytes transferred in %u secs (%u bytes/sec)\n",
54*59464Sbostic 	    st.bytes, secs, st.bytes / secs);
55*59464Sbostic 	(void)write(STDERR_FILENO, buf, strlen(buf));
5650567Sbostic }
5750567Sbostic 
5850567Sbostic /* ARGSUSED */
5950567Sbostic void
6050567Sbostic terminate(notused)
6150567Sbostic 	int notused;
6250567Sbostic {
6350567Sbostic 	summary(0);
6450567Sbostic 	exit(0);
6550567Sbostic }
6650567Sbostic 
6750567Sbostic #if __STDC__
6850567Sbostic #include <stdarg.h>
6950567Sbostic #else
7050567Sbostic #include <varargs.h>
7150567Sbostic #endif
7250567Sbostic 
7350567Sbostic void
7450567Sbostic #if __STDC__
7550567Sbostic err(const char *fmt, ...)
7650567Sbostic #else
7750567Sbostic err(fmt, va_alist)
7850567Sbostic 	char *fmt;
7950567Sbostic 	va_dcl
8050567Sbostic #endif
8150567Sbostic {
8253813Smarc 	extern int errstats;
8350567Sbostic 	va_list ap;
8450567Sbostic #if __STDC__
8550567Sbostic 	va_start(ap, fmt);
8650567Sbostic #else
8750567Sbostic 	va_start(ap);
8850567Sbostic #endif
8950567Sbostic 	(void)fprintf(stderr, "dd: ");
9050567Sbostic 	(void)vfprintf(stderr, fmt, ap);
9150567Sbostic 	va_end(ap);
9250567Sbostic 	(void)fprintf(stderr, "\n");
9353813Smarc 	if (errstats)
9453813Smarc 		summary(0);
9550567Sbostic 	exit(1);
9650567Sbostic 	/* NOTREACHED */
9750567Sbostic }
9850567Sbostic 
9950567Sbostic void
10050567Sbostic #if __STDC__
10150567Sbostic warn(const char *fmt, ...)
10250567Sbostic #else
10350567Sbostic warn(fmt, va_alist)
10450567Sbostic 	char *fmt;
10550567Sbostic 	va_dcl
10650567Sbostic #endif
10750567Sbostic {
10850567Sbostic 	va_list ap;
10950567Sbostic #if __STDC__
11050567Sbostic 	va_start(ap, fmt);
11150567Sbostic #else
11250567Sbostic 	va_start(ap);
11350567Sbostic #endif
11450567Sbostic 	(void)fprintf(stderr, "dd: ");
11550567Sbostic 	(void)vfprintf(stderr, fmt, ap);
11650567Sbostic 	va_end(ap);
11750567Sbostic 	(void)fprintf(stderr, "\n");
11850567Sbostic }
119