xref: /freebsd/bin/dd/dd.c (revision 8acbb227d9cc40b62c035d0d9d9b54b7872b93d8)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego and Lance
94b88c807SRodney W. Grimes  * Visser of Convex Computer Corporation.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
3609a80d48SDavid E. O'Brien #if 0
374b88c807SRodney W. Grimes #ifndef lint
3878b09ffeSSteve Price static char const copyright[] =
394b88c807SRodney W. Grimes "@(#) Copyright (c) 1991, 1993, 1994\n\
404b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
414b88c807SRodney W. Grimes #endif /* not lint */
424b88c807SRodney W. Grimes 
434b88c807SRodney W. Grimes #ifndef lint
441ba0e048SPhilippe Charnier static char sccsid[] = "@(#)dd.c	8.5 (Berkeley) 4/2/94";
454b88c807SRodney W. Grimes #endif /* not lint */
4609a80d48SDavid E. O'Brien #endif
475eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
485eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$");
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes #include <sys/param.h>
514b88c807SRodney W. Grimes #include <sys/stat.h>
522a65657fSBartek Rutkowski #include <sys/capsicum.h>
53e08d7384SBrian Feldman #include <sys/conf.h>
54015a53cfSDavid E. O'Brien #include <sys/disklabel.h>
55e08d7384SBrian Feldman #include <sys/filio.h>
562a65657fSBartek Rutkowski #include <sys/mtio.h>
574767c42cSKyle Evans #include <sys/time.h>
584b88c807SRodney W. Grimes 
59aa7a161bSThomas Quinot #include <assert.h>
602a65657fSBartek Rutkowski #include <capsicum_helpers.h>
614b88c807SRodney W. Grimes #include <ctype.h>
624b88c807SRodney W. Grimes #include <err.h>
634b88c807SRodney W. Grimes #include <errno.h>
644b88c807SRodney W. Grimes #include <fcntl.h>
657503d74fSMark Murray #include <inttypes.h>
66ad66f7eeSPoul-Henning Kamp #include <locale.h>
674b88c807SRodney W. Grimes #include <stdio.h>
684b88c807SRodney W. Grimes #include <stdlib.h>
694b88c807SRodney W. Grimes #include <string.h>
70d1d66eacSAlan Somers #include <time.h>
714b88c807SRodney W. Grimes #include <unistd.h>
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes #include "dd.h"
744b88c807SRodney W. Grimes #include "extern.h"
754b88c807SRodney W. Grimes 
76f9bcb0beSWarner Losh static void dd_close(void);
77f9bcb0beSWarner Losh static void dd_in(void);
78f9bcb0beSWarner Losh static void getfdtype(IO *);
79f9bcb0beSWarner Losh static void setup(void);
804b88c807SRodney W. Grimes 
814b88c807SRodney W. Grimes IO	in, out;		/* input/output state */
824b88c807SRodney W. Grimes STAT	st;			/* statistics */
83f9bcb0beSWarner Losh void	(*cfunc)(void);		/* conversion function */
847503d74fSMark Murray uintmax_t cpy_cnt;		/* # of blocks to copy */
859afa09cdSMark Murray static off_t	pending = 0;	/* pending seek if sparse */
86413ef2a3SXin LI u_int	ddflags = 0;		/* conversion options */
8758687472SBrian Feldman size_t	cbsz;			/* conversion block size */
887503d74fSMark Murray uintmax_t files_cnt = 1;	/* # of files to copy */
8958687472SBrian Feldman const	u_char *ctab;		/* conversion table */
90e3edab4aSRobert Watson char	fill_char;		/* Character to fill with if defined */
9175e55112SEdward Tomasz Napierala size_t	speed = 0;		/* maximum speed, in bytes per second */
924ac11639SEitan Adler volatile sig_atomic_t need_summary;
934767c42cSKyle Evans volatile sig_atomic_t need_progress;
944b88c807SRodney W. Grimes 
954b88c807SRodney W. Grimes int
96f9bcb0beSWarner Losh main(int argc __unused, char *argv[])
974b88c807SRodney W. Grimes {
98*8acbb227SKyle Evans 	struct itimerval itv = { { 1, 0 }, { 1, 0 } }; /* SIGALARM every second, if needed */
99*8acbb227SKyle Evans 
100f7c627b2SAndrey A. Chernov 	(void)setlocale(LC_CTYPE, "");
1014b88c807SRodney W. Grimes 	jcl(argv);
1024b88c807SRodney W. Grimes 	setup();
1034b88c807SRodney W. Grimes 
1042a65657fSBartek Rutkowski 	caph_cache_catpages();
1057672a014SMariusz Zaborski 	if (caph_enter() < 0)
1062a65657fSBartek Rutkowski 		err(1, "unable to enter capability mode");
1072a65657fSBartek Rutkowski 
1084ac11639SEitan Adler 	(void)signal(SIGINFO, siginfo_handler);
109*8acbb227SKyle Evans 	if (ddflags & C_PROGRESS) {
110*8acbb227SKyle Evans 		(void)signal(SIGALRM, sigalarm_handler);
111*8acbb227SKyle Evans 		setitimer(ITIMER_REAL, &itv, NULL);
112*8acbb227SKyle Evans 	}
1134b88c807SRodney W. Grimes 	(void)signal(SIGINT, terminate);
1144b88c807SRodney W. Grimes 
1154b88c807SRodney W. Grimes 	atexit(summary);
1164b88c807SRodney W. Grimes 
1174b88c807SRodney W. Grimes 	while (files_cnt--)
1184b88c807SRodney W. Grimes 		dd_in();
1194b88c807SRodney W. Grimes 
1204b88c807SRodney W. Grimes 	dd_close();
121c183a03bSBrooks Davis 	/*
122c183a03bSBrooks Davis 	 * Some devices such as cfi(4) may perform significant amounts
123c183a03bSBrooks Davis 	 * of work when a write descriptor is closed.  Close the out
124c183a03bSBrooks Davis 	 * descriptor explicitly so that the summary handler (called
125c183a03bSBrooks Davis 	 * from an atexit() hook) includes this work.
126c183a03bSBrooks Davis 	 */
127c183a03bSBrooks Davis 	close(out.fd);
1284b88c807SRodney W. Grimes 	exit(0);
1294b88c807SRodney W. Grimes }
1304b88c807SRodney W. Grimes 
1316a3d33acSPoul-Henning Kamp static int
1326a3d33acSPoul-Henning Kamp parity(u_char c)
1336a3d33acSPoul-Henning Kamp {
1346a3d33acSPoul-Henning Kamp 	int i;
1356a3d33acSPoul-Henning Kamp 
1366a3d33acSPoul-Henning Kamp 	i = c ^ (c >> 1) ^ (c >> 2) ^ (c >> 3) ^
1376a3d33acSPoul-Henning Kamp 	    (c >> 4) ^ (c >> 5) ^ (c >> 6) ^ (c >> 7);
1386a3d33acSPoul-Henning Kamp 	return (i & 1);
1396a3d33acSPoul-Henning Kamp }
1406a3d33acSPoul-Henning Kamp 
1414b88c807SRodney W. Grimes static void
142f9bcb0beSWarner Losh setup(void)
1434b88c807SRodney W. Grimes {
1444b88c807SRodney W. Grimes 	u_int cnt;
1452a65657fSBartek Rutkowski 	cap_rights_t rights;
1462a65657fSBartek Rutkowski 	unsigned long cmds[] = { FIODTYPE, MTIOCTOP };
1474b88c807SRodney W. Grimes 
1484b88c807SRodney W. Grimes 	if (in.name == NULL) {
1494b88c807SRodney W. Grimes 		in.name = "stdin";
1504b88c807SRodney W. Grimes 		in.fd = STDIN_FILENO;
1514b88c807SRodney W. Grimes 	} else {
15254946e00SBrian Feldman 		in.fd = open(in.name, O_RDONLY, 0);
153767bc8adSBrian Feldman 		if (in.fd == -1)
1544b88c807SRodney W. Grimes 			err(1, "%s", in.name);
1554b88c807SRodney W. Grimes 	}
1564b88c807SRodney W. Grimes 
1574b88c807SRodney W. Grimes 	getfdtype(&in);
1584b88c807SRodney W. Grimes 
1592a65657fSBartek Rutkowski 	cap_rights_init(&rights, CAP_READ, CAP_SEEK);
1602a65657fSBartek Rutkowski 	if (cap_rights_limit(in.fd, &rights) == -1 && errno != ENOSYS)
1612a65657fSBartek Rutkowski 		err(1, "unable to limit capability rights");
1622a65657fSBartek Rutkowski 
1634b88c807SRodney W. Grimes 	if (files_cnt > 1 && !(in.flags & ISTAPE))
1644b88c807SRodney W. Grimes 		errx(1, "files is not supported for non-tape devices");
1654b88c807SRodney W. Grimes 
1662a65657fSBartek Rutkowski 	cap_rights_set(&rights, CAP_FTRUNCATE, CAP_IOCTL, CAP_WRITE);
1674b88c807SRodney W. Grimes 	if (out.name == NULL) {
1684b88c807SRodney W. Grimes 		/* No way to check for read access here. */
1694b88c807SRodney W. Grimes 		out.fd = STDOUT_FILENO;
1704b88c807SRodney W. Grimes 		out.name = "stdout";
1714b88c807SRodney W. Grimes 	} else {
1724b88c807SRodney W. Grimes #define	OFLAGS \
1734b88c807SRodney W. Grimes     (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
1744b88c807SRodney W. Grimes 		out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
1754b88c807SRodney W. Grimes 		/*
1764b88c807SRodney W. Grimes 		 * May not have read access, so try again with write only.
1774b88c807SRodney W. Grimes 		 * Without read we may have a problem if output also does
1784b88c807SRodney W. Grimes 		 * not support seeks.
1794b88c807SRodney W. Grimes 		 */
180767bc8adSBrian Feldman 		if (out.fd == -1) {
1814b88c807SRodney W. Grimes 			out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
1824b88c807SRodney W. Grimes 			out.flags |= NOREAD;
1832a65657fSBartek Rutkowski 			cap_rights_clear(&rights, CAP_READ);
1844b88c807SRodney W. Grimes 		}
185767bc8adSBrian Feldman 		if (out.fd == -1)
1864b88c807SRodney W. Grimes 			err(1, "%s", out.name);
1874b88c807SRodney W. Grimes 	}
1884b88c807SRodney W. Grimes 
1894b88c807SRodney W. Grimes 	getfdtype(&out);
1904b88c807SRodney W. Grimes 
1912a65657fSBartek Rutkowski 	if (cap_rights_limit(out.fd, &rights) == -1 && errno != ENOSYS)
1922a65657fSBartek Rutkowski 		err(1, "unable to limit capability rights");
1932a65657fSBartek Rutkowski 	if (cap_ioctls_limit(out.fd, cmds, nitems(cmds)) == -1 &&
1942a65657fSBartek Rutkowski 	    errno != ENOSYS)
1952a65657fSBartek Rutkowski 		err(1, "unable to limit capability rights");
1962a65657fSBartek Rutkowski 
19794161f30SBartek Rutkowski 	if (in.fd != STDIN_FILENO && out.fd != STDIN_FILENO) {
19894161f30SBartek Rutkowski 		if (caph_limit_stdin() == -1)
19994161f30SBartek Rutkowski 			err(1, "unable to limit capability rights");
20094161f30SBartek Rutkowski 	}
20194161f30SBartek Rutkowski 
20294161f30SBartek Rutkowski 	if (in.fd != STDOUT_FILENO && out.fd != STDOUT_FILENO) {
20394161f30SBartek Rutkowski 		if (caph_limit_stdout() == -1)
20494161f30SBartek Rutkowski 			err(1, "unable to limit capability rights");
20594161f30SBartek Rutkowski 	}
20694161f30SBartek Rutkowski 
2072a65657fSBartek Rutkowski 	if (in.fd != STDERR_FILENO && out.fd != STDERR_FILENO) {
2082a65657fSBartek Rutkowski 		if (caph_limit_stderr() == -1)
2092a65657fSBartek Rutkowski 			err(1, "unable to limit capability rights");
2102a65657fSBartek Rutkowski 	}
2112a65657fSBartek Rutkowski 
2124b88c807SRodney W. Grimes 	/*
2134b88c807SRodney W. Grimes 	 * Allocate space for the input and output buffers.  If not doing
2144b88c807SRodney W. Grimes 	 * record oriented I/O, only need a single buffer.
2154b88c807SRodney W. Grimes 	 */
2164b88c807SRodney W. Grimes 	if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
21777a798b3SAlan Somers 		if ((in.db = malloc((size_t)out.dbsz + in.dbsz - 1)) == NULL)
21858687472SBrian Feldman 			err(1, "input buffer");
2194b88c807SRodney W. Grimes 		out.db = in.db;
22077a798b3SAlan Somers 	} else if ((in.db = malloc(MAX((size_t)in.dbsz, cbsz) + cbsz)) == NULL ||
22158687472SBrian Feldman 	    (out.db = malloc(out.dbsz + cbsz)) == NULL)
22258687472SBrian Feldman 		err(1, "output buffer");
223aa7a161bSThomas Quinot 
224aa7a161bSThomas Quinot 	/* dbp is the first free position in each buffer. */
2254b88c807SRodney W. Grimes 	in.dbp = in.db;
2264b88c807SRodney W. Grimes 	out.dbp = out.db;
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes 	/* Position the input/output streams. */
2294b88c807SRodney W. Grimes 	if (in.offset)
2304b88c807SRodney W. Grimes 		pos_in();
2314b88c807SRodney W. Grimes 	if (out.offset)
2324b88c807SRodney W. Grimes 		pos_out();
2334b88c807SRodney W. Grimes 
2344b88c807SRodney W. Grimes 	/*
2355976ee7eSBrian Feldman 	 * Truncate the output file.  If it fails on a type of output file
2365976ee7eSBrian Feldman 	 * that it should _not_ fail on, error out.
2374b88c807SRodney W. Grimes 	 */
238c15c898eSBrian Feldman 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
239c15c898eSBrian Feldman 	    out.flags & ISTRUNC)
240c15c898eSBrian Feldman 		if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
241c15c898eSBrian Feldman 			err(1, "truncating %s", out.name);
2424b88c807SRodney W. Grimes 
2436a3d33acSPoul-Henning Kamp 	if (ddflags & (C_LCASE  | C_UCASE | C_ASCII | C_EBCDIC | C_PARITY)) {
2446a3d33acSPoul-Henning Kamp 		if (ctab != NULL) {
2456a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2466a3d33acSPoul-Henning Kamp 				casetab[cnt] = ctab[cnt];
2476a3d33acSPoul-Henning Kamp 		} else {
2486a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2496a3d33acSPoul-Henning Kamp 				casetab[cnt] = cnt;
2506a3d33acSPoul-Henning Kamp 		}
2516a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARITY) && !(ddflags & C_ASCII)) {
2524b88c807SRodney W. Grimes 			/*
2536a3d33acSPoul-Henning Kamp 			 * If the input is not EBCDIC, and we do parity
2546a3d33acSPoul-Henning Kamp 			 * processing, strip input parity.
2554b88c807SRodney W. Grimes 			 */
2566a3d33acSPoul-Henning Kamp 			for (cnt = 200; cnt <= 0377; ++cnt)
2576a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt & 0x7f];
2586a3d33acSPoul-Henning Kamp 		}
2594b88c807SRodney W. Grimes 		if (ddflags & C_LCASE) {
260c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2616a3d33acSPoul-Henning Kamp 				casetab[cnt] = tolower(casetab[cnt]);
2626a3d33acSPoul-Henning Kamp 		} else if (ddflags & C_UCASE) {
263c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2646a3d33acSPoul-Henning Kamp 				casetab[cnt] = toupper(casetab[cnt]);
2654b88c807SRodney W. Grimes 		}
2666a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARITY)) {
2676a3d33acSPoul-Henning Kamp 			/*
2686a3d33acSPoul-Henning Kamp 			 * This should strictly speaking be a no-op, but I
2696a3d33acSPoul-Henning Kamp 			 * wonder what funny LANG settings could get us.
2706a3d33acSPoul-Henning Kamp 			 */
271c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2726a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt] & 0x7f;
2736a3d33acSPoul-Henning Kamp 		}
2746a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARSET)) {
275c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2766a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt] | 0x80;
277dde07463SAndrey A. Chernov 		}
2786a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PAREVEN)) {
2796a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2806a3d33acSPoul-Henning Kamp 				if (parity(casetab[cnt]))
2816a3d33acSPoul-Henning Kamp 					casetab[cnt] = casetab[cnt] | 0x80;
282dde07463SAndrey A. Chernov 		}
2836a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARODD)) {
2846a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2856a3d33acSPoul-Henning Kamp 				if (!parity(casetab[cnt]))
2866a3d33acSPoul-Henning Kamp 					casetab[cnt] = casetab[cnt] | 0x80;
2876a3d33acSPoul-Henning Kamp 		}
2886a3d33acSPoul-Henning Kamp 
28958687472SBrian Feldman 		ctab = casetab;
290426e9c1dSWarner Losh 	}
29158687472SBrian Feldman 
292540c7825SAlan Somers 	if (clock_gettime(CLOCK_MONOTONIC, &st.start))
293540c7825SAlan Somers 		err(1, "clock_gettime");
2944b88c807SRodney W. Grimes }
2954b88c807SRodney W. Grimes 
2964b88c807SRodney W. Grimes static void
297f9bcb0beSWarner Losh getfdtype(IO *io)
2984b88c807SRodney W. Grimes {
2994b88c807SRodney W. Grimes 	struct stat sb;
300e08d7384SBrian Feldman 	int type;
3014b88c807SRodney W. Grimes 
30258687472SBrian Feldman 	if (fstat(io->fd, &sb) == -1)
3034b88c807SRodney W. Grimes 		err(1, "%s", io->name);
304c15c898eSBrian Feldman 	if (S_ISREG(sb.st_mode))
305c15c898eSBrian Feldman 		io->flags |= ISTRUNC;
306e08d7384SBrian Feldman 	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
307ff8bb989SBrian Feldman 		if (ioctl(io->fd, FIODTYPE, &type) == -1) {
308bf3367d0SBrian Feldman 			err(1, "%s", io->name);
309ff8bb989SBrian Feldman 		} else {
310e08d7384SBrian Feldman 			if (type & D_TAPE)
311e08d7384SBrian Feldman 				io->flags |= ISTAPE;
312cd967e32SPoul-Henning Kamp 			else if (type & (D_DISK | D_MEM))
31332952d4bSBrian Feldman 				io->flags |= ISSEEK;
314e08d7384SBrian Feldman 			if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
315e08d7384SBrian Feldman 				io->flags |= ISCHR;
316ff8bb989SBrian Feldman 		}
3175ff6541eSBrian Feldman 		return;
3185ff6541eSBrian Feldman 	}
3195ff6541eSBrian Feldman 	errno = 0;
3205ff6541eSBrian Feldman 	if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
321e08d7384SBrian Feldman 		io->flags |= ISPIPE;
3225ff6541eSBrian Feldman 	else
3235ff6541eSBrian Feldman 		io->flags |= ISSEEK;
3244b88c807SRodney W. Grimes }
3254b88c807SRodney W. Grimes 
32675e55112SEdward Tomasz Napierala /*
32775e55112SEdward Tomasz Napierala  * Limit the speed by adding a delay before every block read.
32875e55112SEdward Tomasz Napierala  * The delay (t_usleep) is equal to the time computed from block
32975e55112SEdward Tomasz Napierala  * size and the specified speed limit (t_target) minus the time
33075e55112SEdward Tomasz Napierala  * spent on actual read and write operations (t_io).
33175e55112SEdward Tomasz Napierala  */
33275e55112SEdward Tomasz Napierala static void
33375e55112SEdward Tomasz Napierala speed_limit(void)
33475e55112SEdward Tomasz Napierala {
33575e55112SEdward Tomasz Napierala 	static double t_prev, t_usleep;
33675e55112SEdward Tomasz Napierala 	double t_now, t_io, t_target;
33775e55112SEdward Tomasz Napierala 
33875e55112SEdward Tomasz Napierala 	t_now = secs_elapsed();
33975e55112SEdward Tomasz Napierala 	t_io = t_now - t_prev - t_usleep;
34075e55112SEdward Tomasz Napierala 	t_target = (double)in.dbsz / (double)speed;
34175e55112SEdward Tomasz Napierala 	t_usleep = t_target - t_io;
34275e55112SEdward Tomasz Napierala 	if (t_usleep > 0)
34375e55112SEdward Tomasz Napierala 		usleep(t_usleep * 1000000);
34475e55112SEdward Tomasz Napierala 	else
34575e55112SEdward Tomasz Napierala 		t_usleep = 0;
34675e55112SEdward Tomasz Napierala 	t_prev = t_now;
34775e55112SEdward Tomasz Napierala }
34875e55112SEdward Tomasz Napierala 
3494b88c807SRodney W. Grimes static void
35044e0a832SEitan Adler swapbytes(void *v, size_t len)
35144e0a832SEitan Adler {
35244e0a832SEitan Adler 	unsigned char *p = v;
35344e0a832SEitan Adler 	unsigned char t;
35444e0a832SEitan Adler 
35544e0a832SEitan Adler 	while (len > 1) {
35644e0a832SEitan Adler 		t = p[0];
35744e0a832SEitan Adler 		p[0] = p[1];
35844e0a832SEitan Adler 		p[1] = t;
35944e0a832SEitan Adler 		p += 2;
36044e0a832SEitan Adler 		len -= 2;
36144e0a832SEitan Adler 	}
36244e0a832SEitan Adler }
36344e0a832SEitan Adler 
36444e0a832SEitan Adler static void
365f9bcb0beSWarner Losh dd_in(void)
3664b88c807SRodney W. Grimes {
367767bc8adSBrian Feldman 	ssize_t n;
3684b88c807SRodney W. Grimes 
369af041bf3SJonathan Lemon 	for (;;) {
3705ff6541eSBrian Feldman 		switch (cpy_cnt) {
3715ff6541eSBrian Feldman 		case -1:			/* count=0 was specified */
3724b88c807SRodney W. Grimes 			return;
3735ff6541eSBrian Feldman 		case 0:
3745ff6541eSBrian Feldman 			break;
3755ff6541eSBrian Feldman 		default:
3767503d74fSMark Murray 			if (st.in_full + st.in_part >= (uintmax_t)cpy_cnt)
3775ff6541eSBrian Feldman 				return;
3785ff6541eSBrian Feldman 			break;
3795ff6541eSBrian Feldman 		}
3804b88c807SRodney W. Grimes 
38175e55112SEdward Tomasz Napierala 		if (speed > 0)
38275e55112SEdward Tomasz Napierala 			speed_limit();
38375e55112SEdward Tomasz Napierala 
3844b88c807SRodney W. Grimes 		/*
38558687472SBrian Feldman 		 * Zero the buffer first if sync; if doing block operations,
3864b88c807SRodney W. Grimes 		 * use spaces.
3874b88c807SRodney W. Grimes 		 */
388767bc8adSBrian Feldman 		if (ddflags & C_SYNC) {
389e3edab4aSRobert Watson 			if (ddflags & C_FILL)
390e3edab4aSRobert Watson 				memset(in.dbp, fill_char, in.dbsz);
391e3edab4aSRobert Watson 			else if (ddflags & (C_BLOCK | C_UNBLOCK))
3924b88c807SRodney W. Grimes 				memset(in.dbp, ' ', in.dbsz);
3934b88c807SRodney W. Grimes 			else
3944b88c807SRodney W. Grimes 				memset(in.dbp, 0, in.dbsz);
395767bc8adSBrian Feldman 		}
3964b88c807SRodney W. Grimes 
3974b88c807SRodney W. Grimes 		n = read(in.fd, in.dbp, in.dbsz);
3984b88c807SRodney W. Grimes 		if (n == 0) {
3994b88c807SRodney W. Grimes 			in.dbrcnt = 0;
4004b88c807SRodney W. Grimes 			return;
4014b88c807SRodney W. Grimes 		}
4024b88c807SRodney W. Grimes 
4034b88c807SRodney W. Grimes 		/* Read error. */
404767bc8adSBrian Feldman 		if (n == -1) {
4054b88c807SRodney W. Grimes 			/*
4064b88c807SRodney W. Grimes 			 * If noerror not specified, die.  POSIX requires that
4074b88c807SRodney W. Grimes 			 * the warning message be followed by an I/O display.
4084b88c807SRodney W. Grimes 			 */
409af041bf3SJonathan Lemon 			if (!(ddflags & C_NOERROR))
4104b88c807SRodney W. Grimes 				err(1, "%s", in.name);
4114b88c807SRodney W. Grimes 			warn("%s", in.name);
4124b88c807SRodney W. Grimes 			summary();
4134b88c807SRodney W. Grimes 
4144b88c807SRodney W. Grimes 			/*
4157599187eSBrian Feldman 			 * If it's a seekable file descriptor, seek past the
4164b88c807SRodney W. Grimes 			 * error.  If your OS doesn't do the right thing for
4174b88c807SRodney W. Grimes 			 * raw disks this section should be modified to re-read
4184b88c807SRodney W. Grimes 			 * in sector size chunks.
4194b88c807SRodney W. Grimes 			 */
4207599187eSBrian Feldman 			if (in.flags & ISSEEK &&
4214b88c807SRodney W. Grimes 			    lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
4224b88c807SRodney W. Grimes 				warn("%s", in.name);
4234b88c807SRodney W. Grimes 
4244b88c807SRodney W. Grimes 			/* If sync not specified, omit block and continue. */
4254b88c807SRodney W. Grimes 			if (!(ddflags & C_SYNC))
4264b88c807SRodney W. Grimes 				continue;
4274b88c807SRodney W. Grimes 
4284b88c807SRodney W. Grimes 			/* Read errors count as full blocks. */
4294b88c807SRodney W. Grimes 			in.dbcnt += in.dbrcnt = in.dbsz;
4304b88c807SRodney W. Grimes 			++st.in_full;
4314b88c807SRodney W. Grimes 
4324b88c807SRodney W. Grimes 		/* Handle full input blocks. */
43377a798b3SAlan Somers 		} else if ((size_t)n == (size_t)in.dbsz) {
4344b88c807SRodney W. Grimes 			in.dbcnt += in.dbrcnt = n;
4354b88c807SRodney W. Grimes 			++st.in_full;
4364b88c807SRodney W. Grimes 
4374b88c807SRodney W. Grimes 		/* Handle partial input blocks. */
4384b88c807SRodney W. Grimes 		} else {
4394b88c807SRodney W. Grimes 			/* If sync, use the entire block. */
4404b88c807SRodney W. Grimes 			if (ddflags & C_SYNC)
4414b88c807SRodney W. Grimes 				in.dbcnt += in.dbrcnt = in.dbsz;
4424b88c807SRodney W. Grimes 			else
4434b88c807SRodney W. Grimes 				in.dbcnt += in.dbrcnt = n;
4444b88c807SRodney W. Grimes 			++st.in_part;
4454b88c807SRodney W. Grimes 		}
4464b88c807SRodney W. Grimes 
4474b88c807SRodney W. Grimes 		/*
4484b88c807SRodney W. Grimes 		 * POSIX states that if bs is set and no other conversions
4494b88c807SRodney W. Grimes 		 * than noerror, notrunc or sync are specified, the block
4504b88c807SRodney W. Grimes 		 * is output without buffering as it is read.
4514b88c807SRodney W. Grimes 		 */
4526784d416SKonstantin Belousov 		if ((ddflags & ~(C_NOERROR | C_NOTRUNC | C_SYNC)) == C_BS) {
4534b88c807SRodney W. Grimes 			out.dbcnt = in.dbcnt;
4544b88c807SRodney W. Grimes 			dd_out(1);
4554b88c807SRodney W. Grimes 			in.dbcnt = 0;
4564b88c807SRodney W. Grimes 			continue;
4574b88c807SRodney W. Grimes 		}
4584b88c807SRodney W. Grimes 
4594b88c807SRodney W. Grimes 		if (ddflags & C_SWAB) {
460a28ea077SJoerg Wunsch 			if ((n = in.dbrcnt) & 1) {
4614b88c807SRodney W. Grimes 				++st.swab;
4624b88c807SRodney W. Grimes 				--n;
4634b88c807SRodney W. Grimes 			}
46444e0a832SEitan Adler 			swapbytes(in.dbp, (size_t)n);
4654b88c807SRodney W. Grimes 		}
4664b88c807SRodney W. Grimes 
4674b88c807SRodney W. Grimes 		in.dbp += in.dbrcnt;
4684b88c807SRodney W. Grimes 		(*cfunc)();
469*8acbb227SKyle Evans 		if (need_summary)
4704ac11639SEitan Adler 			summary();
471*8acbb227SKyle Evans 		if (need_progress)
4724767c42cSKyle Evans 			progress();
4734767c42cSKyle Evans 	}
4744b88c807SRodney W. Grimes }
4754b88c807SRodney W. Grimes 
4764b88c807SRodney W. Grimes /*
47758687472SBrian Feldman  * Clean up any remaining I/O and flush output.  If necessary, the output file
4784b88c807SRodney W. Grimes  * is truncated.
4794b88c807SRodney W. Grimes  */
4804b88c807SRodney W. Grimes static void
481f9bcb0beSWarner Losh dd_close(void)
4824b88c807SRodney W. Grimes {
4834b88c807SRodney W. Grimes 	if (cfunc == def)
4844b88c807SRodney W. Grimes 		def_close();
4854b88c807SRodney W. Grimes 	else if (cfunc == block)
4864b88c807SRodney W. Grimes 		block_close();
4874b88c807SRodney W. Grimes 	else if (cfunc == unblock)
4884b88c807SRodney W. Grimes 		unblock_close();
489af041bf3SJonathan Lemon 	if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
490e3edab4aSRobert Watson 		if (ddflags & C_FILL)
491e3edab4aSRobert Watson 			memset(out.dbp, fill_char, out.dbsz - out.dbcnt);
492e3edab4aSRobert Watson 		else if (ddflags & (C_BLOCK | C_UNBLOCK))
493af041bf3SJonathan Lemon 			memset(out.dbp, ' ', out.dbsz - out.dbcnt);
494af041bf3SJonathan Lemon 		else
4954b88c807SRodney W. Grimes 			memset(out.dbp, 0, out.dbsz - out.dbcnt);
4964b88c807SRodney W. Grimes 		out.dbcnt = out.dbsz;
4974b88c807SRodney W. Grimes 	}
4984ddfeabdSJoerg Wunsch 	if (out.dbcnt || pending)
4994b88c807SRodney W. Grimes 		dd_out(1);
500cd1832feSThomas Quinot 
501cd1832feSThomas Quinot 	/*
502cd1832feSThomas Quinot 	 * If the file ends with a hole, ftruncate it to extend its size
503cd1832feSThomas Quinot 	 * up to the end of the hole (without having to write any data).
504cd1832feSThomas Quinot 	 */
505cd1832feSThomas Quinot 	if (out.seek_offset > 0 && (out.flags & ISTRUNC)) {
506cd1832feSThomas Quinot 		if (ftruncate(out.fd, out.seek_offset) == -1)
507cd1832feSThomas Quinot 			err(1, "truncating %s", out.name);
508cd1832feSThomas Quinot 	}
5094b88c807SRodney W. Grimes }
5104b88c807SRodney W. Grimes 
5114b88c807SRodney W. Grimes void
512f9bcb0beSWarner Losh dd_out(int force)
5134b88c807SRodney W. Grimes {
5144b88c807SRodney W. Grimes 	u_char *outp;
51558687472SBrian Feldman 	size_t cnt, i, n;
51658687472SBrian Feldman 	ssize_t nw;
51758687472SBrian Feldman 	static int warned;
51858687472SBrian Feldman 	int sparse;
5194b88c807SRodney W. Grimes 
5204b88c807SRodney W. Grimes 	/*
5214b88c807SRodney W. Grimes 	 * Write one or more blocks out.  The common case is writing a full
5224b88c807SRodney W. Grimes 	 * output block in a single write; increment the full block stats.
5234b88c807SRodney W. Grimes 	 * Otherwise, we're into partial block writes.  If a partial write,
5244b88c807SRodney W. Grimes 	 * and it's a character device, just warn.  If a tape device, quit.
5254b88c807SRodney W. Grimes 	 *
5264b88c807SRodney W. Grimes 	 * The partial writes represent two cases.  1: Where the input block
5274b88c807SRodney W. Grimes 	 * was less than expected so the output block was less than expected.
5284b88c807SRodney W. Grimes 	 * 2: Where the input block was the right size but we were forced to
5294b88c807SRodney W. Grimes 	 * write the block in multiple chunks.  The original versions of dd(1)
5304b88c807SRodney W. Grimes 	 * never wrote a block in more than a single write, so the latter case
5314b88c807SRodney W. Grimes 	 * never happened.
5324b88c807SRodney W. Grimes 	 *
5334b88c807SRodney W. Grimes 	 * One special case is if we're forced to do the write -- in that case
5344b88c807SRodney W. Grimes 	 * we play games with the buffer size, and it's usually a partial write.
5354b88c807SRodney W. Grimes 	 */
5364b88c807SRodney W. Grimes 	outp = out.db;
537aa7a161bSThomas Quinot 
538aa7a161bSThomas Quinot 	/*
539aa7a161bSThomas Quinot 	 * If force, first try to write all pending data, else try to write
540aa7a161bSThomas Quinot 	 * just one block. Subsequently always write data one full block at
541aa7a161bSThomas Quinot 	 * a time at most.
542aa7a161bSThomas Quinot 	 */
5434b88c807SRodney W. Grimes 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
544aa7a161bSThomas Quinot 		cnt = n;
545aa7a161bSThomas Quinot 		do {
5464ddfeabdSJoerg Wunsch 			sparse = 0;
5474ddfeabdSJoerg Wunsch 			if (ddflags & C_SPARSE) {
5484ddfeabdSJoerg Wunsch 				sparse = 1;	/* Is buffer sparse? */
5494ddfeabdSJoerg Wunsch 				for (i = 0; i < cnt; i++)
5504ddfeabdSJoerg Wunsch 					if (outp[i] != 0) {
5514ddfeabdSJoerg Wunsch 						sparse = 0;
5524ddfeabdSJoerg Wunsch 						break;
5534ddfeabdSJoerg Wunsch 					}
5544ddfeabdSJoerg Wunsch 			}
5554ddfeabdSJoerg Wunsch 			if (sparse && !force) {
5564ddfeabdSJoerg Wunsch 				pending += cnt;
5574ddfeabdSJoerg Wunsch 				nw = cnt;
5584ddfeabdSJoerg Wunsch 			} else {
5594ddfeabdSJoerg Wunsch 				if (pending != 0) {
560cd1832feSThomas Quinot 					/*
561cd1832feSThomas Quinot 					 * Seek past hole.  Note that we need to record the
562cd1832feSThomas Quinot 					 * reached offset, because we might have no more data
563cd1832feSThomas Quinot 					 * to write, in which case we'll need to call
564cd1832feSThomas Quinot 					 * ftruncate to extend the file size.
565aa7a161bSThomas Quinot 					 */
566cd1832feSThomas Quinot 					out.seek_offset = lseek(out.fd, pending, SEEK_CUR);
567cd1832feSThomas Quinot 					if (out.seek_offset == -1)
56854946e00SBrian Feldman 						err(2, "%s: seek error creating sparse file",
56954946e00SBrian Feldman 						    out.name);
570cd1832feSThomas Quinot 					pending = 0;
5714ddfeabdSJoerg Wunsch 				}
572cd1832feSThomas Quinot 				if (cnt) {
5734b88c807SRodney W. Grimes 					nw = write(out.fd, outp, cnt);
574cd1832feSThomas Quinot 					out.seek_offset = 0;
575cd1832feSThomas Quinot 				} else {
5764ddfeabdSJoerg Wunsch 					return;
5774ddfeabdSJoerg Wunsch 				}
578cd1832feSThomas Quinot 			}
5794ddfeabdSJoerg Wunsch 
5804b88c807SRodney W. Grimes 			if (nw <= 0) {
5814b88c807SRodney W. Grimes 				if (nw == 0)
5824b88c807SRodney W. Grimes 					errx(1, "%s: end of device", out.name);
5834b88c807SRodney W. Grimes 				if (errno != EINTR)
5844b88c807SRodney W. Grimes 					err(1, "%s", out.name);
5854b88c807SRodney W. Grimes 				nw = 0;
5864b88c807SRodney W. Grimes 			}
587aa7a161bSThomas Quinot 
5884b88c807SRodney W. Grimes 			outp += nw;
5894b88c807SRodney W. Grimes 			st.bytes += nw;
590aa7a161bSThomas Quinot 
59177a798b3SAlan Somers 			if ((size_t)nw == n && n == (size_t)out.dbsz)
5924b88c807SRodney W. Grimes 				++st.out_full;
593aa7a161bSThomas Quinot 			else
5944b88c807SRodney W. Grimes 				++st.out_part;
595aa7a161bSThomas Quinot 
596aa7a161bSThomas Quinot 			if ((size_t) nw != cnt) {
5977599187eSBrian Feldman 				if (out.flags & ISTAPE)
5987599187eSBrian Feldman 					errx(1, "%s: short write on tape device",
5997599187eSBrian Feldman 				    	out.name);
6004b88c807SRodney W. Grimes 				if (out.flags & ISCHR && !warned) {
6014b88c807SRodney W. Grimes 					warned = 1;
6024b88c807SRodney W. Grimes 					warnx("%s: short write on character device",
6034b88c807SRodney W. Grimes 				    	out.name);
6044b88c807SRodney W. Grimes 				}
6054b88c807SRodney W. Grimes 			}
606aa7a161bSThomas Quinot 
607aa7a161bSThomas Quinot 			cnt -= nw;
608aa7a161bSThomas Quinot 		} while (cnt != 0);
609aa7a161bSThomas Quinot 
6104b88c807SRodney W. Grimes 		if ((out.dbcnt -= n) < out.dbsz)
6114b88c807SRodney W. Grimes 			break;
6124b88c807SRodney W. Grimes 	}
6134b88c807SRodney W. Grimes 
6144b88c807SRodney W. Grimes 	/* Reassemble the output block. */
6154b88c807SRodney W. Grimes 	if (out.dbcnt)
61658687472SBrian Feldman 		(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
6174b88c807SRodney W. Grimes 	out.dbp = out.db + out.dbcnt;
6184b88c807SRodney W. Grimes }
619