xref: /freebsd/bin/dd/dd.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
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 
364b88c807SRodney W. Grimes #include <sys/param.h>
374b88c807SRodney W. Grimes #include <sys/stat.h>
382a65657fSBartek Rutkowski #include <sys/capsicum.h>
39e08d7384SBrian Feldman #include <sys/conf.h>
40015a53cfSDavid E. O'Brien #include <sys/disklabel.h>
41e08d7384SBrian Feldman #include <sys/filio.h>
422a65657fSBartek Rutkowski #include <sys/mtio.h>
434767c42cSKyle Evans #include <sys/time.h>
444b88c807SRodney W. Grimes 
45aa7a161bSThomas Quinot #include <assert.h>
462a65657fSBartek Rutkowski #include <capsicum_helpers.h>
474b88c807SRodney W. Grimes #include <ctype.h>
484b88c807SRodney W. Grimes #include <err.h>
494b88c807SRodney W. Grimes #include <errno.h>
504b88c807SRodney W. Grimes #include <fcntl.h>
517503d74fSMark Murray #include <inttypes.h>
52ad66f7eeSPoul-Henning Kamp #include <locale.h>
535807f35cSDag-Erling Smørgrav #include <signal.h>
544b88c807SRodney W. Grimes #include <stdio.h>
554b88c807SRodney W. Grimes #include <stdlib.h>
564b88c807SRodney W. Grimes #include <string.h>
57d1d66eacSAlan Somers #include <time.h>
584b88c807SRodney W. Grimes #include <unistd.h>
594b88c807SRodney W. Grimes 
604b88c807SRodney W. Grimes #include "dd.h"
614b88c807SRodney W. Grimes #include "extern.h"
624b88c807SRodney W. Grimes 
63f9bcb0beSWarner Losh static void dd_close(void);
64f9bcb0beSWarner Losh static void dd_in(void);
65f9bcb0beSWarner Losh static void getfdtype(IO *);
66f9bcb0beSWarner Losh static void setup(void);
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes IO	in, out;		/* input/output state */
694b88c807SRodney W. Grimes STAT	st;			/* statistics */
70f9bcb0beSWarner Losh void	(*cfunc)(void);		/* conversion function */
717503d74fSMark Murray uintmax_t cpy_cnt;		/* # of blocks to copy */
729afa09cdSMark Murray static off_t	pending = 0;	/* pending seek if sparse */
733b96efbdSMatt Macy uint64_t	ddflags = 0;	/* conversion options */
7458687472SBrian Feldman size_t	cbsz;			/* conversion block size */
757503d74fSMark Murray uintmax_t files_cnt = 1;	/* # of files to copy */
7658687472SBrian Feldman const	u_char *ctab;		/* conversion table */
77e3edab4aSRobert Watson char	fill_char;		/* Character to fill with if defined */
7875e55112SEdward Tomasz Napierala size_t	speed = 0;		/* maximum speed, in bytes per second */
794ac11639SEitan Adler volatile sig_atomic_t need_summary;
804767c42cSKyle Evans volatile sig_atomic_t need_progress;
815807f35cSDag-Erling Smørgrav volatile sig_atomic_t kill_signal;
824b88c807SRodney W. Grimes 
834b88c807SRodney W. Grimes int
main(int argc __unused,char * argv[])84f9bcb0beSWarner Losh main(int argc __unused, char *argv[])
854b88c807SRodney W. Grimes {
868acbb227SKyle Evans 	struct itimerval itv = { { 1, 0 }, { 1, 0 } }; /* SIGALARM every second, if needed */
878acbb227SKyle Evans 
88*8dad5eceSKonstantin Belousov 	prepare_io();
895807f35cSDag-Erling Smørgrav 
90f7c627b2SAndrey A. Chernov 	(void)setlocale(LC_CTYPE, "");
914b88c807SRodney W. Grimes 	jcl(argv);
924b88c807SRodney W. Grimes 	setup();
934b88c807SRodney W. Grimes 
942a65657fSBartek Rutkowski 	caph_cache_catpages();
957672a014SMariusz Zaborski 	if (caph_enter() < 0)
962a65657fSBartek Rutkowski 		err(1, "unable to enter capability mode");
972a65657fSBartek Rutkowski 
984ac11639SEitan Adler 	(void)signal(SIGINFO, siginfo_handler);
998acbb227SKyle Evans 	if (ddflags & C_PROGRESS) {
1008acbb227SKyle Evans 		(void)signal(SIGALRM, sigalarm_handler);
1018acbb227SKyle Evans 		setitimer(ITIMER_REAL, &itv, NULL);
1028acbb227SKyle Evans 	}
1034b88c807SRodney W. Grimes 
1044b88c807SRodney W. Grimes 	atexit(summary);
1054b88c807SRodney W. Grimes 
1064b88c807SRodney W. Grimes 	while (files_cnt--)
1074b88c807SRodney W. Grimes 		dd_in();
1084b88c807SRodney W. Grimes 
1094b88c807SRodney W. Grimes 	dd_close();
110c183a03bSBrooks Davis 	/*
111c183a03bSBrooks Davis 	 * Some devices such as cfi(4) may perform significant amounts
112c183a03bSBrooks Davis 	 * of work when a write descriptor is closed.  Close the out
113c183a03bSBrooks Davis 	 * descriptor explicitly so that the summary handler (called
114c183a03bSBrooks Davis 	 * from an atexit() hook) includes this work.
115c183a03bSBrooks Davis 	 */
1162048fe70SMatt Macy 	if (close(out.fd) == -1 && errno != EINTR)
1172048fe70SMatt Macy 		err(1, "close");
1184b88c807SRodney W. Grimes 	exit(0);
1194b88c807SRodney W. Grimes }
1204b88c807SRodney W. Grimes 
1216a3d33acSPoul-Henning Kamp static int
parity(u_char c)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
setup(void)132f9bcb0beSWarner Losh setup(void)
1334b88c807SRodney W. Grimes {
1344b88c807SRodney W. Grimes 	u_int cnt;
135f4b4526fSRichard Scheffenegger 	int iflags, oflags;
1362a65657fSBartek Rutkowski 	cap_rights_t rights;
1372a65657fSBartek Rutkowski 	unsigned long cmds[] = { FIODTYPE, MTIOCTOP };
1384b88c807SRodney W. Grimes 
1394b88c807SRodney W. Grimes 	if (in.name == NULL) {
1404b88c807SRodney W. Grimes 		in.name = "stdin";
1414b88c807SRodney W. Grimes 		in.fd = STDIN_FILENO;
1424b88c807SRodney W. Grimes 	} else {
143f4b4526fSRichard Scheffenegger 		iflags = 0;
144f4b4526fSRichard Scheffenegger 		if (ddflags & C_IDIRECT)
145f4b4526fSRichard Scheffenegger 			iflags |= O_DIRECT;
146*8dad5eceSKonstantin Belousov 		before_io();
147f4b4526fSRichard Scheffenegger 		in.fd = open(in.name, O_RDONLY | iflags, 0);
148*8dad5eceSKonstantin Belousov 		after_io();
149767bc8adSBrian Feldman 		if (in.fd == -1)
1504b88c807SRodney W. Grimes 			err(1, "%s", in.name);
1514b88c807SRodney W. Grimes 	}
1524b88c807SRodney W. Grimes 
1534b88c807SRodney W. Grimes 	getfdtype(&in);
1544b88c807SRodney W. Grimes 
1552a65657fSBartek Rutkowski 	cap_rights_init(&rights, CAP_READ, CAP_SEEK);
156377421dfSMariusz Zaborski 	if (caph_rights_limit(in.fd, &rights) == -1)
1572a65657fSBartek Rutkowski 		err(1, "unable to limit capability rights");
1582a65657fSBartek Rutkowski 
1594b88c807SRodney W. Grimes 	if (files_cnt > 1 && !(in.flags & ISTAPE))
1604b88c807SRodney W. Grimes 		errx(1, "files is not supported for non-tape devices");
1614b88c807SRodney W. Grimes 
1622a65657fSBartek Rutkowski 	cap_rights_set(&rights, CAP_FTRUNCATE, CAP_IOCTL, CAP_WRITE);
1633b96efbdSMatt Macy 	if (ddflags & (C_FDATASYNC | C_FSYNC))
164ce1b19d8SMatt Macy 		cap_rights_set(&rights, CAP_FSYNC);
1654b88c807SRodney W. Grimes 	if (out.name == NULL) {
1664b88c807SRodney W. Grimes 		/* No way to check for read access here. */
1674b88c807SRodney W. Grimes 		out.fd = STDOUT_FILENO;
1684b88c807SRodney W. Grimes 		out.name = "stdout";
169919156e3SMatt Macy 		if (ddflags & C_OFSYNC) {
170919156e3SMatt Macy 			oflags = fcntl(out.fd, F_GETFL);
171919156e3SMatt Macy 			if (oflags == -1)
172919156e3SMatt Macy 				err(1, "unable to get fd flags for stdout");
173919156e3SMatt Macy 			oflags |= O_FSYNC;
174919156e3SMatt Macy 			if (fcntl(out.fd, F_SETFL, oflags) == -1)
175919156e3SMatt Macy 				err(1, "unable to set fd flags for stdout");
176919156e3SMatt Macy 		}
1774b88c807SRodney W. Grimes 	} else {
178919156e3SMatt Macy 		oflags = O_CREAT;
179919156e3SMatt Macy 		if (!(ddflags & (C_SEEK | C_NOTRUNC)))
180919156e3SMatt Macy 			oflags |= O_TRUNC;
181919156e3SMatt Macy 		if (ddflags & C_OFSYNC)
182919156e3SMatt Macy 			oflags |= O_FSYNC;
183f4b4526fSRichard Scheffenegger 		if (ddflags & C_ODIRECT)
184f4b4526fSRichard Scheffenegger 			oflags |= O_DIRECT;
185*8dad5eceSKonstantin Belousov 		before_io();
186919156e3SMatt Macy 		out.fd = open(out.name, O_RDWR | oflags, DEFFILEMODE);
187*8dad5eceSKonstantin Belousov 		after_io();
1884b88c807SRodney W. Grimes 		/*
1894b88c807SRodney W. Grimes 		 * May not have read access, so try again with write only.
1904b88c807SRodney W. Grimes 		 * Without read we may have a problem if output also does
1914b88c807SRodney W. Grimes 		 * not support seeks.
1924b88c807SRodney W. Grimes 		 */
193767bc8adSBrian Feldman 		if (out.fd == -1) {
194*8dad5eceSKonstantin Belousov 			before_io();
195919156e3SMatt Macy 			out.fd = open(out.name, O_WRONLY | oflags, DEFFILEMODE);
196*8dad5eceSKonstantin Belousov 			after_io();
1974b88c807SRodney W. Grimes 			out.flags |= NOREAD;
1982a65657fSBartek Rutkowski 			cap_rights_clear(&rights, CAP_READ);
1994b88c807SRodney W. Grimes 		}
200767bc8adSBrian Feldman 		if (out.fd == -1)
2014b88c807SRodney W. Grimes 			err(1, "%s", out.name);
2024b88c807SRodney W. Grimes 	}
2034b88c807SRodney W. Grimes 
2044b88c807SRodney W. Grimes 	getfdtype(&out);
2054b88c807SRodney W. Grimes 
206377421dfSMariusz Zaborski 	if (caph_rights_limit(out.fd, &rights) == -1)
2072a65657fSBartek Rutkowski 		err(1, "unable to limit capability rights");
208377421dfSMariusz Zaborski 	if (caph_ioctls_limit(out.fd, cmds, nitems(cmds)) == -1)
2092a65657fSBartek Rutkowski 		err(1, "unable to limit capability rights");
2102a65657fSBartek Rutkowski 
21194161f30SBartek Rutkowski 	if (in.fd != STDIN_FILENO && out.fd != STDIN_FILENO) {
21294161f30SBartek Rutkowski 		if (caph_limit_stdin() == -1)
21394161f30SBartek Rutkowski 			err(1, "unable to limit capability rights");
21494161f30SBartek Rutkowski 	}
21594161f30SBartek Rutkowski 
21694161f30SBartek Rutkowski 	if (in.fd != STDOUT_FILENO && out.fd != STDOUT_FILENO) {
21794161f30SBartek Rutkowski 		if (caph_limit_stdout() == -1)
21894161f30SBartek Rutkowski 			err(1, "unable to limit capability rights");
21994161f30SBartek Rutkowski 	}
22094161f30SBartek Rutkowski 
2212a65657fSBartek Rutkowski 	if (in.fd != STDERR_FILENO && out.fd != STDERR_FILENO) {
2222a65657fSBartek Rutkowski 		if (caph_limit_stderr() == -1)
2232a65657fSBartek Rutkowski 			err(1, "unable to limit capability rights");
2242a65657fSBartek Rutkowski 	}
2252a65657fSBartek Rutkowski 
2264b88c807SRodney W. Grimes 	/*
2274b88c807SRodney W. Grimes 	 * Allocate space for the input and output buffers.  If not doing
2284b88c807SRodney W. Grimes 	 * record oriented I/O, only need a single buffer.
2294b88c807SRodney W. Grimes 	 */
2304b88c807SRodney W. Grimes 	if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
23177a798b3SAlan Somers 		if ((in.db = malloc((size_t)out.dbsz + in.dbsz - 1)) == NULL)
23258687472SBrian Feldman 			err(1, "input buffer");
2334b88c807SRodney W. Grimes 		out.db = in.db;
23477a798b3SAlan Somers 	} else if ((in.db = malloc(MAX((size_t)in.dbsz, cbsz) + cbsz)) == NULL ||
23558687472SBrian Feldman 	    (out.db = malloc(out.dbsz + cbsz)) == NULL)
23658687472SBrian Feldman 		err(1, "output buffer");
237aa7a161bSThomas Quinot 
238aa7a161bSThomas Quinot 	/* dbp is the first free position in each buffer. */
2394b88c807SRodney W. Grimes 	in.dbp = in.db;
2404b88c807SRodney W. Grimes 	out.dbp = out.db;
2414b88c807SRodney W. Grimes 
2424b88c807SRodney W. Grimes 	/* Position the input/output streams. */
2434b88c807SRodney W. Grimes 	if (in.offset)
2444b88c807SRodney W. Grimes 		pos_in();
2454b88c807SRodney W. Grimes 	if (out.offset)
2464b88c807SRodney W. Grimes 		pos_out();
2474b88c807SRodney W. Grimes 
2484b88c807SRodney W. Grimes 	/*
2495976ee7eSBrian Feldman 	 * Truncate the output file.  If it fails on a type of output file
2505976ee7eSBrian Feldman 	 * that it should _not_ fail on, error out.
2514b88c807SRodney W. Grimes 	 */
252c15c898eSBrian Feldman 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
253c15c898eSBrian Feldman 	    out.flags & ISTRUNC)
254c15c898eSBrian Feldman 		if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
255c15c898eSBrian Feldman 			err(1, "truncating %s", out.name);
2564b88c807SRodney W. Grimes 
2576a3d33acSPoul-Henning Kamp 	if (ddflags & (C_LCASE  | C_UCASE | C_ASCII | C_EBCDIC | C_PARITY)) {
2586a3d33acSPoul-Henning Kamp 		if (ctab != NULL) {
2596a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2606a3d33acSPoul-Henning Kamp 				casetab[cnt] = ctab[cnt];
2616a3d33acSPoul-Henning Kamp 		} else {
2626a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2636a3d33acSPoul-Henning Kamp 				casetab[cnt] = cnt;
2646a3d33acSPoul-Henning Kamp 		}
2656a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARITY) && !(ddflags & C_ASCII)) {
2664b88c807SRodney W. Grimes 			/*
2676a3d33acSPoul-Henning Kamp 			 * If the input is not EBCDIC, and we do parity
2686a3d33acSPoul-Henning Kamp 			 * processing, strip input parity.
2694b88c807SRodney W. Grimes 			 */
2706a3d33acSPoul-Henning Kamp 			for (cnt = 200; cnt <= 0377; ++cnt)
2716a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt & 0x7f];
2726a3d33acSPoul-Henning Kamp 		}
2734b88c807SRodney W. Grimes 		if (ddflags & C_LCASE) {
274c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2756a3d33acSPoul-Henning Kamp 				casetab[cnt] = tolower(casetab[cnt]);
2766a3d33acSPoul-Henning Kamp 		} else if (ddflags & C_UCASE) {
277c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2786a3d33acSPoul-Henning Kamp 				casetab[cnt] = toupper(casetab[cnt]);
2794b88c807SRodney W. Grimes 		}
2806a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARITY)) {
2816a3d33acSPoul-Henning Kamp 			/*
2826a3d33acSPoul-Henning Kamp 			 * This should strictly speaking be a no-op, but I
2836a3d33acSPoul-Henning Kamp 			 * wonder what funny LANG settings could get us.
2846a3d33acSPoul-Henning Kamp 			 */
285c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2866a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt] & 0x7f;
2876a3d33acSPoul-Henning Kamp 		}
2886a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARSET)) {
289c5191e58SAndrey A. Chernov 			for (cnt = 0; cnt <= 0377; ++cnt)
2906a3d33acSPoul-Henning Kamp 				casetab[cnt] = casetab[cnt] | 0x80;
291dde07463SAndrey A. Chernov 		}
2926a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PAREVEN)) {
2936a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2946a3d33acSPoul-Henning Kamp 				if (parity(casetab[cnt]))
2956a3d33acSPoul-Henning Kamp 					casetab[cnt] = casetab[cnt] | 0x80;
296dde07463SAndrey A. Chernov 		}
2976a3d33acSPoul-Henning Kamp 		if ((ddflags & C_PARODD)) {
2986a3d33acSPoul-Henning Kamp 			for (cnt = 0; cnt <= 0377; ++cnt)
2996a3d33acSPoul-Henning Kamp 				if (!parity(casetab[cnt]))
3006a3d33acSPoul-Henning Kamp 					casetab[cnt] = casetab[cnt] | 0x80;
3016a3d33acSPoul-Henning Kamp 		}
3026a3d33acSPoul-Henning Kamp 
30358687472SBrian Feldman 		ctab = casetab;
304426e9c1dSWarner Losh 	}
30558687472SBrian Feldman 
306540c7825SAlan Somers 	if (clock_gettime(CLOCK_MONOTONIC, &st.start))
307540c7825SAlan Somers 		err(1, "clock_gettime");
3084b88c807SRodney W. Grimes }
3094b88c807SRodney W. Grimes 
3104b88c807SRodney W. Grimes static void
getfdtype(IO * io)311f9bcb0beSWarner Losh getfdtype(IO *io)
3124b88c807SRodney W. Grimes {
3134b88c807SRodney W. Grimes 	struct stat sb;
314e08d7384SBrian Feldman 	int type;
3154b88c807SRodney W. Grimes 
31658687472SBrian Feldman 	if (fstat(io->fd, &sb) == -1)
3174b88c807SRodney W. Grimes 		err(1, "%s", io->name);
318c15c898eSBrian Feldman 	if (S_ISREG(sb.st_mode))
319c15c898eSBrian Feldman 		io->flags |= ISTRUNC;
320e08d7384SBrian Feldman 	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
321ff8bb989SBrian Feldman 		if (ioctl(io->fd, FIODTYPE, &type) == -1) {
322bf3367d0SBrian Feldman 			err(1, "%s", io->name);
323ff8bb989SBrian Feldman 		} else {
324e08d7384SBrian Feldman 			if (type & D_TAPE)
325e08d7384SBrian Feldman 				io->flags |= ISTAPE;
326cd967e32SPoul-Henning Kamp 			else if (type & (D_DISK | D_MEM))
32732952d4bSBrian Feldman 				io->flags |= ISSEEK;
328e08d7384SBrian Feldman 			if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
329e08d7384SBrian Feldman 				io->flags |= ISCHR;
330ff8bb989SBrian Feldman 		}
3315ff6541eSBrian Feldman 		return;
3325ff6541eSBrian Feldman 	}
3335ff6541eSBrian Feldman 	errno = 0;
3345ff6541eSBrian Feldman 	if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
335e08d7384SBrian Feldman 		io->flags |= ISPIPE;
3365ff6541eSBrian Feldman 	else
3375ff6541eSBrian Feldman 		io->flags |= ISSEEK;
3384b88c807SRodney W. Grimes }
3394b88c807SRodney W. Grimes 
34075e55112SEdward Tomasz Napierala /*
34175e55112SEdward Tomasz Napierala  * Limit the speed by adding a delay before every block read.
34275e55112SEdward Tomasz Napierala  * The delay (t_usleep) is equal to the time computed from block
34375e55112SEdward Tomasz Napierala  * size and the specified speed limit (t_target) minus the time
34475e55112SEdward Tomasz Napierala  * spent on actual read and write operations (t_io).
34575e55112SEdward Tomasz Napierala  */
34675e55112SEdward Tomasz Napierala static void
speed_limit(void)34775e55112SEdward Tomasz Napierala speed_limit(void)
34875e55112SEdward Tomasz Napierala {
34975e55112SEdward Tomasz Napierala 	static double t_prev, t_usleep;
35075e55112SEdward Tomasz Napierala 	double t_now, t_io, t_target;
35175e55112SEdward Tomasz Napierala 
35275e55112SEdward Tomasz Napierala 	t_now = secs_elapsed();
35375e55112SEdward Tomasz Napierala 	t_io = t_now - t_prev - t_usleep;
35475e55112SEdward Tomasz Napierala 	t_target = (double)in.dbsz / (double)speed;
35575e55112SEdward Tomasz Napierala 	t_usleep = t_target - t_io;
35675e55112SEdward Tomasz Napierala 	if (t_usleep > 0)
35775e55112SEdward Tomasz Napierala 		usleep(t_usleep * 1000000);
35875e55112SEdward Tomasz Napierala 	else
35975e55112SEdward Tomasz Napierala 		t_usleep = 0;
36075e55112SEdward Tomasz Napierala 	t_prev = t_now;
36175e55112SEdward Tomasz Napierala }
36275e55112SEdward Tomasz Napierala 
3634b88c807SRodney W. Grimes static void
swapbytes(void * v,size_t len)36444e0a832SEitan Adler swapbytes(void *v, size_t len)
36544e0a832SEitan Adler {
36644e0a832SEitan Adler 	unsigned char *p = v;
36744e0a832SEitan Adler 	unsigned char t;
36844e0a832SEitan Adler 
36944e0a832SEitan Adler 	while (len > 1) {
37044e0a832SEitan Adler 		t = p[0];
37144e0a832SEitan Adler 		p[0] = p[1];
37244e0a832SEitan Adler 		p[1] = t;
37344e0a832SEitan Adler 		p += 2;
37444e0a832SEitan Adler 		len -= 2;
37544e0a832SEitan Adler 	}
37644e0a832SEitan Adler }
37744e0a832SEitan Adler 
37844e0a832SEitan Adler static void
dd_in(void)379f9bcb0beSWarner Losh dd_in(void)
3804b88c807SRodney W. Grimes {
381767bc8adSBrian Feldman 	ssize_t n;
3824b88c807SRodney W. Grimes 
383af041bf3SJonathan Lemon 	for (;;) {
3845ff6541eSBrian Feldman 		switch (cpy_cnt) {
3855ff6541eSBrian Feldman 		case -1:			/* count=0 was specified */
3864b88c807SRodney W. Grimes 			return;
3875ff6541eSBrian Feldman 		case 0:
3885ff6541eSBrian Feldman 			break;
3895ff6541eSBrian Feldman 		default:
3907503d74fSMark Murray 			if (st.in_full + st.in_part >= (uintmax_t)cpy_cnt)
3915ff6541eSBrian Feldman 				return;
3925ff6541eSBrian Feldman 			break;
3935ff6541eSBrian Feldman 		}
3944b88c807SRodney W. Grimes 
39575e55112SEdward Tomasz Napierala 		if (speed > 0)
39675e55112SEdward Tomasz Napierala 			speed_limit();
39775e55112SEdward Tomasz Napierala 
3984b88c807SRodney W. Grimes 		/*
39958687472SBrian Feldman 		 * Zero the buffer first if sync; if doing block operations,
4004b88c807SRodney W. Grimes 		 * use spaces.
4014b88c807SRodney W. Grimes 		 */
402767bc8adSBrian Feldman 		if (ddflags & C_SYNC) {
403e3edab4aSRobert Watson 			if (ddflags & C_FILL)
404e3edab4aSRobert Watson 				memset(in.dbp, fill_char, in.dbsz);
405e3edab4aSRobert Watson 			else if (ddflags & (C_BLOCK | C_UNBLOCK))
4064b88c807SRodney W. Grimes 				memset(in.dbp, ' ', in.dbsz);
4074b88c807SRodney W. Grimes 			else
4084b88c807SRodney W. Grimes 				memset(in.dbp, 0, in.dbsz);
409767bc8adSBrian Feldman 		}
4104b88c807SRodney W. Grimes 
4114b88c807SRodney W. Grimes 		in.dbrcnt = 0;
412b52c534bSMatt Macy fill:
413*8dad5eceSKonstantin Belousov 		before_io();
414b52c534bSMatt Macy 		n = read(in.fd, in.dbp + in.dbrcnt, in.dbsz - in.dbrcnt);
415*8dad5eceSKonstantin Belousov 		after_io();
4164b88c807SRodney W. Grimes 
417b52c534bSMatt Macy 		/* EOF */
418b52c534bSMatt Macy 		if (n == 0 && in.dbrcnt == 0)
419b52c534bSMatt Macy 			return;
420b52c534bSMatt Macy 
421b52c534bSMatt Macy 		/* Read error */
422767bc8adSBrian Feldman 		if (n == -1) {
4234b88c807SRodney W. Grimes 			/*
4244b88c807SRodney W. Grimes 			 * If noerror not specified, die.  POSIX requires that
4254b88c807SRodney W. Grimes 			 * the warning message be followed by an I/O display.
4264b88c807SRodney W. Grimes 			 */
427af041bf3SJonathan Lemon 			if (!(ddflags & C_NOERROR))
4284b88c807SRodney W. Grimes 				err(1, "%s", in.name);
4294b88c807SRodney W. Grimes 			warn("%s", in.name);
4304b88c807SRodney W. Grimes 			summary();
4314b88c807SRodney W. Grimes 
4324b88c807SRodney W. Grimes 			/*
4337599187eSBrian Feldman 			 * If it's a seekable file descriptor, seek past the
4344b88c807SRodney W. Grimes 			 * error.  If your OS doesn't do the right thing for
4354b88c807SRodney W. Grimes 			 * raw disks this section should be modified to re-read
4364b88c807SRodney W. Grimes 			 * in sector size chunks.
4374b88c807SRodney W. Grimes 			 */
4387599187eSBrian Feldman 			if (in.flags & ISSEEK &&
4394b88c807SRodney W. Grimes 			    lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
4404b88c807SRodney W. Grimes 				warn("%s", in.name);
4414b88c807SRodney W. Grimes 
4424b88c807SRodney W. Grimes 			/* If sync not specified, omit block and continue. */
4434b88c807SRodney W. Grimes 			if (!(ddflags & C_SYNC))
4444b88c807SRodney W. Grimes 				continue;
4454b88c807SRodney W. Grimes 		}
4464b88c807SRodney W. Grimes 
447b52c534bSMatt Macy 		/* If conv=sync, use the entire block. */
448b52c534bSMatt Macy 		if (ddflags & C_SYNC)
449b52c534bSMatt Macy 			n = in.dbsz;
450b52c534bSMatt Macy 
451b52c534bSMatt Macy 		/* Count the bytes read for this block. */
452b52c534bSMatt Macy 		in.dbrcnt += n;
453b52c534bSMatt Macy 
454b52c534bSMatt Macy 		/* Count the number of full and partial blocks. */
455b52c534bSMatt Macy 		if (in.dbrcnt == in.dbsz)
456b52c534bSMatt Macy 			++st.in_full;
457b52c534bSMatt Macy 		else if (ddflags & C_IFULLBLOCK && n != 0)
458b52c534bSMatt Macy 			goto fill; /* these don't count */
459b52c534bSMatt Macy 		else
460b52c534bSMatt Macy 			++st.in_part;
461b52c534bSMatt Macy 
462b52c534bSMatt Macy 		/* Count the total bytes read for this file. */
463b52c534bSMatt Macy 		in.dbcnt += in.dbrcnt;
464b52c534bSMatt Macy 
4654b88c807SRodney W. Grimes 		/*
4664b88c807SRodney W. Grimes 		 * POSIX states that if bs is set and no other conversions
4674b88c807SRodney W. Grimes 		 * than noerror, notrunc or sync are specified, the block
4684b88c807SRodney W. Grimes 		 * is output without buffering as it is read.
4694b88c807SRodney W. Grimes 		 */
4706784d416SKonstantin Belousov 		if ((ddflags & ~(C_NOERROR | C_NOTRUNC | C_SYNC)) == C_BS) {
4714b88c807SRodney W. Grimes 			out.dbcnt = in.dbcnt;
4724b88c807SRodney W. Grimes 			dd_out(1);
4734b88c807SRodney W. Grimes 			in.dbcnt = 0;
4744b88c807SRodney W. Grimes 			continue;
4754b88c807SRodney W. Grimes 		}
4764b88c807SRodney W. Grimes 
4774b88c807SRodney W. Grimes 		if (ddflags & C_SWAB) {
478a28ea077SJoerg Wunsch 			if ((n = in.dbrcnt) & 1) {
4794b88c807SRodney W. Grimes 				++st.swab;
4804b88c807SRodney W. Grimes 				--n;
4814b88c807SRodney W. Grimes 			}
48244e0a832SEitan Adler 			swapbytes(in.dbp, (size_t)n);
4834b88c807SRodney W. Grimes 		}
4844b88c807SRodney W. Grimes 
485b52c534bSMatt Macy 		/* Advance to the next block. */
4864b88c807SRodney W. Grimes 		in.dbp += in.dbrcnt;
4874b88c807SRodney W. Grimes 		(*cfunc)();
4888acbb227SKyle Evans 		if (need_summary)
4894ac11639SEitan Adler 			summary();
4908acbb227SKyle Evans 		if (need_progress)
4914767c42cSKyle Evans 			progress();
4924767c42cSKyle Evans 	}
4934b88c807SRodney W. Grimes }
4944b88c807SRodney W. Grimes 
4954b88c807SRodney W. Grimes /*
49658687472SBrian Feldman  * Clean up any remaining I/O and flush output.  If necessary, the output file
4974b88c807SRodney W. Grimes  * is truncated.
4984b88c807SRodney W. Grimes  */
4994b88c807SRodney W. Grimes static void
dd_close(void)500f9bcb0beSWarner Losh dd_close(void)
5014b88c807SRodney W. Grimes {
5024b88c807SRodney W. Grimes 	if (cfunc == def)
5034b88c807SRodney W. Grimes 		def_close();
5044b88c807SRodney W. Grimes 	else if (cfunc == block)
5054b88c807SRodney W. Grimes 		block_close();
5064b88c807SRodney W. Grimes 	else if (cfunc == unblock)
5074b88c807SRodney W. Grimes 		unblock_close();
508af041bf3SJonathan Lemon 	if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
509e3edab4aSRobert Watson 		if (ddflags & C_FILL)
510e3edab4aSRobert Watson 			memset(out.dbp, fill_char, out.dbsz - out.dbcnt);
511e3edab4aSRobert Watson 		else if (ddflags & (C_BLOCK | C_UNBLOCK))
512af041bf3SJonathan Lemon 			memset(out.dbp, ' ', out.dbsz - out.dbcnt);
513af041bf3SJonathan Lemon 		else
5144b88c807SRodney W. Grimes 			memset(out.dbp, 0, out.dbsz - out.dbcnt);
5154b88c807SRodney W. Grimes 		out.dbcnt = out.dbsz;
5164b88c807SRodney W. Grimes 	}
5174ddfeabdSJoerg Wunsch 	if (out.dbcnt || pending)
5184b88c807SRodney W. Grimes 		dd_out(1);
519cd1832feSThomas Quinot 
520cd1832feSThomas Quinot 	/*
521cd1832feSThomas Quinot 	 * If the file ends with a hole, ftruncate it to extend its size
522cd1832feSThomas Quinot 	 * up to the end of the hole (without having to write any data).
523cd1832feSThomas Quinot 	 */
524cd1832feSThomas Quinot 	if (out.seek_offset > 0 && (out.flags & ISTRUNC)) {
525cd1832feSThomas Quinot 		if (ftruncate(out.fd, out.seek_offset) == -1)
526cd1832feSThomas Quinot 			err(1, "truncating %s", out.name);
527cd1832feSThomas Quinot 	}
528ce1b19d8SMatt Macy 
529ce1b19d8SMatt Macy 	if (ddflags & C_FSYNC) {
530ce1b19d8SMatt Macy 		if (fsync(out.fd) == -1)
531ce1b19d8SMatt Macy 			err(1, "fsyncing %s", out.name);
5323b96efbdSMatt Macy 	} else if (ddflags & C_FDATASYNC) {
5333b96efbdSMatt Macy 		if (fdatasync(out.fd) == -1)
5343b96efbdSMatt Macy 			err(1, "fdatasyncing %s", out.name);
535ce1b19d8SMatt Macy 	}
5364b88c807SRodney W. Grimes }
5374b88c807SRodney W. Grimes 
5384b88c807SRodney W. Grimes void
dd_out(int force)539f9bcb0beSWarner Losh dd_out(int force)
5404b88c807SRodney W. Grimes {
5414b88c807SRodney W. Grimes 	u_char *outp;
542dead7b5eSMaxim Sobolev 	size_t cnt, n;
54358687472SBrian Feldman 	ssize_t nw;
54458687472SBrian Feldman 	static int warned;
54558687472SBrian Feldman 	int sparse;
5464b88c807SRodney W. Grimes 
5474b88c807SRodney W. Grimes 	/*
5484b88c807SRodney W. Grimes 	 * Write one or more blocks out.  The common case is writing a full
5494b88c807SRodney W. Grimes 	 * output block in a single write; increment the full block stats.
5504b88c807SRodney W. Grimes 	 * Otherwise, we're into partial block writes.  If a partial write,
5514b88c807SRodney W. Grimes 	 * and it's a character device, just warn.  If a tape device, quit.
5524b88c807SRodney W. Grimes 	 *
5534b88c807SRodney W. Grimes 	 * The partial writes represent two cases.  1: Where the input block
5544b88c807SRodney W. Grimes 	 * was less than expected so the output block was less than expected.
5554b88c807SRodney W. Grimes 	 * 2: Where the input block was the right size but we were forced to
5564b88c807SRodney W. Grimes 	 * write the block in multiple chunks.  The original versions of dd(1)
5574b88c807SRodney W. Grimes 	 * never wrote a block in more than a single write, so the latter case
5584b88c807SRodney W. Grimes 	 * never happened.
5594b88c807SRodney W. Grimes 	 *
5604b88c807SRodney W. Grimes 	 * One special case is if we're forced to do the write -- in that case
5614b88c807SRodney W. Grimes 	 * we play games with the buffer size, and it's usually a partial write.
5624b88c807SRodney W. Grimes 	 */
5634b88c807SRodney W. Grimes 	outp = out.db;
564aa7a161bSThomas Quinot 
565aa7a161bSThomas Quinot 	/*
566aa7a161bSThomas Quinot 	 * If force, first try to write all pending data, else try to write
567aa7a161bSThomas Quinot 	 * just one block. Subsequently always write data one full block at
568aa7a161bSThomas Quinot 	 * a time at most.
569aa7a161bSThomas Quinot 	 */
5704b88c807SRodney W. Grimes 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
571aa7a161bSThomas Quinot 		cnt = n;
572aa7a161bSThomas Quinot 		do {
5734ddfeabdSJoerg Wunsch 			sparse = 0;
5744ddfeabdSJoerg Wunsch 			if (ddflags & C_SPARSE) {
575dead7b5eSMaxim Sobolev 				/* Is buffer sparse? */
576dead7b5eSMaxim Sobolev 				sparse = BISZERO(outp, cnt);
5774ddfeabdSJoerg Wunsch 			}
5784ddfeabdSJoerg Wunsch 			if (sparse && !force) {
5794ddfeabdSJoerg Wunsch 				pending += cnt;
5804ddfeabdSJoerg Wunsch 				nw = cnt;
5814ddfeabdSJoerg Wunsch 			} else {
5824ddfeabdSJoerg Wunsch 				if (pending != 0) {
583cd1832feSThomas Quinot 					/*
584cd1832feSThomas Quinot 					 * Seek past hole.  Note that we need to record the
585cd1832feSThomas Quinot 					 * reached offset, because we might have no more data
586cd1832feSThomas Quinot 					 * to write, in which case we'll need to call
587cd1832feSThomas Quinot 					 * ftruncate to extend the file size.
588aa7a161bSThomas Quinot 					 */
589cd1832feSThomas Quinot 					out.seek_offset = lseek(out.fd, pending, SEEK_CUR);
590cd1832feSThomas Quinot 					if (out.seek_offset == -1)
59154946e00SBrian Feldman 						err(2, "%s: seek error creating sparse file",
59254946e00SBrian Feldman 						    out.name);
593cd1832feSThomas Quinot 					pending = 0;
5944ddfeabdSJoerg Wunsch 				}
595cd1832feSThomas Quinot 				if (cnt) {
596*8dad5eceSKonstantin Belousov 					before_io();
5974b88c807SRodney W. Grimes 					nw = write(out.fd, outp, cnt);
598*8dad5eceSKonstantin Belousov 					after_io();
599cd1832feSThomas Quinot 					out.seek_offset = 0;
600cd1832feSThomas Quinot 				} else {
6014ddfeabdSJoerg Wunsch 					return;
6024ddfeabdSJoerg Wunsch 				}
603cd1832feSThomas Quinot 			}
6044ddfeabdSJoerg Wunsch 
6054b88c807SRodney W. Grimes 			if (nw <= 0) {
6064b88c807SRodney W. Grimes 				if (nw == 0)
6074b88c807SRodney W. Grimes 					errx(1, "%s: end of device", out.name);
6084b88c807SRodney W. Grimes 				if (errno != EINTR)
6094b88c807SRodney W. Grimes 					err(1, "%s", out.name);
6104b88c807SRodney W. Grimes 				nw = 0;
6114b88c807SRodney W. Grimes 			}
612aa7a161bSThomas Quinot 
6134b88c807SRodney W. Grimes 			outp += nw;
6144b88c807SRodney W. Grimes 			st.bytes += nw;
615aa7a161bSThomas Quinot 
61677a798b3SAlan Somers 			if ((size_t)nw == n && n == (size_t)out.dbsz)
6174b88c807SRodney W. Grimes 				++st.out_full;
618aa7a161bSThomas Quinot 			else
6194b88c807SRodney W. Grimes 				++st.out_part;
620aa7a161bSThomas Quinot 
621aa7a161bSThomas Quinot 			if ((size_t) nw != cnt) {
6227599187eSBrian Feldman 				if (out.flags & ISTAPE)
6237599187eSBrian Feldman 					errx(1, "%s: short write on tape device",
6247599187eSBrian Feldman 				    	out.name);
6254b88c807SRodney W. Grimes 				if (out.flags & ISCHR && !warned) {
6264b88c807SRodney W. Grimes 					warned = 1;
6274b88c807SRodney W. Grimes 					warnx("%s: short write on character device",
6284b88c807SRodney W. Grimes 				    	out.name);
6294b88c807SRodney W. Grimes 				}
6304b88c807SRodney W. Grimes 			}
631aa7a161bSThomas Quinot 
632aa7a161bSThomas Quinot 			cnt -= nw;
633aa7a161bSThomas Quinot 		} while (cnt != 0);
634aa7a161bSThomas Quinot 
6354b88c807SRodney W. Grimes 		if ((out.dbcnt -= n) < out.dbsz)
6364b88c807SRodney W. Grimes 			break;
6374b88c807SRodney W. Grimes 	}
6384b88c807SRodney W. Grimes 
6394b88c807SRodney W. Grimes 	/* Reassemble the output block. */
6404b88c807SRodney W. Grimes 	if (out.dbcnt)
64158687472SBrian Feldman 		(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
6424b88c807SRodney W. Grimes 	out.dbp = out.db + out.dbcnt;
6434b88c807SRodney W. Grimes }
644