xref: /freebsd/bin/dd/dd.c (revision 77a798b30ae43e8aac56a124e159b0b8624f4195)
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.
17fbbd9655SWarner Losh  * 3. 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>
502a65657fSBartek Rutkowski #include <sys/capsicum.h>
51e08d7384SBrian Feldman #include <sys/conf.h>
52015a53cfSDavid E. O'Brien #include <sys/disklabel.h>
53e08d7384SBrian Feldman #include <sys/filio.h>
542a65657fSBartek Rutkowski #include <sys/mtio.h>
554b88c807SRodney W. Grimes 
56aa7a161bSThomas Quinot #include <assert.h>
572a65657fSBartek Rutkowski #include <capsicum_helpers.h>
584b88c807SRodney W. Grimes #include <ctype.h>
594b88c807SRodney W. Grimes #include <err.h>
604b88c807SRodney W. Grimes #include <errno.h>
614b88c807SRodney W. Grimes #include <fcntl.h>
627503d74fSMark Murray #include <inttypes.h>
63ad66f7eeSPoul-Henning Kamp #include <locale.h>
644b88c807SRodney W. Grimes #include <stdio.h>
654b88c807SRodney W. Grimes #include <stdlib.h>
664b88c807SRodney W. Grimes #include <string.h>
67d1d66eacSAlan Somers #include <time.h>
684b88c807SRodney W. Grimes #include <unistd.h>
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes #include "dd.h"
714b88c807SRodney W. Grimes #include "extern.h"
724b88c807SRodney W. Grimes 
73f9bcb0beSWarner Losh static void dd_close(void);
74f9bcb0beSWarner Losh static void dd_in(void);
75f9bcb0beSWarner Losh static void getfdtype(IO *);
76f9bcb0beSWarner Losh static void setup(void);
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes IO	in, out;		/* input/output state */
794b88c807SRodney W. Grimes STAT	st;			/* statistics */
80f9bcb0beSWarner Losh void	(*cfunc)(void);		/* conversion function */
817503d74fSMark Murray uintmax_t cpy_cnt;		/* # of blocks to copy */
829afa09cdSMark Murray static off_t	pending = 0;	/* pending seek if sparse */
83413ef2a3SXin LI u_int	ddflags = 0;		/* conversion options */
8458687472SBrian Feldman size_t	cbsz;			/* conversion block size */
857503d74fSMark Murray uintmax_t files_cnt = 1;	/* # of files to copy */
8658687472SBrian Feldman const	u_char *ctab;		/* conversion table */
87e3edab4aSRobert Watson char	fill_char;		/* Character to fill with if defined */
8875e55112SEdward Tomasz Napierala size_t	speed = 0;		/* maximum speed, in bytes per second */
894ac11639SEitan Adler volatile sig_atomic_t need_summary;
904b88c807SRodney W. Grimes 
914b88c807SRodney W. Grimes int
92f9bcb0beSWarner Losh main(int argc __unused, char *argv[])
934b88c807SRodney W. Grimes {
94f7c627b2SAndrey A. Chernov 	(void)setlocale(LC_CTYPE, "");
954b88c807SRodney W. Grimes 	jcl(argv);
964b88c807SRodney W. Grimes 	setup();
974b88c807SRodney W. Grimes 
982a65657fSBartek Rutkowski 	caph_cache_catpages();
992a65657fSBartek Rutkowski 	if (cap_enter() == -1 && errno != ENOSYS)
1002a65657fSBartek Rutkowski 		err(1, "unable to enter capability mode");
1012a65657fSBartek Rutkowski 
1024ac11639SEitan Adler 	(void)signal(SIGINFO, siginfo_handler);
1034b88c807SRodney W. Grimes 	(void)signal(SIGINT, terminate);
1044b88c807SRodney W. Grimes 
1054b88c807SRodney W. Grimes 	atexit(summary);
1064b88c807SRodney W. Grimes 
1074b88c807SRodney W. Grimes 	while (files_cnt--)
1084b88c807SRodney W. Grimes 		dd_in();
1094b88c807SRodney W. Grimes 
1104b88c807SRodney W. Grimes 	dd_close();
111c183a03bSBrooks Davis 	/*
112c183a03bSBrooks Davis 	 * Some devices such as cfi(4) may perform significant amounts
113c183a03bSBrooks Davis 	 * of work when a write descriptor is closed.  Close the out
114c183a03bSBrooks Davis 	 * descriptor explicitly so that the summary handler (called
115c183a03bSBrooks Davis 	 * from an atexit() hook) includes this work.
116c183a03bSBrooks Davis 	 */
117c183a03bSBrooks Davis 	close(out.fd);
1184b88c807SRodney W. Grimes 	exit(0);
1194b88c807SRodney W. Grimes }
1204b88c807SRodney W. Grimes 
1216a3d33acSPoul-Henning Kamp static int
1226a3d33acSPoul-Henning Kamp parity(u_char c)
1236a3d33acSPoul-Henning Kamp {
1246a3d33acSPoul-Henning Kamp 	int i;
1256a3d33acSPoul-Henning Kamp 
1266a3d33acSPoul-Henning Kamp 	i = c ^ (c >> 1) ^ (c >> 2) ^ (c >> 3) ^
1276a3d33acSPoul-Henning Kamp 	    (c >> 4) ^ (c >> 5) ^ (c >> 6) ^ (c >> 7);
1286a3d33acSPoul-Henning Kamp 	return (i & 1);
1296a3d33acSPoul-Henning Kamp }
1306a3d33acSPoul-Henning Kamp 
1314b88c807SRodney W. Grimes static void
132f9bcb0beSWarner Losh setup(void)
1334b88c807SRodney W. Grimes {
1344b88c807SRodney W. Grimes 	u_int cnt;
1352a65657fSBartek Rutkowski 	cap_rights_t rights;
1362a65657fSBartek Rutkowski 	unsigned long cmds[] = { FIODTYPE, MTIOCTOP };
1374b88c807SRodney W. Grimes 
1384b88c807SRodney W. Grimes 	if (in.name == NULL) {
1394b88c807SRodney W. Grimes 		in.name = "stdin";
1404b88c807SRodney W. Grimes 		in.fd = STDIN_FILENO;
1414b88c807SRodney W. Grimes 	} else {
14254946e00SBrian Feldman 		in.fd = open(in.name, O_RDONLY, 0);
143767bc8adSBrian Feldman 		if (in.fd == -1)
1444b88c807SRodney W. Grimes 			err(1, "%s", in.name);
1454b88c807SRodney W. Grimes 	}
1464b88c807SRodney W. Grimes 
1474b88c807SRodney W. Grimes 	getfdtype(&in);
1484b88c807SRodney W. Grimes 
1492a65657fSBartek Rutkowski 	cap_rights_init(&rights, CAP_READ, CAP_SEEK);
1502a65657fSBartek Rutkowski 	if (cap_rights_limit(in.fd, &rights) == -1 && errno != ENOSYS)
1512a65657fSBartek Rutkowski 		err(1, "unable to limit capability rights");
1522a65657fSBartek Rutkowski 
1534b88c807SRodney W. Grimes 	if (files_cnt > 1 && !(in.flags & ISTAPE))
1544b88c807SRodney W. Grimes 		errx(1, "files is not supported for non-tape devices");
1554b88c807SRodney W. Grimes 
1562a65657fSBartek Rutkowski 	cap_rights_set(&rights, CAP_FTRUNCATE, CAP_IOCTL, CAP_WRITE);
1574b88c807SRodney W. Grimes 	if (out.name == NULL) {
1584b88c807SRodney W. Grimes 		/* No way to check for read access here. */
1594b88c807SRodney W. Grimes 		out.fd = STDOUT_FILENO;
1604b88c807SRodney W. Grimes 		out.name = "stdout";
1614b88c807SRodney W. Grimes 	} else {
1624b88c807SRodney W. Grimes #define	OFLAGS \
1634b88c807SRodney W. Grimes     (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
1644b88c807SRodney W. Grimes 		out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
1654b88c807SRodney W. Grimes 		/*
1664b88c807SRodney W. Grimes 		 * May not have read access, so try again with write only.
1674b88c807SRodney W. Grimes 		 * Without read we may have a problem if output also does
1684b88c807SRodney W. Grimes 		 * not support seeks.
1694b88c807SRodney W. Grimes 		 */
170767bc8adSBrian Feldman 		if (out.fd == -1) {
1714b88c807SRodney W. Grimes 			out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
1724b88c807SRodney W. Grimes 			out.flags |= NOREAD;
1732a65657fSBartek Rutkowski 			cap_rights_clear(&rights, CAP_READ);
1744b88c807SRodney W. Grimes 		}
175767bc8adSBrian Feldman 		if (out.fd == -1)
1764b88c807SRodney W. Grimes 			err(1, "%s", out.name);
1774b88c807SRodney W. Grimes 	}
1784b88c807SRodney W. Grimes 
1794b88c807SRodney W. Grimes 	getfdtype(&out);
1804b88c807SRodney W. Grimes 
1812a65657fSBartek Rutkowski 	if (cap_rights_limit(out.fd, &rights) == -1 && errno != ENOSYS)
1822a65657fSBartek Rutkowski 		err(1, "unable to limit capability rights");
1832a65657fSBartek Rutkowski 	if (cap_ioctls_limit(out.fd, cmds, nitems(cmds)) == -1 &&
1842a65657fSBartek Rutkowski 	    errno != ENOSYS)
1852a65657fSBartek Rutkowski 		err(1, "unable to limit capability rights");
1862a65657fSBartek Rutkowski 
18794161f30SBartek Rutkowski 	if (in.fd != STDIN_FILENO && out.fd != STDIN_FILENO) {
18894161f30SBartek Rutkowski 		if (caph_limit_stdin() == -1)
18994161f30SBartek Rutkowski 			err(1, "unable to limit capability rights");
19094161f30SBartek Rutkowski 	}
19194161f30SBartek Rutkowski 
19294161f30SBartek Rutkowski 	if (in.fd != STDOUT_FILENO && out.fd != STDOUT_FILENO) {
19394161f30SBartek Rutkowski 		if (caph_limit_stdout() == -1)
19494161f30SBartek Rutkowski 			err(1, "unable to limit capability rights");
19594161f30SBartek Rutkowski 	}
19694161f30SBartek Rutkowski 
1972a65657fSBartek Rutkowski 	if (in.fd != STDERR_FILENO && out.fd != STDERR_FILENO) {
1982a65657fSBartek Rutkowski 		if (caph_limit_stderr() == -1)
1992a65657fSBartek Rutkowski 			err(1, "unable to limit capability rights");
2002a65657fSBartek Rutkowski 	}
2012a65657fSBartek Rutkowski 
2024b88c807SRodney W. Grimes 	/*
2034b88c807SRodney W. Grimes 	 * Allocate space for the input and output buffers.  If not doing
2044b88c807SRodney W. Grimes 	 * record oriented I/O, only need a single buffer.
2054b88c807SRodney W. Grimes 	 */
2064b88c807SRodney W. Grimes 	if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
207*77a798b3SAlan Somers 		if ((in.db = malloc((size_t)out.dbsz + in.dbsz - 1)) == NULL)
20858687472SBrian Feldman 			err(1, "input buffer");
2094b88c807SRodney W. Grimes 		out.db = in.db;
210*77a798b3SAlan Somers 	} else if ((in.db = malloc(MAX((size_t)in.dbsz, cbsz) + cbsz)) == NULL ||
21158687472SBrian Feldman 	    (out.db = malloc(out.dbsz + cbsz)) == NULL)
21258687472SBrian Feldman 		err(1, "output buffer");
213aa7a161bSThomas Quinot 
214aa7a161bSThomas Quinot 	/* dbp is the first free position in each buffer. */
2154b88c807SRodney W. Grimes 	in.dbp = in.db;
2164b88c807SRodney W. Grimes 	out.dbp = out.db;
2174b88c807SRodney W. Grimes 
2184b88c807SRodney W. Grimes 	/* Position the input/output streams. */
2194b88c807SRodney W. Grimes 	if (in.offset)
2204b88c807SRodney W. Grimes 		pos_in();
2214b88c807SRodney W. Grimes 	if (out.offset)
2224b88c807SRodney W. Grimes 		pos_out();
2234b88c807SRodney W. Grimes 
2244b88c807SRodney W. Grimes 	/*
2255976ee7eSBrian Feldman 	 * Truncate the output file.  If it fails on a type of output file
2265976ee7eSBrian Feldman 	 * that it should _not_ fail on, error out.
2274b88c807SRodney W. Grimes 	 */
228c15c898eSBrian Feldman 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
229c15c898eSBrian Feldman 	    out.flags & ISTRUNC)
230c15c898eSBrian Feldman 		if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
231c15c898eSBrian Feldman 			err(1, "truncating %s", out.name);
2324b88c807SRodney W. Grimes 
2336a3d33acSPoul-Henning Kamp 	if (ddflags & (C_LCASE  | C_UCASE | C_ASCII | C_EBCDIC | C_PARITY)) {
2346a3d33acSPoul-Henning Kamp 		if (ctab != NULL) {
2356a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2366a3d33acSPoul-Henning Kamp 				casetab[cnt] = ctab[cnt];
2376a3d33acSPoul-Henning Kamp 		} else {
2386a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2396a3d33acSPoul-Henning Kamp 				casetab[cnt] = cnt;
2406a3d33acSPoul-Henning Kamp 		}
2416a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARITY) && !(ddflags & C_ASCII)) {
2424b88c807SRodney W. Grimes 			/*
2436a3d33acSPoul-Henning Kamp 			 * If the input is not EBCDIC, and we do parity
2446a3d33acSPoul-Henning Kamp 			 * processing, strip input parity.
2454b88c807SRodney W. Grimes 			 */
2466a3d33acSPoul-Henning Kamp 			for (cnt = 200; cnt <= 0377; ++cnt)
2476a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt & 0x7f];
2486a3d33acSPoul-Henning Kamp 		}
2494b88c807SRodney W. Grimes 		if (ddflags & C_LCASE) {
250c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2516a3d33acSPoul-Henning Kamp 				casetab[cnt] = tolower(casetab[cnt]);
2526a3d33acSPoul-Henning Kamp 		} else if (ddflags & C_UCASE) {
253c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2546a3d33acSPoul-Henning Kamp 				casetab[cnt] = toupper(casetab[cnt]);
2554b88c807SRodney W. Grimes 		}
2566a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARITY)) {
2576a3d33acSPoul-Henning Kamp 			/*
2586a3d33acSPoul-Henning Kamp 			 * This should strictly speaking be a no-op, but I
2596a3d33acSPoul-Henning Kamp 			 * wonder what funny LANG settings could get us.
2606a3d33acSPoul-Henning Kamp 			 */
261c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2626a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt] & 0x7f;
2636a3d33acSPoul-Henning Kamp 		}
2646a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARSET)) {
265c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2666a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt] | 0x80;
267dde07463SAndrey A. Chernov 		}
2686a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PAREVEN)) {
2696a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2706a3d33acSPoul-Henning Kamp 				if (parity(casetab[cnt]))
2716a3d33acSPoul-Henning Kamp 					casetab[cnt] = casetab[cnt] | 0x80;
272dde07463SAndrey A. Chernov 		}
2736a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARODD)) {
2746a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2756a3d33acSPoul-Henning Kamp 				if (!parity(casetab[cnt]))
2766a3d33acSPoul-Henning Kamp 					casetab[cnt] = casetab[cnt] | 0x80;
2776a3d33acSPoul-Henning Kamp 		}
2786a3d33acSPoul-Henning Kamp 
27958687472SBrian Feldman 		ctab = casetab;
280426e9c1dSWarner Losh 	}
28158687472SBrian Feldman 
282540c7825SAlan Somers 	if (clock_gettime(CLOCK_MONOTONIC, &st.start))
283540c7825SAlan Somers 		err(1, "clock_gettime");
2844b88c807SRodney W. Grimes }
2854b88c807SRodney W. Grimes 
2864b88c807SRodney W. Grimes static void
287f9bcb0beSWarner Losh getfdtype(IO *io)
2884b88c807SRodney W. Grimes {
2894b88c807SRodney W. Grimes 	struct stat sb;
290e08d7384SBrian Feldman 	int type;
2914b88c807SRodney W. Grimes 
29258687472SBrian Feldman 	if (fstat(io->fd, &sb) == -1)
2934b88c807SRodney W. Grimes 		err(1, "%s", io->name);
294c15c898eSBrian Feldman 	if (S_ISREG(sb.st_mode))
295c15c898eSBrian Feldman 		io->flags |= ISTRUNC;
296e08d7384SBrian Feldman 	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
297ff8bb989SBrian Feldman 		if (ioctl(io->fd, FIODTYPE, &type) == -1) {
298bf3367d0SBrian Feldman 			err(1, "%s", io->name);
299ff8bb989SBrian Feldman 		} else {
300e08d7384SBrian Feldman 			if (type & D_TAPE)
301e08d7384SBrian Feldman 				io->flags |= ISTAPE;
302cd967e32SPoul-Henning Kamp 			else if (type & (D_DISK | D_MEM))
30332952d4bSBrian Feldman 				io->flags |= ISSEEK;
304e08d7384SBrian Feldman 			if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
305e08d7384SBrian Feldman 				io->flags |= ISCHR;
306ff8bb989SBrian Feldman 		}
3075ff6541eSBrian Feldman 		return;
3085ff6541eSBrian Feldman 	}
3095ff6541eSBrian Feldman 	errno = 0;
3105ff6541eSBrian Feldman 	if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
311e08d7384SBrian Feldman 		io->flags |= ISPIPE;
3125ff6541eSBrian Feldman 	else
3135ff6541eSBrian Feldman 		io->flags |= ISSEEK;
3144b88c807SRodney W. Grimes }
3154b88c807SRodney W. Grimes 
31675e55112SEdward Tomasz Napierala /*
31775e55112SEdward Tomasz Napierala  * Limit the speed by adding a delay before every block read.
31875e55112SEdward Tomasz Napierala  * The delay (t_usleep) is equal to the time computed from block
31975e55112SEdward Tomasz Napierala  * size and the specified speed limit (t_target) minus the time
32075e55112SEdward Tomasz Napierala  * spent on actual read and write operations (t_io).
32175e55112SEdward Tomasz Napierala  */
32275e55112SEdward Tomasz Napierala static void
32375e55112SEdward Tomasz Napierala speed_limit(void)
32475e55112SEdward Tomasz Napierala {
32575e55112SEdward Tomasz Napierala 	static double t_prev, t_usleep;
32675e55112SEdward Tomasz Napierala 	double t_now, t_io, t_target;
32775e55112SEdward Tomasz Napierala 
32875e55112SEdward Tomasz Napierala 	t_now = secs_elapsed();
32975e55112SEdward Tomasz Napierala 	t_io = t_now - t_prev - t_usleep;
33075e55112SEdward Tomasz Napierala 	t_target = (double)in.dbsz / (double)speed;
33175e55112SEdward Tomasz Napierala 	t_usleep = t_target - t_io;
33275e55112SEdward Tomasz Napierala 	if (t_usleep > 0)
33375e55112SEdward Tomasz Napierala 		usleep(t_usleep * 1000000);
33475e55112SEdward Tomasz Napierala 	else
33575e55112SEdward Tomasz Napierala 		t_usleep = 0;
33675e55112SEdward Tomasz Napierala 	t_prev = t_now;
33775e55112SEdward Tomasz Napierala }
33875e55112SEdward Tomasz Napierala 
3394b88c807SRodney W. Grimes static void
340f9bcb0beSWarner Losh dd_in(void)
3414b88c807SRodney W. Grimes {
342767bc8adSBrian Feldman 	ssize_t n;
3434b88c807SRodney W. Grimes 
344af041bf3SJonathan Lemon 	for (;;) {
3455ff6541eSBrian Feldman 		switch (cpy_cnt) {
3465ff6541eSBrian Feldman 		case -1:			/* count=0 was specified */
3474b88c807SRodney W. Grimes 			return;
3485ff6541eSBrian Feldman 		case 0:
3495ff6541eSBrian Feldman 			break;
3505ff6541eSBrian Feldman 		default:
3517503d74fSMark Murray 			if (st.in_full + st.in_part >= (uintmax_t)cpy_cnt)
3525ff6541eSBrian Feldman 				return;
3535ff6541eSBrian Feldman 			break;
3545ff6541eSBrian Feldman 		}
3554b88c807SRodney W. Grimes 
35675e55112SEdward Tomasz Napierala 		if (speed > 0)
35775e55112SEdward Tomasz Napierala 			speed_limit();
35875e55112SEdward Tomasz Napierala 
3594b88c807SRodney W. Grimes 		/*
36058687472SBrian Feldman 		 * Zero the buffer first if sync; if doing block operations,
3614b88c807SRodney W. Grimes 		 * use spaces.
3624b88c807SRodney W. Grimes 		 */
363767bc8adSBrian Feldman 		if (ddflags & C_SYNC) {
364e3edab4aSRobert Watson 			if (ddflags & C_FILL)
365e3edab4aSRobert Watson 				memset(in.dbp, fill_char, in.dbsz);
366e3edab4aSRobert Watson 			else if (ddflags & (C_BLOCK | C_UNBLOCK))
3674b88c807SRodney W. Grimes 				memset(in.dbp, ' ', in.dbsz);
3684b88c807SRodney W. Grimes 			else
3694b88c807SRodney W. Grimes 				memset(in.dbp, 0, in.dbsz);
370767bc8adSBrian Feldman 		}
3714b88c807SRodney W. Grimes 
3724b88c807SRodney W. Grimes 		n = read(in.fd, in.dbp, in.dbsz);
3734b88c807SRodney W. Grimes 		if (n == 0) {
3744b88c807SRodney W. Grimes 			in.dbrcnt = 0;
3754b88c807SRodney W. Grimes 			return;
3764b88c807SRodney W. Grimes 		}
3774b88c807SRodney W. Grimes 
3784b88c807SRodney W. Grimes 		/* Read error. */
379767bc8adSBrian Feldman 		if (n == -1) {
3804b88c807SRodney W. Grimes 			/*
3814b88c807SRodney W. Grimes 			 * If noerror not specified, die.  POSIX requires that
3824b88c807SRodney W. Grimes 			 * the warning message be followed by an I/O display.
3834b88c807SRodney W. Grimes 			 */
384af041bf3SJonathan Lemon 			if (!(ddflags & C_NOERROR))
3854b88c807SRodney W. Grimes 				err(1, "%s", in.name);
3864b88c807SRodney W. Grimes 			warn("%s", in.name);
3874b88c807SRodney W. Grimes 			summary();
3884b88c807SRodney W. Grimes 
3894b88c807SRodney W. Grimes 			/*
3907599187eSBrian Feldman 			 * If it's a seekable file descriptor, seek past the
3914b88c807SRodney W. Grimes 			 * error.  If your OS doesn't do the right thing for
3924b88c807SRodney W. Grimes 			 * raw disks this section should be modified to re-read
3934b88c807SRodney W. Grimes 			 * in sector size chunks.
3944b88c807SRodney W. Grimes 			 */
3957599187eSBrian Feldman 			if (in.flags & ISSEEK &&
3964b88c807SRodney W. Grimes 			    lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
3974b88c807SRodney W. Grimes 				warn("%s", in.name);
3984b88c807SRodney W. Grimes 
3994b88c807SRodney W. Grimes 			/* If sync not specified, omit block and continue. */
4004b88c807SRodney W. Grimes 			if (!(ddflags & C_SYNC))
4014b88c807SRodney W. Grimes 				continue;
4024b88c807SRodney W. Grimes 
4034b88c807SRodney W. Grimes 			/* Read errors count as full blocks. */
4044b88c807SRodney W. Grimes 			in.dbcnt += in.dbrcnt = in.dbsz;
4054b88c807SRodney W. Grimes 			++st.in_full;
4064b88c807SRodney W. Grimes 
4074b88c807SRodney W. Grimes 		/* Handle full input blocks. */
408*77a798b3SAlan Somers 		} else if ((size_t)n == (size_t)in.dbsz) {
4094b88c807SRodney W. Grimes 			in.dbcnt += in.dbrcnt = n;
4104b88c807SRodney W. Grimes 			++st.in_full;
4114b88c807SRodney W. Grimes 
4124b88c807SRodney W. Grimes 		/* Handle partial input blocks. */
4134b88c807SRodney W. Grimes 		} else {
4144b88c807SRodney W. Grimes 			/* If sync, use the entire block. */
4154b88c807SRodney W. Grimes 			if (ddflags & C_SYNC)
4164b88c807SRodney W. Grimes 				in.dbcnt += in.dbrcnt = in.dbsz;
4174b88c807SRodney W. Grimes 			else
4184b88c807SRodney W. Grimes 				in.dbcnt += in.dbrcnt = n;
4194b88c807SRodney W. Grimes 			++st.in_part;
4204b88c807SRodney W. Grimes 		}
4214b88c807SRodney W. Grimes 
4224b88c807SRodney W. Grimes 		/*
4234b88c807SRodney W. Grimes 		 * POSIX states that if bs is set and no other conversions
4244b88c807SRodney W. Grimes 		 * than noerror, notrunc or sync are specified, the block
4254b88c807SRodney W. Grimes 		 * is output without buffering as it is read.
4264b88c807SRodney W. Grimes 		 */
4276784d416SKonstantin Belousov 		if ((ddflags & ~(C_NOERROR | C_NOTRUNC | C_SYNC)) == C_BS) {
4284b88c807SRodney W. Grimes 			out.dbcnt = in.dbcnt;
4294b88c807SRodney W. Grimes 			dd_out(1);
4304b88c807SRodney W. Grimes 			in.dbcnt = 0;
4314b88c807SRodney W. Grimes 			continue;
4324b88c807SRodney W. Grimes 		}
4334b88c807SRodney W. Grimes 
4344b88c807SRodney W. Grimes 		if (ddflags & C_SWAB) {
435a28ea077SJoerg Wunsch 			if ((n = in.dbrcnt) & 1) {
4364b88c807SRodney W. Grimes 				++st.swab;
4374b88c807SRodney W. Grimes 				--n;
4384b88c807SRodney W. Grimes 			}
439c15c898eSBrian Feldman 			swab(in.dbp, in.dbp, (size_t)n);
4404b88c807SRodney W. Grimes 		}
4414b88c807SRodney W. Grimes 
4424b88c807SRodney W. Grimes 		in.dbp += in.dbrcnt;
4434b88c807SRodney W. Grimes 		(*cfunc)();
4444ac11639SEitan Adler 		if (need_summary) {
4454ac11639SEitan Adler 			summary();
4464ac11639SEitan Adler 		}
4474b88c807SRodney W. Grimes 	}
4484b88c807SRodney W. Grimes }
4494b88c807SRodney W. Grimes 
4504b88c807SRodney W. Grimes /*
45158687472SBrian Feldman  * Clean up any remaining I/O and flush output.  If necessary, the output file
4524b88c807SRodney W. Grimes  * is truncated.
4534b88c807SRodney W. Grimes  */
4544b88c807SRodney W. Grimes static void
455f9bcb0beSWarner Losh dd_close(void)
4564b88c807SRodney W. Grimes {
4574b88c807SRodney W. Grimes 	if (cfunc == def)
4584b88c807SRodney W. Grimes 		def_close();
4594b88c807SRodney W. Grimes 	else if (cfunc == block)
4604b88c807SRodney W. Grimes 		block_close();
4614b88c807SRodney W. Grimes 	else if (cfunc == unblock)
4624b88c807SRodney W. Grimes 		unblock_close();
463af041bf3SJonathan Lemon 	if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
464e3edab4aSRobert Watson 		if (ddflags & C_FILL)
465e3edab4aSRobert Watson 			memset(out.dbp, fill_char, out.dbsz - out.dbcnt);
466e3edab4aSRobert Watson 		else if (ddflags & (C_BLOCK | C_UNBLOCK))
467af041bf3SJonathan Lemon 			memset(out.dbp, ' ', out.dbsz - out.dbcnt);
468af041bf3SJonathan Lemon 		else
4694b88c807SRodney W. Grimes 			memset(out.dbp, 0, out.dbsz - out.dbcnt);
4704b88c807SRodney W. Grimes 		out.dbcnt = out.dbsz;
4714b88c807SRodney W. Grimes 	}
4724ddfeabdSJoerg Wunsch 	if (out.dbcnt || pending)
4734b88c807SRodney W. Grimes 		dd_out(1);
474cd1832feSThomas Quinot 
475cd1832feSThomas Quinot 	/*
476cd1832feSThomas Quinot 	 * If the file ends with a hole, ftruncate it to extend its size
477cd1832feSThomas Quinot 	 * up to the end of the hole (without having to write any data).
478cd1832feSThomas Quinot 	 */
479cd1832feSThomas Quinot 	if (out.seek_offset > 0 && (out.flags & ISTRUNC)) {
480cd1832feSThomas Quinot 		if (ftruncate(out.fd, out.seek_offset) == -1)
481cd1832feSThomas Quinot 			err(1, "truncating %s", out.name);
482cd1832feSThomas Quinot 	}
4834b88c807SRodney W. Grimes }
4844b88c807SRodney W. Grimes 
4854b88c807SRodney W. Grimes void
486f9bcb0beSWarner Losh dd_out(int force)
4874b88c807SRodney W. Grimes {
4884b88c807SRodney W. Grimes 	u_char *outp;
48958687472SBrian Feldman 	size_t cnt, i, n;
49058687472SBrian Feldman 	ssize_t nw;
49158687472SBrian Feldman 	static int warned;
49258687472SBrian Feldman 	int sparse;
4934b88c807SRodney W. Grimes 
4944b88c807SRodney W. Grimes 	/*
4954b88c807SRodney W. Grimes 	 * Write one or more blocks out.  The common case is writing a full
4964b88c807SRodney W. Grimes 	 * output block in a single write; increment the full block stats.
4974b88c807SRodney W. Grimes 	 * Otherwise, we're into partial block writes.  If a partial write,
4984b88c807SRodney W. Grimes 	 * and it's a character device, just warn.  If a tape device, quit.
4994b88c807SRodney W. Grimes 	 *
5004b88c807SRodney W. Grimes 	 * The partial writes represent two cases.  1: Where the input block
5014b88c807SRodney W. Grimes 	 * was less than expected so the output block was less than expected.
5024b88c807SRodney W. Grimes 	 * 2: Where the input block was the right size but we were forced to
5034b88c807SRodney W. Grimes 	 * write the block in multiple chunks.  The original versions of dd(1)
5044b88c807SRodney W. Grimes 	 * never wrote a block in more than a single write, so the latter case
5054b88c807SRodney W. Grimes 	 * never happened.
5064b88c807SRodney W. Grimes 	 *
5074b88c807SRodney W. Grimes 	 * One special case is if we're forced to do the write -- in that case
5084b88c807SRodney W. Grimes 	 * we play games with the buffer size, and it's usually a partial write.
5094b88c807SRodney W. Grimes 	 */
5104b88c807SRodney W. Grimes 	outp = out.db;
511aa7a161bSThomas Quinot 
512aa7a161bSThomas Quinot 	/*
513aa7a161bSThomas Quinot 	 * If force, first try to write all pending data, else try to write
514aa7a161bSThomas Quinot 	 * just one block. Subsequently always write data one full block at
515aa7a161bSThomas Quinot 	 * a time at most.
516aa7a161bSThomas Quinot 	 */
5174b88c807SRodney W. Grimes 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
518aa7a161bSThomas Quinot 		cnt = n;
519aa7a161bSThomas Quinot 		do {
5204ddfeabdSJoerg Wunsch 			sparse = 0;
5214ddfeabdSJoerg Wunsch 			if (ddflags & C_SPARSE) {
5224ddfeabdSJoerg Wunsch 				sparse = 1;	/* Is buffer sparse? */
5234ddfeabdSJoerg Wunsch 				for (i = 0; i < cnt; i++)
5244ddfeabdSJoerg Wunsch 					if (outp[i] != 0) {
5254ddfeabdSJoerg Wunsch 						sparse = 0;
5264ddfeabdSJoerg Wunsch 						break;
5274ddfeabdSJoerg Wunsch 					}
5284ddfeabdSJoerg Wunsch 			}
5294ddfeabdSJoerg Wunsch 			if (sparse && !force) {
5304ddfeabdSJoerg Wunsch 				pending += cnt;
5314ddfeabdSJoerg Wunsch 				nw = cnt;
5324ddfeabdSJoerg Wunsch 			} else {
5334ddfeabdSJoerg Wunsch 				if (pending != 0) {
534cd1832feSThomas Quinot 					/*
535cd1832feSThomas Quinot 					 * Seek past hole.  Note that we need to record the
536cd1832feSThomas Quinot 					 * reached offset, because we might have no more data
537cd1832feSThomas Quinot 					 * to write, in which case we'll need to call
538cd1832feSThomas Quinot 					 * ftruncate to extend the file size.
539aa7a161bSThomas Quinot 					 */
540cd1832feSThomas Quinot 					out.seek_offset = lseek(out.fd, pending, SEEK_CUR);
541cd1832feSThomas Quinot 					if (out.seek_offset == -1)
54254946e00SBrian Feldman 						err(2, "%s: seek error creating sparse file",
54354946e00SBrian Feldman 						    out.name);
544cd1832feSThomas Quinot 					pending = 0;
5454ddfeabdSJoerg Wunsch 				}
546cd1832feSThomas Quinot 				if (cnt) {
5474b88c807SRodney W. Grimes 					nw = write(out.fd, outp, cnt);
548cd1832feSThomas Quinot 					out.seek_offset = 0;
549cd1832feSThomas Quinot 				} else {
5504ddfeabdSJoerg Wunsch 					return;
5514ddfeabdSJoerg Wunsch 				}
552cd1832feSThomas Quinot 			}
5534ddfeabdSJoerg Wunsch 
5544b88c807SRodney W. Grimes 			if (nw <= 0) {
5554b88c807SRodney W. Grimes 				if (nw == 0)
5564b88c807SRodney W. Grimes 					errx(1, "%s: end of device", out.name);
5574b88c807SRodney W. Grimes 				if (errno != EINTR)
5584b88c807SRodney W. Grimes 					err(1, "%s", out.name);
5594b88c807SRodney W. Grimes 				nw = 0;
5604b88c807SRodney W. Grimes 			}
561aa7a161bSThomas Quinot 
5624b88c807SRodney W. Grimes 			outp += nw;
5634b88c807SRodney W. Grimes 			st.bytes += nw;
564aa7a161bSThomas Quinot 
565*77a798b3SAlan Somers 			if ((size_t)nw == n && n == (size_t)out.dbsz)
5664b88c807SRodney W. Grimes 				++st.out_full;
567aa7a161bSThomas Quinot 			else
5684b88c807SRodney W. Grimes 				++st.out_part;
569aa7a161bSThomas Quinot 
570aa7a161bSThomas Quinot 			if ((size_t) nw != cnt) {
5717599187eSBrian Feldman 				if (out.flags & ISTAPE)
5727599187eSBrian Feldman 					errx(1, "%s: short write on tape device",
5737599187eSBrian Feldman 				    	out.name);
5744b88c807SRodney W. Grimes 				if (out.flags & ISCHR && !warned) {
5754b88c807SRodney W. Grimes 					warned = 1;
5764b88c807SRodney W. Grimes 					warnx("%s: short write on character device",
5774b88c807SRodney W. Grimes 				    	out.name);
5784b88c807SRodney W. Grimes 				}
5794b88c807SRodney W. Grimes 			}
580aa7a161bSThomas Quinot 
581aa7a161bSThomas Quinot 			cnt -= nw;
582aa7a161bSThomas Quinot 		} while (cnt != 0);
583aa7a161bSThomas Quinot 
5844b88c807SRodney W. Grimes 		if ((out.dbcnt -= n) < out.dbsz)
5854b88c807SRodney W. Grimes 			break;
5864b88c807SRodney W. Grimes 	}
5874b88c807SRodney W. Grimes 
5884b88c807SRodney W. Grimes 	/* Reassemble the output block. */
5894b88c807SRodney W. Grimes 	if (out.dbcnt)
59058687472SBrian Feldman 		(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
5914b88c807SRodney W. Grimes 	out.dbp = out.db + out.dbcnt;
5924b88c807SRodney W. Grimes }
593