Home | History | Annotate | Line # | Download | only in dd
misc.c revision 1.16
      1 /*	$NetBSD: misc.c,v 1.16 2003/08/07 09:05:10 agc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Keith Muller of the University of California, San Diego and Lance
      9  * Visser of Convex Computer Corporation.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)misc.c	8.3 (Berkeley) 4/2/94";
     40 #else
     41 __RCSID("$NetBSD: misc.c,v 1.16 2003/08/07 09:05:10 agc Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/types.h>
     47 #include <sys/time.h>
     48 
     49 #include <err.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 #include <inttypes.h>
     55 #include <vis.h>
     56 
     57 #include "dd.h"
     58 #include "extern.h"
     59 
     60 #define	tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000)
     61 
     62 void
     63 summary(void)
     64 {
     65 	char buf[100];
     66 	int64_t mS;
     67 	struct timeval tv;
     68 
     69 	if (progress)
     70 		(void)write(STDERR_FILENO, "\n", 1);
     71 
     72 	(void)gettimeofday(&tv, NULL);
     73 	mS = tv2mS(tv) - tv2mS(st.start);
     74 	if (mS == 0)
     75 		mS = 1;
     76 	/* Use snprintf(3) so that we don't reenter stdio(3). */
     77 	(void)snprintf(buf, sizeof(buf),
     78 	    "%llu+%llu records in\n%llu+%llu records out\n",
     79 	    (unsigned long long)st.in_full,  (unsigned long long)st.in_part,
     80 	    (unsigned long long)st.out_full, (unsigned long long)st.out_part);
     81 	(void)write(STDERR_FILENO, buf, strlen(buf));
     82 	if (st.swab) {
     83 		(void)snprintf(buf, sizeof(buf), "%llu odd length swab %s\n",
     84 		    (unsigned long long)st.swab,
     85 		    (st.swab == 1) ? "block" : "blocks");
     86 		(void)write(STDERR_FILENO, buf, strlen(buf));
     87 	}
     88 	if (st.trunc) {
     89 		(void)snprintf(buf, sizeof(buf), "%llu truncated %s\n",
     90 		    (unsigned long long)st.trunc,
     91 		    (st.trunc == 1) ? "block" : "blocks");
     92 		(void)write(STDERR_FILENO, buf, strlen(buf));
     93 	}
     94 	(void)snprintf(buf, sizeof(buf),
     95 	    "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n",
     96 	    (unsigned long long) st.bytes,
     97 	    (long) (mS / 1000),
     98 	    (int) (mS % 1000),
     99 	    (unsigned long long) (st.bytes * 1000LL / mS));
    100 	(void)write(STDERR_FILENO, buf, strlen(buf));
    101 }
    102 
    103 /* ARGSUSED */
    104 void
    105 summaryx(int notused)
    106 {
    107 
    108 	summary();
    109 }
    110 
    111 /* ARGSUSED */
    112 void
    113 terminate(int notused)
    114 {
    115 
    116 	exit(0);
    117 	/* NOTREACHED */
    118 }
    119 
    120 char *
    121 printescaped(const char *src)
    122 {
    123 	size_t len;
    124 	char *retval;
    125 
    126 	len = strlen(src);
    127 	if (len != 0 && SIZE_T_MAX/len <= 4) {
    128 		errx(EXIT_FAILURE, "%s: name too long", src);
    129 		/* NOTREACHED */
    130 	}
    131 
    132 	retval = (char *)malloc(4*len+1);
    133 	if (retval != NULL) {
    134 		if (isatty(STDOUT_FILENO))
    135 			(void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
    136 		else
    137 			(void)strcpy(retval, src);
    138 		return retval;
    139 	} else
    140 		errx(EXIT_FAILURE, "out of memory!");
    141 		/* NOTREACHED */
    142 }
    143