xref: /freebsd/bin/dd/dd.c (revision 75e5511214e741fecba26bfc2d97ce035a509797)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego and Lance
74b88c807SRodney W. Grimes  * Visser of Convex Computer Corporation.
84b88c807SRodney W. Grimes  *
94b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
104b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
114b88c807SRodney W. Grimes  * are met:
124b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
144b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
164b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
174b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
184b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
194b88c807SRodney W. Grimes  *    without specific prior written permission.
204b88c807SRodney W. Grimes  *
214b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314b88c807SRodney W. Grimes  * SUCH DAMAGE.
324b88c807SRodney W. Grimes  */
334b88c807SRodney W. Grimes 
3409a80d48SDavid E. O'Brien #if 0
354b88c807SRodney W. Grimes #ifndef lint
3678b09ffeSSteve Price static char const copyright[] =
374b88c807SRodney W. Grimes "@(#) Copyright (c) 1991, 1993, 1994\n\
384b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
394b88c807SRodney W. Grimes #endif /* not lint */
404b88c807SRodney W. Grimes 
414b88c807SRodney W. Grimes #ifndef lint
421ba0e048SPhilippe Charnier static char sccsid[] = "@(#)dd.c	8.5 (Berkeley) 4/2/94";
434b88c807SRodney W. Grimes #endif /* not lint */
4409a80d48SDavid E. O'Brien #endif
455eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
465eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$");
474b88c807SRodney W. Grimes 
484b88c807SRodney W. Grimes #include <sys/param.h>
494b88c807SRodney W. Grimes #include <sys/stat.h>
50e08d7384SBrian Feldman #include <sys/conf.h>
51015a53cfSDavid E. O'Brien #include <sys/disklabel.h>
52e08d7384SBrian Feldman #include <sys/filio.h>
534b88c807SRodney W. Grimes 
54aa7a161bSThomas Quinot #include <assert.h>
554b88c807SRodney W. Grimes #include <ctype.h>
564b88c807SRodney W. Grimes #include <err.h>
574b88c807SRodney W. Grimes #include <errno.h>
584b88c807SRodney W. Grimes #include <fcntl.h>
597503d74fSMark Murray #include <inttypes.h>
60ad66f7eeSPoul-Henning Kamp #include <locale.h>
614b88c807SRodney W. Grimes #include <stdio.h>
624b88c807SRodney W. Grimes #include <stdlib.h>
634b88c807SRodney W. Grimes #include <string.h>
64d1d66eacSAlan Somers #include <time.h>
654b88c807SRodney W. Grimes #include <unistd.h>
664b88c807SRodney W. Grimes 
674b88c807SRodney W. Grimes #include "dd.h"
684b88c807SRodney W. Grimes #include "extern.h"
694b88c807SRodney W. Grimes 
70f9bcb0beSWarner Losh static void dd_close(void);
71f9bcb0beSWarner Losh static void dd_in(void);
72f9bcb0beSWarner Losh static void getfdtype(IO *);
73f9bcb0beSWarner Losh static void setup(void);
744b88c807SRodney W. Grimes 
754b88c807SRodney W. Grimes IO	in, out;		/* input/output state */
764b88c807SRodney W. Grimes STAT	st;			/* statistics */
77f9bcb0beSWarner Losh void	(*cfunc)(void);		/* conversion function */
787503d74fSMark Murray uintmax_t cpy_cnt;		/* # of blocks to copy */
799afa09cdSMark Murray static off_t	pending = 0;	/* pending seek if sparse */
80413ef2a3SXin LI u_int	ddflags = 0;		/* conversion options */
8158687472SBrian Feldman size_t	cbsz;			/* conversion block size */
827503d74fSMark Murray uintmax_t files_cnt = 1;	/* # of files to copy */
8358687472SBrian Feldman const	u_char *ctab;		/* conversion table */
84e3edab4aSRobert Watson char	fill_char;		/* Character to fill with if defined */
85*75e55112SEdward Tomasz Napierala size_t	speed = 0;		/* maximum speed, in bytes per second */
864ac11639SEitan Adler volatile sig_atomic_t need_summary;
874b88c807SRodney W. Grimes 
884b88c807SRodney W. Grimes int
89f9bcb0beSWarner Losh main(int argc __unused, char *argv[])
904b88c807SRodney W. Grimes {
91f7c627b2SAndrey A. Chernov 	(void)setlocale(LC_CTYPE, "");
924b88c807SRodney W. Grimes 	jcl(argv);
934b88c807SRodney W. Grimes 	setup();
944b88c807SRodney W. Grimes 
954ac11639SEitan Adler 	(void)signal(SIGINFO, siginfo_handler);
964b88c807SRodney W. Grimes 	(void)signal(SIGINT, terminate);
974b88c807SRodney W. Grimes 
984b88c807SRodney W. Grimes 	atexit(summary);
994b88c807SRodney W. Grimes 
1004b88c807SRodney W. Grimes 	while (files_cnt--)
1014b88c807SRodney W. Grimes 		dd_in();
1024b88c807SRodney W. Grimes 
1034b88c807SRodney W. Grimes 	dd_close();
104c183a03bSBrooks Davis 	/*
105c183a03bSBrooks Davis 	 * Some devices such as cfi(4) may perform significant amounts
106c183a03bSBrooks Davis 	 * of work when a write descriptor is closed.  Close the out
107c183a03bSBrooks Davis 	 * descriptor explicitly so that the summary handler (called
108c183a03bSBrooks Davis 	 * from an atexit() hook) includes this work.
109c183a03bSBrooks Davis 	 */
110c183a03bSBrooks Davis 	close(out.fd);
1114b88c807SRodney W. Grimes 	exit(0);
1124b88c807SRodney W. Grimes }
1134b88c807SRodney W. Grimes 
1146a3d33acSPoul-Henning Kamp static int
1156a3d33acSPoul-Henning Kamp parity(u_char c)
1166a3d33acSPoul-Henning Kamp {
1176a3d33acSPoul-Henning Kamp 	int i;
1186a3d33acSPoul-Henning Kamp 
1196a3d33acSPoul-Henning Kamp 	i = c ^ (c >> 1) ^ (c >> 2) ^ (c >> 3) ^
1206a3d33acSPoul-Henning Kamp 	    (c >> 4) ^ (c >> 5) ^ (c >> 6) ^ (c >> 7);
1216a3d33acSPoul-Henning Kamp 	return (i & 1);
1226a3d33acSPoul-Henning Kamp }
1236a3d33acSPoul-Henning Kamp 
1244b88c807SRodney W. Grimes static void
125f9bcb0beSWarner Losh setup(void)
1264b88c807SRodney W. Grimes {
1274b88c807SRodney W. Grimes 	u_int cnt;
1284b88c807SRodney W. Grimes 
1294b88c807SRodney W. Grimes 	if (in.name == NULL) {
1304b88c807SRodney W. Grimes 		in.name = "stdin";
1314b88c807SRodney W. Grimes 		in.fd = STDIN_FILENO;
1324b88c807SRodney W. Grimes 	} else {
13354946e00SBrian Feldman 		in.fd = open(in.name, O_RDONLY, 0);
134767bc8adSBrian Feldman 		if (in.fd == -1)
1354b88c807SRodney W. Grimes 			err(1, "%s", in.name);
1364b88c807SRodney W. Grimes 	}
1374b88c807SRodney W. Grimes 
1384b88c807SRodney W. Grimes 	getfdtype(&in);
1394b88c807SRodney W. Grimes 
1404b88c807SRodney W. Grimes 	if (files_cnt > 1 && !(in.flags & ISTAPE))
1414b88c807SRodney W. Grimes 		errx(1, "files is not supported for non-tape devices");
1424b88c807SRodney W. Grimes 
1434b88c807SRodney W. Grimes 	if (out.name == NULL) {
1444b88c807SRodney W. Grimes 		/* No way to check for read access here. */
1454b88c807SRodney W. Grimes 		out.fd = STDOUT_FILENO;
1464b88c807SRodney W. Grimes 		out.name = "stdout";
1474b88c807SRodney W. Grimes 	} else {
1484b88c807SRodney W. Grimes #define	OFLAGS \
1494b88c807SRodney W. Grimes     (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
1504b88c807SRodney W. Grimes 		out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
1514b88c807SRodney W. Grimes 		/*
1524b88c807SRodney W. Grimes 		 * May not have read access, so try again with write only.
1534b88c807SRodney W. Grimes 		 * Without read we may have a problem if output also does
1544b88c807SRodney W. Grimes 		 * not support seeks.
1554b88c807SRodney W. Grimes 		 */
156767bc8adSBrian Feldman 		if (out.fd == -1) {
1574b88c807SRodney W. Grimes 			out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
1584b88c807SRodney W. Grimes 			out.flags |= NOREAD;
1594b88c807SRodney W. Grimes 		}
160767bc8adSBrian Feldman 		if (out.fd == -1)
1614b88c807SRodney W. Grimes 			err(1, "%s", out.name);
1624b88c807SRodney W. Grimes 	}
1634b88c807SRodney W. Grimes 
1644b88c807SRodney W. Grimes 	getfdtype(&out);
1654b88c807SRodney W. Grimes 
1664b88c807SRodney W. Grimes 	/*
1674b88c807SRodney W. Grimes 	 * Allocate space for the input and output buffers.  If not doing
1684b88c807SRodney W. Grimes 	 * record oriented I/O, only need a single buffer.
1694b88c807SRodney W. Grimes 	 */
1704b88c807SRodney W. Grimes 	if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
171f4cfd28bSKurt Jaeger 		if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
17258687472SBrian Feldman 			err(1, "input buffer");
1734b88c807SRodney W. Grimes 		out.db = in.db;
174f4cfd28bSKurt Jaeger 	} else if ((in.db = malloc(MAX(in.dbsz, cbsz) + cbsz)) == NULL ||
17558687472SBrian Feldman 	    (out.db = malloc(out.dbsz + cbsz)) == NULL)
17658687472SBrian Feldman 		err(1, "output buffer");
177aa7a161bSThomas Quinot 
178aa7a161bSThomas Quinot 	/* dbp is the first free position in each buffer. */
1794b88c807SRodney W. Grimes 	in.dbp = in.db;
1804b88c807SRodney W. Grimes 	out.dbp = out.db;
1814b88c807SRodney W. Grimes 
1824b88c807SRodney W. Grimes 	/* Position the input/output streams. */
1834b88c807SRodney W. Grimes 	if (in.offset)
1844b88c807SRodney W. Grimes 		pos_in();
1854b88c807SRodney W. Grimes 	if (out.offset)
1864b88c807SRodney W. Grimes 		pos_out();
1874b88c807SRodney W. Grimes 
1884b88c807SRodney W. Grimes 	/*
1895976ee7eSBrian Feldman 	 * Truncate the output file.  If it fails on a type of output file
1905976ee7eSBrian Feldman 	 * that it should _not_ fail on, error out.
1914b88c807SRodney W. Grimes 	 */
192c15c898eSBrian Feldman 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
193c15c898eSBrian Feldman 	    out.flags & ISTRUNC)
194c15c898eSBrian Feldman 		if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
195c15c898eSBrian Feldman 			err(1, "truncating %s", out.name);
1964b88c807SRodney W. Grimes 
1976a3d33acSPoul-Henning Kamp 	if (ddflags & (C_LCASE  | C_UCASE | C_ASCII | C_EBCDIC | C_PARITY)) {
1986a3d33acSPoul-Henning Kamp 		if (ctab != NULL) {
1996a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2006a3d33acSPoul-Henning Kamp 				casetab[cnt] = ctab[cnt];
2016a3d33acSPoul-Henning Kamp 		} else {
2026a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2036a3d33acSPoul-Henning Kamp 				casetab[cnt] = cnt;
2046a3d33acSPoul-Henning Kamp 		}
2056a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARITY) && !(ddflags & C_ASCII)) {
2064b88c807SRodney W. Grimes 			/*
2076a3d33acSPoul-Henning Kamp 			 * If the input is not EBCDIC, and we do parity
2086a3d33acSPoul-Henning Kamp 			 * processing, strip input parity.
2094b88c807SRodney W. Grimes 			 */
2106a3d33acSPoul-Henning Kamp 			for (cnt = 200; cnt <= 0377; ++cnt)
2116a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt & 0x7f];
2126a3d33acSPoul-Henning Kamp 		}
2134b88c807SRodney W. Grimes 		if (ddflags & C_LCASE) {
214c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2156a3d33acSPoul-Henning Kamp 				casetab[cnt] = tolower(casetab[cnt]);
2166a3d33acSPoul-Henning Kamp 		} else if (ddflags & C_UCASE) {
217c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2186a3d33acSPoul-Henning Kamp 				casetab[cnt] = toupper(casetab[cnt]);
2194b88c807SRodney W. Grimes 		}
2206a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARITY)) {
2216a3d33acSPoul-Henning Kamp 			/*
2226a3d33acSPoul-Henning Kamp 			 * This should strictly speaking be a no-op, but I
2236a3d33acSPoul-Henning Kamp 			 * wonder what funny LANG settings could get us.
2246a3d33acSPoul-Henning Kamp 			 */
225c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2266a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt] & 0x7f;
2276a3d33acSPoul-Henning Kamp 		}
2286a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARSET)) {
229c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2306a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt] | 0x80;
231dde07463SAndrey A. Chernov 		}
2326a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PAREVEN)) {
2336a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2346a3d33acSPoul-Henning Kamp 				if (parity(casetab[cnt]))
2356a3d33acSPoul-Henning Kamp 					casetab[cnt] = casetab[cnt] | 0x80;
236dde07463SAndrey A. Chernov 		}
2376a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARODD)) {
2386a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2396a3d33acSPoul-Henning Kamp 				if (!parity(casetab[cnt]))
2406a3d33acSPoul-Henning Kamp 					casetab[cnt] = casetab[cnt] | 0x80;
2416a3d33acSPoul-Henning Kamp 		}
2426a3d33acSPoul-Henning Kamp 
24358687472SBrian Feldman 		ctab = casetab;
244426e9c1dSWarner Losh 	}
24558687472SBrian Feldman 
246540c7825SAlan Somers 	if (clock_gettime(CLOCK_MONOTONIC, &st.start))
247540c7825SAlan Somers 		err(1, "clock_gettime");
2484b88c807SRodney W. Grimes }
2494b88c807SRodney W. Grimes 
2504b88c807SRodney W. Grimes static void
251f9bcb0beSWarner Losh getfdtype(IO *io)
2524b88c807SRodney W. Grimes {
2534b88c807SRodney W. Grimes 	struct stat sb;
254e08d7384SBrian Feldman 	int type;
2554b88c807SRodney W. Grimes 
25658687472SBrian Feldman 	if (fstat(io->fd, &sb) == -1)
2574b88c807SRodney W. Grimes 		err(1, "%s", io->name);
258c15c898eSBrian Feldman 	if (S_ISREG(sb.st_mode))
259c15c898eSBrian Feldman 		io->flags |= ISTRUNC;
260e08d7384SBrian Feldman 	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
261ff8bb989SBrian Feldman 		if (ioctl(io->fd, FIODTYPE, &type) == -1) {
262bf3367d0SBrian Feldman 			err(1, "%s", io->name);
263ff8bb989SBrian Feldman 		} else {
264e08d7384SBrian Feldman 			if (type & D_TAPE)
265e08d7384SBrian Feldman 				io->flags |= ISTAPE;
266cd967e32SPoul-Henning Kamp 			else if (type & (D_DISK | D_MEM))
26732952d4bSBrian Feldman 				io->flags |= ISSEEK;
268e08d7384SBrian Feldman 			if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
269e08d7384SBrian Feldman 				io->flags |= ISCHR;
270ff8bb989SBrian Feldman 		}
2715ff6541eSBrian Feldman 		return;
2725ff6541eSBrian Feldman 	}
2735ff6541eSBrian Feldman 	errno = 0;
2745ff6541eSBrian Feldman 	if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
275e08d7384SBrian Feldman 		io->flags |= ISPIPE;
2765ff6541eSBrian Feldman 	else
2775ff6541eSBrian Feldman 		io->flags |= ISSEEK;
2784b88c807SRodney W. Grimes }
2794b88c807SRodney W. Grimes 
280*75e55112SEdward Tomasz Napierala /*
281*75e55112SEdward Tomasz Napierala  * Limit the speed by adding a delay before every block read.
282*75e55112SEdward Tomasz Napierala  * The delay (t_usleep) is equal to the time computed from block
283*75e55112SEdward Tomasz Napierala  * size and the specified speed limit (t_target) minus the time
284*75e55112SEdward Tomasz Napierala  * spent on actual read and write operations (t_io).
285*75e55112SEdward Tomasz Napierala  */
286*75e55112SEdward Tomasz Napierala static void
287*75e55112SEdward Tomasz Napierala speed_limit(void)
288*75e55112SEdward Tomasz Napierala {
289*75e55112SEdward Tomasz Napierala 	static double t_prev, t_usleep;
290*75e55112SEdward Tomasz Napierala 	double t_now, t_io, t_target;
291*75e55112SEdward Tomasz Napierala 
292*75e55112SEdward Tomasz Napierala 	t_now = secs_elapsed();
293*75e55112SEdward Tomasz Napierala 	t_io = t_now - t_prev - t_usleep;
294*75e55112SEdward Tomasz Napierala 	t_target = (double)in.dbsz / (double)speed;
295*75e55112SEdward Tomasz Napierala 	t_usleep = t_target - t_io;
296*75e55112SEdward Tomasz Napierala 	if (t_usleep > 0)
297*75e55112SEdward Tomasz Napierala 		usleep(t_usleep * 1000000);
298*75e55112SEdward Tomasz Napierala 	else
299*75e55112SEdward Tomasz Napierala 		t_usleep = 0;
300*75e55112SEdward Tomasz Napierala 	t_prev = t_now;
301*75e55112SEdward Tomasz Napierala }
302*75e55112SEdward Tomasz Napierala 
3034b88c807SRodney W. Grimes static void
304f9bcb0beSWarner Losh dd_in(void)
3054b88c807SRodney W. Grimes {
306767bc8adSBrian Feldman 	ssize_t n;
3074b88c807SRodney W. Grimes 
308af041bf3SJonathan Lemon 	for (;;) {
3095ff6541eSBrian Feldman 		switch (cpy_cnt) {
3105ff6541eSBrian Feldman 		case -1:			/* count=0 was specified */
3114b88c807SRodney W. Grimes 			return;
3125ff6541eSBrian Feldman 		case 0:
3135ff6541eSBrian Feldman 			break;
3145ff6541eSBrian Feldman 		default:
3157503d74fSMark Murray 			if (st.in_full + st.in_part >= (uintmax_t)cpy_cnt)
3165ff6541eSBrian Feldman 				return;
3175ff6541eSBrian Feldman 			break;
3185ff6541eSBrian Feldman 		}
3194b88c807SRodney W. Grimes 
320*75e55112SEdward Tomasz Napierala 		if (speed > 0)
321*75e55112SEdward Tomasz Napierala 			speed_limit();
322*75e55112SEdward Tomasz Napierala 
3234b88c807SRodney W. Grimes 		/*
32458687472SBrian Feldman 		 * Zero the buffer first if sync; if doing block operations,
3254b88c807SRodney W. Grimes 		 * use spaces.
3264b88c807SRodney W. Grimes 		 */
327767bc8adSBrian Feldman 		if (ddflags & C_SYNC) {
328e3edab4aSRobert Watson 			if (ddflags & C_FILL)
329e3edab4aSRobert Watson 				memset(in.dbp, fill_char, in.dbsz);
330e3edab4aSRobert Watson 			else if (ddflags & (C_BLOCK | C_UNBLOCK))
3314b88c807SRodney W. Grimes 				memset(in.dbp, ' ', in.dbsz);
3324b88c807SRodney W. Grimes 			else
3334b88c807SRodney W. Grimes 				memset(in.dbp, 0, in.dbsz);
334767bc8adSBrian Feldman 		}
3354b88c807SRodney W. Grimes 
3364b88c807SRodney W. Grimes 		n = read(in.fd, in.dbp, in.dbsz);
3374b88c807SRodney W. Grimes 		if (n == 0) {
3384b88c807SRodney W. Grimes 			in.dbrcnt = 0;
3394b88c807SRodney W. Grimes 			return;
3404b88c807SRodney W. Grimes 		}
3414b88c807SRodney W. Grimes 
3424b88c807SRodney W. Grimes 		/* Read error. */
343767bc8adSBrian Feldman 		if (n == -1) {
3444b88c807SRodney W. Grimes 			/*
3454b88c807SRodney W. Grimes 			 * If noerror not specified, die.  POSIX requires that
3464b88c807SRodney W. Grimes 			 * the warning message be followed by an I/O display.
3474b88c807SRodney W. Grimes 			 */
348af041bf3SJonathan Lemon 			if (!(ddflags & C_NOERROR))
3494b88c807SRodney W. Grimes 				err(1, "%s", in.name);
3504b88c807SRodney W. Grimes 			warn("%s", in.name);
3514b88c807SRodney W. Grimes 			summary();
3524b88c807SRodney W. Grimes 
3534b88c807SRodney W. Grimes 			/*
3547599187eSBrian Feldman 			 * If it's a seekable file descriptor, seek past the
3554b88c807SRodney W. Grimes 			 * error.  If your OS doesn't do the right thing for
3564b88c807SRodney W. Grimes 			 * raw disks this section should be modified to re-read
3574b88c807SRodney W. Grimes 			 * in sector size chunks.
3584b88c807SRodney W. Grimes 			 */
3597599187eSBrian Feldman 			if (in.flags & ISSEEK &&
3604b88c807SRodney W. Grimes 			    lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
3614b88c807SRodney W. Grimes 				warn("%s", in.name);
3624b88c807SRodney W. Grimes 
3634b88c807SRodney W. Grimes 			/* If sync not specified, omit block and continue. */
3644b88c807SRodney W. Grimes 			if (!(ddflags & C_SYNC))
3654b88c807SRodney W. Grimes 				continue;
3664b88c807SRodney W. Grimes 
3674b88c807SRodney W. Grimes 			/* Read errors count as full blocks. */
3684b88c807SRodney W. Grimes 			in.dbcnt += in.dbrcnt = in.dbsz;
3694b88c807SRodney W. Grimes 			++st.in_full;
3704b88c807SRodney W. Grimes 
3714b88c807SRodney W. Grimes 		/* Handle full input blocks. */
372f4cfd28bSKurt Jaeger 		} else if ((size_t)n == in.dbsz) {
3734b88c807SRodney W. Grimes 			in.dbcnt += in.dbrcnt = n;
3744b88c807SRodney W. Grimes 			++st.in_full;
3754b88c807SRodney W. Grimes 
3764b88c807SRodney W. Grimes 		/* Handle partial input blocks. */
3774b88c807SRodney W. Grimes 		} else {
3784b88c807SRodney W. Grimes 			/* If sync, use the entire block. */
3794b88c807SRodney W. Grimes 			if (ddflags & C_SYNC)
3804b88c807SRodney W. Grimes 				in.dbcnt += in.dbrcnt = in.dbsz;
3814b88c807SRodney W. Grimes 			else
3824b88c807SRodney W. Grimes 				in.dbcnt += in.dbrcnt = n;
3834b88c807SRodney W. Grimes 			++st.in_part;
3844b88c807SRodney W. Grimes 		}
3854b88c807SRodney W. Grimes 
3864b88c807SRodney W. Grimes 		/*
3874b88c807SRodney W. Grimes 		 * POSIX states that if bs is set and no other conversions
3884b88c807SRodney W. Grimes 		 * than noerror, notrunc or sync are specified, the block
3894b88c807SRodney W. Grimes 		 * is output without buffering as it is read.
3904b88c807SRodney W. Grimes 		 */
3916784d416SKonstantin Belousov 		if ((ddflags & ~(C_NOERROR | C_NOTRUNC | C_SYNC)) == C_BS) {
3924b88c807SRodney W. Grimes 			out.dbcnt = in.dbcnt;
3934b88c807SRodney W. Grimes 			dd_out(1);
3944b88c807SRodney W. Grimes 			in.dbcnt = 0;
3954b88c807SRodney W. Grimes 			continue;
3964b88c807SRodney W. Grimes 		}
3974b88c807SRodney W. Grimes 
3984b88c807SRodney W. Grimes 		if (ddflags & C_SWAB) {
399a28ea077SJoerg Wunsch 			if ((n = in.dbrcnt) & 1) {
4004b88c807SRodney W. Grimes 				++st.swab;
4014b88c807SRodney W. Grimes 				--n;
4024b88c807SRodney W. Grimes 			}
403c15c898eSBrian Feldman 			swab(in.dbp, in.dbp, (size_t)n);
4044b88c807SRodney W. Grimes 		}
4054b88c807SRodney W. Grimes 
4064b88c807SRodney W. Grimes 		in.dbp += in.dbrcnt;
4074b88c807SRodney W. Grimes 		(*cfunc)();
4084ac11639SEitan Adler 		if (need_summary) {
4094ac11639SEitan Adler 			summary();
4104ac11639SEitan Adler 		}
4114b88c807SRodney W. Grimes 	}
4124b88c807SRodney W. Grimes }
4134b88c807SRodney W. Grimes 
4144b88c807SRodney W. Grimes /*
41558687472SBrian Feldman  * Clean up any remaining I/O and flush output.  If necessary, the output file
4164b88c807SRodney W. Grimes  * is truncated.
4174b88c807SRodney W. Grimes  */
4184b88c807SRodney W. Grimes static void
419f9bcb0beSWarner Losh dd_close(void)
4204b88c807SRodney W. Grimes {
4214b88c807SRodney W. Grimes 	if (cfunc == def)
4224b88c807SRodney W. Grimes 		def_close();
4234b88c807SRodney W. Grimes 	else if (cfunc == block)
4244b88c807SRodney W. Grimes 		block_close();
4254b88c807SRodney W. Grimes 	else if (cfunc == unblock)
4264b88c807SRodney W. Grimes 		unblock_close();
427af041bf3SJonathan Lemon 	if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
428e3edab4aSRobert Watson 		if (ddflags & C_FILL)
429e3edab4aSRobert Watson 			memset(out.dbp, fill_char, out.dbsz - out.dbcnt);
430e3edab4aSRobert Watson 		else if (ddflags & (C_BLOCK | C_UNBLOCK))
431af041bf3SJonathan Lemon 			memset(out.dbp, ' ', out.dbsz - out.dbcnt);
432af041bf3SJonathan Lemon 		else
4334b88c807SRodney W. Grimes 			memset(out.dbp, 0, out.dbsz - out.dbcnt);
4344b88c807SRodney W. Grimes 		out.dbcnt = out.dbsz;
4354b88c807SRodney W. Grimes 	}
4364ddfeabdSJoerg Wunsch 	if (out.dbcnt || pending)
4374b88c807SRodney W. Grimes 		dd_out(1);
438cd1832feSThomas Quinot 
439cd1832feSThomas Quinot 	/*
440cd1832feSThomas Quinot 	 * If the file ends with a hole, ftruncate it to extend its size
441cd1832feSThomas Quinot 	 * up to the end of the hole (without having to write any data).
442cd1832feSThomas Quinot 	 */
443cd1832feSThomas Quinot 	if (out.seek_offset > 0 && (out.flags & ISTRUNC)) {
444cd1832feSThomas Quinot 		if (ftruncate(out.fd, out.seek_offset) == -1)
445cd1832feSThomas Quinot 			err(1, "truncating %s", out.name);
446cd1832feSThomas Quinot 	}
4474b88c807SRodney W. Grimes }
4484b88c807SRodney W. Grimes 
4494b88c807SRodney W. Grimes void
450f9bcb0beSWarner Losh dd_out(int force)
4514b88c807SRodney W. Grimes {
4524b88c807SRodney W. Grimes 	u_char *outp;
45358687472SBrian Feldman 	size_t cnt, i, n;
45458687472SBrian Feldman 	ssize_t nw;
45558687472SBrian Feldman 	static int warned;
45658687472SBrian Feldman 	int sparse;
4574b88c807SRodney W. Grimes 
4584b88c807SRodney W. Grimes 	/*
4594b88c807SRodney W. Grimes 	 * Write one or more blocks out.  The common case is writing a full
4604b88c807SRodney W. Grimes 	 * output block in a single write; increment the full block stats.
4614b88c807SRodney W. Grimes 	 * Otherwise, we're into partial block writes.  If a partial write,
4624b88c807SRodney W. Grimes 	 * and it's a character device, just warn.  If a tape device, quit.
4634b88c807SRodney W. Grimes 	 *
4644b88c807SRodney W. Grimes 	 * The partial writes represent two cases.  1: Where the input block
4654b88c807SRodney W. Grimes 	 * was less than expected so the output block was less than expected.
4664b88c807SRodney W. Grimes 	 * 2: Where the input block was the right size but we were forced to
4674b88c807SRodney W. Grimes 	 * write the block in multiple chunks.  The original versions of dd(1)
4684b88c807SRodney W. Grimes 	 * never wrote a block in more than a single write, so the latter case
4694b88c807SRodney W. Grimes 	 * never happened.
4704b88c807SRodney W. Grimes 	 *
4714b88c807SRodney W. Grimes 	 * One special case is if we're forced to do the write -- in that case
4724b88c807SRodney W. Grimes 	 * we play games with the buffer size, and it's usually a partial write.
4734b88c807SRodney W. Grimes 	 */
4744b88c807SRodney W. Grimes 	outp = out.db;
475aa7a161bSThomas Quinot 
476aa7a161bSThomas Quinot 	/*
477aa7a161bSThomas Quinot 	 * If force, first try to write all pending data, else try to write
478aa7a161bSThomas Quinot 	 * just one block. Subsequently always write data one full block at
479aa7a161bSThomas Quinot 	 * a time at most.
480aa7a161bSThomas Quinot 	 */
4814b88c807SRodney W. Grimes 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
482aa7a161bSThomas Quinot 		cnt = n;
483aa7a161bSThomas Quinot 		do {
4844ddfeabdSJoerg Wunsch 			sparse = 0;
4854ddfeabdSJoerg Wunsch 			if (ddflags & C_SPARSE) {
4864ddfeabdSJoerg Wunsch 				sparse = 1;	/* Is buffer sparse? */
4874ddfeabdSJoerg Wunsch 				for (i = 0; i < cnt; i++)
4884ddfeabdSJoerg Wunsch 					if (outp[i] != 0) {
4894ddfeabdSJoerg Wunsch 						sparse = 0;
4904ddfeabdSJoerg Wunsch 						break;
4914ddfeabdSJoerg Wunsch 					}
4924ddfeabdSJoerg Wunsch 			}
4934ddfeabdSJoerg Wunsch 			if (sparse && !force) {
4944ddfeabdSJoerg Wunsch 				pending += cnt;
4954ddfeabdSJoerg Wunsch 				nw = cnt;
4964ddfeabdSJoerg Wunsch 			} else {
4974ddfeabdSJoerg Wunsch 				if (pending != 0) {
498cd1832feSThomas Quinot 					/*
499cd1832feSThomas Quinot 					 * Seek past hole.  Note that we need to record the
500cd1832feSThomas Quinot 					 * reached offset, because we might have no more data
501cd1832feSThomas Quinot 					 * to write, in which case we'll need to call
502cd1832feSThomas Quinot 					 * ftruncate to extend the file size.
503aa7a161bSThomas Quinot 					 */
504cd1832feSThomas Quinot 					out.seek_offset = lseek(out.fd, pending, SEEK_CUR);
505cd1832feSThomas Quinot 					if (out.seek_offset == -1)
50654946e00SBrian Feldman 						err(2, "%s: seek error creating sparse file",
50754946e00SBrian Feldman 						    out.name);
508cd1832feSThomas Quinot 					pending = 0;
5094ddfeabdSJoerg Wunsch 				}
510cd1832feSThomas Quinot 				if (cnt) {
5114b88c807SRodney W. Grimes 					nw = write(out.fd, outp, cnt);
512cd1832feSThomas Quinot 					out.seek_offset = 0;
513cd1832feSThomas Quinot 				} else {
5144ddfeabdSJoerg Wunsch 					return;
5154ddfeabdSJoerg Wunsch 				}
516cd1832feSThomas Quinot 			}
5174ddfeabdSJoerg Wunsch 
5184b88c807SRodney W. Grimes 			if (nw <= 0) {
5194b88c807SRodney W. Grimes 				if (nw == 0)
5204b88c807SRodney W. Grimes 					errx(1, "%s: end of device", out.name);
5214b88c807SRodney W. Grimes 				if (errno != EINTR)
5224b88c807SRodney W. Grimes 					err(1, "%s", out.name);
5234b88c807SRodney W. Grimes 				nw = 0;
5244b88c807SRodney W. Grimes 			}
525aa7a161bSThomas Quinot 
5264b88c807SRodney W. Grimes 			outp += nw;
5274b88c807SRodney W. Grimes 			st.bytes += nw;
528aa7a161bSThomas Quinot 
529f4cfd28bSKurt Jaeger 			if ((size_t)nw == n && n == out.dbsz)
5304b88c807SRodney W. Grimes 				++st.out_full;
531aa7a161bSThomas Quinot 			else
5324b88c807SRodney W. Grimes 				++st.out_part;
533aa7a161bSThomas Quinot 
534aa7a161bSThomas Quinot 			if ((size_t) nw != cnt) {
5357599187eSBrian Feldman 				if (out.flags & ISTAPE)
5367599187eSBrian Feldman 					errx(1, "%s: short write on tape device",
5377599187eSBrian Feldman 				    	out.name);
5384b88c807SRodney W. Grimes 				if (out.flags & ISCHR && !warned) {
5394b88c807SRodney W. Grimes 					warned = 1;
5404b88c807SRodney W. Grimes 					warnx("%s: short write on character device",
5414b88c807SRodney W. Grimes 				    	out.name);
5424b88c807SRodney W. Grimes 				}
5434b88c807SRodney W. Grimes 			}
544aa7a161bSThomas Quinot 
545aa7a161bSThomas Quinot 			cnt -= nw;
546aa7a161bSThomas Quinot 		} while (cnt != 0);
547aa7a161bSThomas Quinot 
5484b88c807SRodney W. Grimes 		if ((out.dbcnt -= n) < out.dbsz)
5494b88c807SRodney W. Grimes 			break;
5504b88c807SRodney W. Grimes 	}
5514b88c807SRodney W. Grimes 
5524b88c807SRodney W. Grimes 	/* Reassemble the output block. */
5534b88c807SRodney W. Grimes 	if (out.dbcnt)
55458687472SBrian Feldman 		(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
5554b88c807SRodney W. Grimes 	out.dbp = out.db + out.dbcnt;
5564b88c807SRodney W. Grimes }
557