Home | History | Annotate | Line # | Download | only in dd
misc.c revision 1.22
      1 /*	$NetBSD: misc.c,v 1.22 2011/11/06 21:22:23 jym 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.22 2011/11/06 21:22:23 jym 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 <util.h>
     55 #include <inttypes.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 static void posix_summary(void);
     63 #ifndef NO_MSGFMT
     64 static void custom_summary(void);
     65 static void human_summary(void);
     66 static void quiet_summary(void);
     67 
     68 static void buffer_write(const char *, size_t, int);
     69 static int  dd_write_msg(const char *);
     70 #endif /* NO_MSGFMT */
     71 
     72 void
     73 summary(void)
     74 {
     75 
     76 	if (progress)
     77 		(void)write(STDERR_FILENO, "\n", 1);
     78 
     79 #ifdef NO_MSGFMT
     80 	return posix_summary();
     81 #else /* NO_MSGFMT */
     82 	if (strncmp(msgfmt, "human", sizeof("human")) == 0)
     83 		return human_summary();
     84 
     85 	if (strncmp(msgfmt, "posix", sizeof("posix")) == 0)
     86 		return posix_summary();
     87 
     88 	if (strncmp(msgfmt, "quiet", sizeof("quiet")) == 0)
     89 		return quiet_summary();
     90 
     91 	return custom_summary();
     92 #endif /* NO_MSGFMT */
     93 }
     94 
     95 static void
     96 posix_summary(void)
     97 {
     98 	char buf[100];
     99 	int64_t mS;
    100 	struct timeval tv;
    101 
    102 	if (progress)
    103 		(void)write(STDERR_FILENO, "\n", 1);
    104 
    105 	(void)gettimeofday(&tv, NULL);
    106 	mS = tv2mS(tv) - tv2mS(st.start);
    107 	if (mS == 0)
    108 		mS = 1;
    109 
    110 	/* Use snprintf(3) so that we don't reenter stdio(3). */
    111 	(void)snprintf(buf, sizeof(buf),
    112 	    "%llu+%llu records in\n%llu+%llu records out\n",
    113 	    (unsigned long long)st.in_full,  (unsigned long long)st.in_part,
    114 	    (unsigned long long)st.out_full, (unsigned long long)st.out_part);
    115 	(void)write(STDERR_FILENO, buf, strlen(buf));
    116 	if (st.swab) {
    117 		(void)snprintf(buf, sizeof(buf), "%llu odd length swab %s\n",
    118 		    (unsigned long long)st.swab,
    119 		    (st.swab == 1) ? "block" : "blocks");
    120 		(void)write(STDERR_FILENO, buf, strlen(buf));
    121 	}
    122 	if (st.trunc) {
    123 		(void)snprintf(buf, sizeof(buf), "%llu truncated %s\n",
    124 		    (unsigned long long)st.trunc,
    125 		    (st.trunc == 1) ? "block" : "blocks");
    126 		(void)write(STDERR_FILENO, buf, strlen(buf));
    127 	}
    128 	if (st.sparse) {
    129 		(void)snprintf(buf, sizeof(buf), "%llu sparse output %s\n",
    130 		    (unsigned long long)st.sparse,
    131 		    (st.sparse == 1) ? "block" : "blocks");
    132 		(void)write(STDERR_FILENO, buf, strlen(buf));
    133 	}
    134 	(void)snprintf(buf, sizeof(buf),
    135 	    "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n",
    136 	    (unsigned long long) st.bytes,
    137 	    (long) (mS / 1000),
    138 	    (int) (mS % 1000),
    139 	    (unsigned long long) (st.bytes * 1000LL / mS));
    140 	(void)write(STDERR_FILENO, buf, strlen(buf));
    141 }
    142 
    143 /* ARGSUSED */
    144 void
    145 summaryx(int notused)
    146 {
    147 
    148 	summary();
    149 }
    150 
    151 /* ARGSUSED */
    152 void
    153 terminate(int signo)
    154 {
    155 
    156 	summary();
    157 	(void)raise_default_signal(signo);
    158 	_exit(127);
    159 }
    160 
    161 #ifndef NO_MSGFMT
    162 /*
    163  * Buffer write(2) calls
    164  */
    165 static void
    166 buffer_write(const char *str, size_t size, int flush)
    167 {
    168 	static char wbuf[128];
    169 	static size_t cnt = 0; /* Internal counter to allow wbuf to wrap */
    170 
    171 	unsigned int i;
    172 
    173 	for (i = 0; i < size; i++) {
    174 		wbuf[cnt++] = str[i];
    175 		if (cnt >= sizeof(wbuf) || flush == 1) {
    176 			(void)write(STDERR_FILENO, wbuf, cnt);
    177 			cnt = 0;
    178 		}
    179 	}
    180 }
    181 
    182 static int
    183 dd_write_msg(const char *fmt)
    184 {
    185 	char hbuf[7], nbuf[32];
    186 	const char *ptr;
    187 	int64_t mS;
    188         struct timeval tv;
    189 
    190 	(void)gettimeofday(&tv, NULL);
    191 	mS = tv2mS(tv) - tv2mS(st.start);
    192 	if (mS == 0)
    193 		mS = 1;
    194 
    195 #define ADDC(c) do { buffer_write(&c, 1, 0); } \
    196 	while (/*CONSTCOND*/0)
    197 #define ADDS(p) do { buffer_write(p, strlen(p), 0); } \
    198 	while (/*CONSTCOND*/0)
    199 
    200 	for (ptr = fmt; *ptr; ptr++) {
    201 		if (*ptr != '%') {
    202 			ADDC(*ptr);
    203 			continue;
    204 		}
    205 
    206  		switch (*++ptr) {
    207 		case 'b':
    208 			(void)snprintf(nbuf, sizeof(nbuf), "%llu",
    209 			    (unsigned long long)st.bytes);
    210 			ADDS(nbuf);
    211 			break;
    212 		case 'B':
    213 			if (humanize_number(hbuf, sizeof(hbuf),
    214 			    st.bytes, "B",
    215 			    HN_AUTOSCALE, HN_DECIMAL) == -1)
    216 				warnx("humanize_number (bytes transferred)");
    217 			ADDS(hbuf);
    218 			break;
    219 		case 'e':
    220 			(void)snprintf(nbuf, sizeof(nbuf), "%llu",
    221 			    (unsigned long long) (st.bytes * 1000LL / mS));
    222 			ADDS(nbuf);
    223 			break;
    224 		case 'E':
    225 			if (humanize_number(hbuf, sizeof(hbuf),
    226 			    st.bytes * 1000LL / mS, "B",
    227 			    HN_AUTOSCALE, HN_DECIMAL) == -1)
    228 				warnx("humanize_number (bytes per second)");
    229 			ADDS(hbuf); ADDS("/sec");
    230 			break;
    231 		case 'i':
    232 			(void)snprintf(nbuf, sizeof(nbuf), "%llu",
    233 			    (unsigned long long)st.in_part);
    234 			ADDS(nbuf);
    235 			break;
    236 		case 'I':
    237 			(void)snprintf(nbuf, sizeof(nbuf), "%llu",
    238 			    (unsigned long long)st.in_full);
    239 			ADDS(nbuf);
    240 			break;
    241 		case 'o':
    242 			(void)snprintf(nbuf, sizeof(nbuf), "%llu",
    243 			    (unsigned long long)st.out_part);
    244 			ADDS(nbuf);
    245 			break;
    246 		case 'O':
    247 			(void)snprintf(nbuf, sizeof(nbuf), "%llu",
    248 			    (unsigned long long)st.out_full);
    249 			ADDS(nbuf);
    250 			break;
    251 		case 's':
    252 			(void)snprintf(nbuf, sizeof(nbuf), "%li.%03d",
    253 			    (long) (mS / 1000), (int) (mS % 1000));
    254 			ADDS(nbuf);
    255 			break;
    256 		case 'p':
    257 			(void)snprintf(nbuf, sizeof(nbuf), "%llu",
    258 			    (unsigned long long)st.sparse);
    259 			ADDS(nbuf);
    260 			break;
    261 		case 't':
    262 			(void)snprintf(nbuf, sizeof(nbuf), "%llu",
    263 			    (unsigned long long)st.trunc);
    264 			ADDS(nbuf);
    265 			break;
    266 		case 'w':
    267 			(void)snprintf(nbuf, sizeof(nbuf), "%llu",
    268 			    (unsigned long long)st.swab);
    269 			ADDS(nbuf);
    270 			break;
    271 		case 'P':
    272 			ADDS("block");
    273 			if (st.sparse != 1) ADDS("s");
    274 			break;
    275 		case 'T':
    276 			ADDS("block");
    277 			if (st.trunc != 1) ADDS("s");
    278 			break;
    279 		case 'W':
    280 			ADDS("block");
    281 			if (st.swab != 1) ADDS("s");
    282 			break;
    283 		default:
    284 			ADDS("%");
    285 			if (*ptr == '\0')
    286 				goto done;
    287 			/*FALLTHROUGH*/
    288 		case '%':
    289 			ADDC(*ptr);
    290 			break;
    291 		}
    292 	}
    293 
    294 done:
    295 	/* flush buffer */
    296 	buffer_write("\0", 1, 1);
    297 	return 0;
    298 }
    299 
    300 static void
    301 custom_summary(void)
    302 {
    303 
    304 	dd_write_msg(msgfmt);
    305 }
    306 
    307 static void
    308 human_summary(void)
    309 {
    310 	(void)dd_write_msg("%I+%i records in\n%O+%o records out\n");
    311 	if (st.swab) {
    312 		(void)dd_write_msg("%w odd length swab %W\n");
    313 	}
    314 	if (st.trunc) {
    315 		(void)dd_write_msg("%t truncated %T\n");
    316 	}
    317 	if (st.sparse) {
    318 		(void)dd_write_msg("%p sparse output %P\n");
    319 	}
    320 	(void)dd_write_msg("%b bytes (%B) transferred in %s secs "
    321 	    "(%e bytes/sec - %E)\n");
    322 }
    323 
    324 static void
    325 quiet_summary(void)
    326 {
    327 
    328 	/* stay quiet */
    329 }
    330 #endif /* NO_MSGFMT */
    331