xref: /freebsd/bin/dd/dd.c (revision c15c898eff1b25d67ee37850fbc38c0370276536)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego and Lance
74b88c807SRodney W. Grimes  * Visser of Convex Computer Corporation.
84b88c807SRodney W. Grimes  *
94b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
104b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
114b88c807SRodney W. Grimes  * are met:
124b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
144b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
164b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
174b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
184b88c807SRodney W. Grimes  *    must display the following acknowledgement:
194b88c807SRodney W. Grimes  *	This product includes software developed by the University of
204b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
214b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
224b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
234b88c807SRodney W. Grimes  *    without specific prior written permission.
244b88c807SRodney W. Grimes  *
254b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
264b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
274b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
294b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
304b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
314b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
324b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
334b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
344b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
354b88c807SRodney W. Grimes  * SUCH DAMAGE.
364b88c807SRodney W. Grimes  */
374b88c807SRodney W. Grimes 
384b88c807SRodney W. Grimes #ifndef lint
3978b09ffeSSteve Price static char const copyright[] =
404b88c807SRodney W. Grimes "@(#) Copyright (c) 1991, 1993, 1994\n\
414b88c807SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
424b88c807SRodney W. Grimes #endif /* not lint */
434b88c807SRodney W. Grimes 
444b88c807SRodney W. Grimes #ifndef lint
45cbf6f7d3SPhilippe Charnier #if 0
461ba0e048SPhilippe Charnier static char sccsid[] = "@(#)dd.c	8.5 (Berkeley) 4/2/94";
47cbf6f7d3SPhilippe Charnier #endif
48cbf6f7d3SPhilippe Charnier static const char rcsid[] =
492a456239SPeter Wemm   "$FreeBSD$";
504b88c807SRodney W. Grimes #endif /* not lint */
514b88c807SRodney W. Grimes 
524b88c807SRodney W. Grimes #include <sys/param.h>
534b88c807SRodney W. Grimes #include <sys/stat.h>
54e08d7384SBrian Feldman #include <sys/conf.h>
55015a53cfSDavid E. O'Brien #include <sys/disklabel.h>
56e08d7384SBrian Feldman #include <sys/filio.h>
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes #include <ctype.h>
594b88c807SRodney W. Grimes #include <err.h>
604b88c807SRodney W. Grimes #include <errno.h>
614b88c807SRodney W. Grimes #include <fcntl.h>
62ad66f7eeSPoul-Henning Kamp #include <locale.h>
634b88c807SRodney W. Grimes #include <stdio.h>
644b88c807SRodney W. Grimes #include <stdlib.h>
654b88c807SRodney W. Grimes #include <string.h>
664b88c807SRodney W. Grimes #include <unistd.h>
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes #include "dd.h"
694b88c807SRodney W. Grimes #include "extern.h"
704b88c807SRodney W. Grimes 
714b88c807SRodney W. Grimes static void dd_close __P((void));
724b88c807SRodney W. Grimes static void dd_in __P((void));
73c15c898eSBrian Feldman int main __P((int, char *[]));
744b88c807SRodney W. Grimes static void getfdtype __P((IO *));
754b88c807SRodney W. Grimes static void setup __P((void));
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes IO	in, out;		/* input/output state */
784b88c807SRodney W. Grimes STAT	st;			/* statistics */
794b88c807SRodney W. Grimes void	(*cfunc) __P((void));	/* conversion function */
8054946e00SBrian Feldman quad_t	cpy_cnt;		/* # of blocks to copy */
8154946e00SBrian Feldman off_t	pending = 0;		/* pending seek if sparse */
824b88c807SRodney W. Grimes u_int	ddflags;		/* conversion options */
8358687472SBrian Feldman size_t	cbsz;			/* conversion block size */
8454946e00SBrian Feldman quad_t	files_cnt = 1;		/* # of files to copy */
8558687472SBrian Feldman const	u_char *ctab;		/* conversion table */
864b88c807SRodney W. Grimes 
874b88c807SRodney W. Grimes int
884b88c807SRodney W. Grimes main(argc, argv)
894b88c807SRodney W. Grimes 	int argc;
904b88c807SRodney W. Grimes 	char *argv[];
914b88c807SRodney W. Grimes {
92f7c627b2SAndrey A. Chernov 	(void)setlocale(LC_CTYPE, "");
934b88c807SRodney W. Grimes 	jcl(argv);
944b88c807SRodney W. Grimes 	setup();
954b88c807SRodney W. Grimes 
964b88c807SRodney W. Grimes 	(void)signal(SIGINFO, summaryx);
974b88c807SRodney W. Grimes 	(void)signal(SIGINT, terminate);
984b88c807SRodney W. Grimes 
994b88c807SRodney W. Grimes 	atexit(summary);
1004b88c807SRodney W. Grimes 
1014b88c807SRodney W. Grimes 	while (files_cnt--)
1024b88c807SRodney W. Grimes 		dd_in();
1034b88c807SRodney W. Grimes 
1044b88c807SRodney W. Grimes 	dd_close();
1054b88c807SRodney W. Grimes 	exit(0);
1064b88c807SRodney W. Grimes }
1074b88c807SRodney W. Grimes 
1084b88c807SRodney W. Grimes static void
1094b88c807SRodney W. Grimes setup()
1104b88c807SRodney W. Grimes {
1114b88c807SRodney W. Grimes 	u_int cnt;
112ad66f7eeSPoul-Henning Kamp 	struct timeval tv;
1134b88c807SRodney W. Grimes 
1144b88c807SRodney W. Grimes 	if (in.name == NULL) {
1154b88c807SRodney W. Grimes 		in.name = "stdin";
1164b88c807SRodney W. Grimes 		in.fd = STDIN_FILENO;
1174b88c807SRodney W. Grimes 	} else {
11854946e00SBrian Feldman 		in.fd = open(in.name, O_RDONLY, 0);
119767bc8adSBrian Feldman 		if (in.fd == -1)
1204b88c807SRodney W. Grimes 			err(1, "%s", in.name);
1214b88c807SRodney W. Grimes 	}
1224b88c807SRodney W. Grimes 
1234b88c807SRodney W. Grimes 	getfdtype(&in);
1244b88c807SRodney W. Grimes 
1254b88c807SRodney W. Grimes 	if (files_cnt > 1 && !(in.flags & ISTAPE))
1264b88c807SRodney W. Grimes 		errx(1, "files is not supported for non-tape devices");
1274b88c807SRodney W. Grimes 
1284b88c807SRodney W. Grimes 	if (out.name == NULL) {
1294b88c807SRodney W. Grimes 		/* No way to check for read access here. */
1304b88c807SRodney W. Grimes 		out.fd = STDOUT_FILENO;
1314b88c807SRodney W. Grimes 		out.name = "stdout";
1324b88c807SRodney W. Grimes 	} else {
1334b88c807SRodney W. Grimes #define	OFLAGS \
1344b88c807SRodney W. Grimes     (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
1354b88c807SRodney W. Grimes 		out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
1364b88c807SRodney W. Grimes 		/*
1374b88c807SRodney W. Grimes 		 * May not have read access, so try again with write only.
1384b88c807SRodney W. Grimes 		 * Without read we may have a problem if output also does
1394b88c807SRodney W. Grimes 		 * not support seeks.
1404b88c807SRodney W. Grimes 		 */
141767bc8adSBrian Feldman 		if (out.fd == -1) {
1424b88c807SRodney W. Grimes 			out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
1434b88c807SRodney W. Grimes 			out.flags |= NOREAD;
1444b88c807SRodney W. Grimes 		}
145767bc8adSBrian Feldman 		if (out.fd == -1)
1464b88c807SRodney W. Grimes 			err(1, "%s", out.name);
1474b88c807SRodney W. Grimes 	}
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes 	getfdtype(&out);
1504b88c807SRodney W. Grimes 
1514b88c807SRodney W. Grimes 	/*
1524b88c807SRodney W. Grimes 	 * Allocate space for the input and output buffers.  If not doing
1534b88c807SRodney W. Grimes 	 * record oriented I/O, only need a single buffer.
1544b88c807SRodney W. Grimes 	 */
1554b88c807SRodney W. Grimes 	if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
1564b88c807SRodney W. Grimes 		if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
15758687472SBrian Feldman 			err(1, "input buffer");
1584b88c807SRodney W. Grimes 		out.db = in.db;
15958687472SBrian Feldman 	} else if ((in.db = malloc(MAX(in.dbsz, cbsz) + cbsz)) == NULL ||
16058687472SBrian Feldman 	    (out.db = malloc(out.dbsz + cbsz)) == NULL)
16158687472SBrian Feldman 		err(1, "output buffer");
1624b88c807SRodney W. Grimes 	in.dbp = in.db;
1634b88c807SRodney W. Grimes 	out.dbp = out.db;
1644b88c807SRodney W. Grimes 
1654b88c807SRodney W. Grimes 	/* Position the input/output streams. */
1664b88c807SRodney W. Grimes 	if (in.offset)
1674b88c807SRodney W. Grimes 		pos_in();
1684b88c807SRodney W. Grimes 	if (out.offset)
1694b88c807SRodney W. Grimes 		pos_out();
1704b88c807SRodney W. Grimes 
1714b88c807SRodney W. Grimes 	/*
1724b88c807SRodney W. Grimes 	 * Truncate the output file; ignore errors because it fails on some
1734b88c807SRodney W. Grimes 	 * kinds of output files, tapes, for example.
1744b88c807SRodney W. Grimes 	 */
175c15c898eSBrian Feldman 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
176c15c898eSBrian Feldman 	    out.flags & ISTRUNC)
177c15c898eSBrian Feldman 		if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
178c15c898eSBrian Feldman 			err(1, "truncating %s", out.name);
1794b88c807SRodney W. Grimes 
1804b88c807SRodney W. Grimes 	/*
1814b88c807SRodney W. Grimes 	 * If converting case at the same time as another conversion, build a
1824b88c807SRodney W. Grimes 	 * table that does both at once.  If just converting case, use the
1834b88c807SRodney W. Grimes 	 * built-in tables.
1844b88c807SRodney W. Grimes 	 */
185426e9c1dSWarner Losh 	if (ddflags & (C_LCASE | C_UCASE)) {
18658687472SBrian Feldman 		if (ddflags & (C_ASCII | C_EBCDIC)) {
1874b88c807SRodney W. Grimes 			if (ddflags & C_LCASE) {
188c5191e58SAndrey A. Chernov 				for (cnt = 0; cnt <= 0377; ++cnt)
18958687472SBrian Feldman 					casetab[cnt] = tolower(ctab[cnt]);
1904b88c807SRodney W. Grimes 			} else {
191c5191e58SAndrey A. Chernov 				for (cnt = 0; cnt <= 0377; ++cnt)
19258687472SBrian Feldman 					casetab[cnt] = toupper(ctab[cnt]);
1934b88c807SRodney W. Grimes 			}
19458687472SBrian Feldman 		} else {
1954b88c807SRodney W. Grimes 			if (ddflags & C_LCASE) {
196c5191e58SAndrey A. Chernov 				for (cnt = 0; cnt <= 0377; ++cnt)
197c15c898eSBrian Feldman 					casetab[cnt] = tolower((int)cnt);
1984b88c807SRodney W. Grimes 			} else {
199c5191e58SAndrey A. Chernov 				for (cnt = 0; cnt <= 0377; ++cnt)
200c15c898eSBrian Feldman 					casetab[cnt] = toupper((int)cnt);
201dde07463SAndrey A. Chernov 			}
202dde07463SAndrey A. Chernov 		}
20358687472SBrian Feldman 		ctab = casetab;
204426e9c1dSWarner Losh 	}
20558687472SBrian Feldman 
206ad66f7eeSPoul-Henning Kamp 	(void)gettimeofday(&tv, (struct timezone *)NULL);
207ad66f7eeSPoul-Henning Kamp 	st.start = tv.tv_sec + tv.tv_usec * 1e-6;
2084b88c807SRodney W. Grimes }
2094b88c807SRodney W. Grimes 
2104b88c807SRodney W. Grimes static void
2114b88c807SRodney W. Grimes getfdtype(io)
2124b88c807SRodney W. Grimes 	IO *io;
2134b88c807SRodney W. Grimes {
2144b88c807SRodney W. Grimes 	struct stat sb;
215e08d7384SBrian Feldman 	int type;
2164b88c807SRodney W. Grimes 
21758687472SBrian Feldman 	if (fstat(io->fd, &sb) == -1)
2184b88c807SRodney W. Grimes 		err(1, "%s", io->name);
219c15c898eSBrian Feldman 	if (S_ISREG(sb.st_mode))
220c15c898eSBrian Feldman 		io->flags |= ISTRUNC;
221e08d7384SBrian Feldman 	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
222ff8bb989SBrian Feldman 		if (ioctl(io->fd, FIODTYPE, &type) == -1) {
223bf3367d0SBrian Feldman 			err(1, "%s", io->name);
224ff8bb989SBrian Feldman 		} else {
225e08d7384SBrian Feldman 			if (type & D_TAPE)
226e08d7384SBrian Feldman 				io->flags |= ISTAPE;
227015a53cfSDavid E. O'Brien 			else if (type & (D_DISK | D_MEM)) {
228015a53cfSDavid E. O'Brien 				if (type & D_DISK) {
229015a53cfSDavid E. O'Brien 					const int one = 1;
230015a53cfSDavid E. O'Brien 
231015a53cfSDavid E. O'Brien 					(void)ioctl(io->fd, DIOCWLABEL, &one);
232015a53cfSDavid E. O'Brien 				}
23332952d4bSBrian Feldman 				io->flags |= ISSEEK;
234015a53cfSDavid E. O'Brien 			}
235e08d7384SBrian Feldman 			if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
236e08d7384SBrian Feldman 				io->flags |= ISCHR;
237ff8bb989SBrian Feldman 		}
2385ff6541eSBrian Feldman 		return;
2395ff6541eSBrian Feldman 	}
2405ff6541eSBrian Feldman 	errno = 0;
2415ff6541eSBrian Feldman 	if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
242e08d7384SBrian Feldman 		io->flags |= ISPIPE;
2435ff6541eSBrian Feldman 	else
2445ff6541eSBrian Feldman 		io->flags |= ISSEEK;
2454b88c807SRodney W. Grimes }
2464b88c807SRodney W. Grimes 
2474b88c807SRodney W. Grimes static void
2484b88c807SRodney W. Grimes dd_in()
2494b88c807SRodney W. Grimes {
250767bc8adSBrian Feldman 	ssize_t n;
2514b88c807SRodney W. Grimes 
252af041bf3SJonathan Lemon 	for (;;) {
2535ff6541eSBrian Feldman 		switch (cpy_cnt) {
2545ff6541eSBrian Feldman 		case -1:			/* count=0 was specified */
2554b88c807SRodney W. Grimes 			return;
2565ff6541eSBrian Feldman 		case 0:
2575ff6541eSBrian Feldman 			break;
2585ff6541eSBrian Feldman 		default:
259c15c898eSBrian Feldman 			if (st.in_full + st.in_part >= (u_quad_t)cpy_cnt)
2605ff6541eSBrian Feldman 				return;
2615ff6541eSBrian Feldman 			break;
2625ff6541eSBrian Feldman 		}
2634b88c807SRodney W. Grimes 
2644b88c807SRodney W. Grimes 		/*
26558687472SBrian Feldman 		 * Zero the buffer first if sync; if doing block operations,
2664b88c807SRodney W. Grimes 		 * use spaces.
2674b88c807SRodney W. Grimes 		 */
268767bc8adSBrian Feldman 		if (ddflags & C_SYNC) {
269af041bf3SJonathan Lemon 			if (ddflags & (C_BLOCK | C_UNBLOCK))
2704b88c807SRodney W. Grimes 				memset(in.dbp, ' ', in.dbsz);
2714b88c807SRodney W. Grimes 			else
2724b88c807SRodney W. Grimes 				memset(in.dbp, 0, in.dbsz);
273767bc8adSBrian Feldman 		}
2744b88c807SRodney W. Grimes 
2754b88c807SRodney W. Grimes 		n = read(in.fd, in.dbp, in.dbsz);
2764b88c807SRodney W. Grimes 		if (n == 0) {
2774b88c807SRodney W. Grimes 			in.dbrcnt = 0;
2784b88c807SRodney W. Grimes 			return;
2794b88c807SRodney W. Grimes 		}
2804b88c807SRodney W. Grimes 
2814b88c807SRodney W. Grimes 		/* Read error. */
282767bc8adSBrian Feldman 		if (n == -1) {
2834b88c807SRodney W. Grimes 			/*
2844b88c807SRodney W. Grimes 			 * If noerror not specified, die.  POSIX requires that
2854b88c807SRodney W. Grimes 			 * the warning message be followed by an I/O display.
2864b88c807SRodney W. Grimes 			 */
287af041bf3SJonathan Lemon 			if (!(ddflags & C_NOERROR))
2884b88c807SRodney W. Grimes 				err(1, "%s", in.name);
2894b88c807SRodney W. Grimes 			warn("%s", in.name);
2904b88c807SRodney W. Grimes 			summary();
2914b88c807SRodney W. Grimes 
2924b88c807SRodney W. Grimes 			/*
2937599187eSBrian Feldman 			 * If it's a seekable file descriptor, seek past the
2944b88c807SRodney W. Grimes 			 * error.  If your OS doesn't do the right thing for
2954b88c807SRodney W. Grimes 			 * raw disks this section should be modified to re-read
2964b88c807SRodney W. Grimes 			 * in sector size chunks.
2974b88c807SRodney W. Grimes 			 */
2987599187eSBrian Feldman 			if (in.flags & ISSEEK &&
2994b88c807SRodney W. Grimes 			    lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
3004b88c807SRodney W. Grimes 				warn("%s", in.name);
3014b88c807SRodney W. Grimes 
3024b88c807SRodney W. Grimes 			/* If sync not specified, omit block and continue. */
3034b88c807SRodney W. Grimes 			if (!(ddflags & C_SYNC))
3044b88c807SRodney W. Grimes 				continue;
3054b88c807SRodney W. Grimes 
3064b88c807SRodney W. Grimes 			/* Read errors count as full blocks. */
3074b88c807SRodney W. Grimes 			in.dbcnt += in.dbrcnt = in.dbsz;
3084b88c807SRodney W. Grimes 			++st.in_full;
3094b88c807SRodney W. Grimes 
3104b88c807SRodney W. Grimes 		/* Handle full input blocks. */
311c15c898eSBrian Feldman 		} else if ((size_t)n == in.dbsz) {
3124b88c807SRodney W. Grimes 			in.dbcnt += in.dbrcnt = n;
3134b88c807SRodney W. Grimes 			++st.in_full;
3144b88c807SRodney W. Grimes 
3154b88c807SRodney W. Grimes 		/* Handle partial input blocks. */
3164b88c807SRodney W. Grimes 		} else {
3174b88c807SRodney W. Grimes 			/* If sync, use the entire block. */
3184b88c807SRodney W. Grimes 			if (ddflags & C_SYNC)
3194b88c807SRodney W. Grimes 				in.dbcnt += in.dbrcnt = in.dbsz;
3204b88c807SRodney W. Grimes 			else
3214b88c807SRodney W. Grimes 				in.dbcnt += in.dbrcnt = n;
3224b88c807SRodney W. Grimes 			++st.in_part;
3234b88c807SRodney W. Grimes 		}
3244b88c807SRodney W. Grimes 
3254b88c807SRodney W. Grimes 		/*
3264b88c807SRodney W. Grimes 		 * POSIX states that if bs is set and no other conversions
3274b88c807SRodney W. Grimes 		 * than noerror, notrunc or sync are specified, the block
3284b88c807SRodney W. Grimes 		 * is output without buffering as it is read.
3294b88c807SRodney W. Grimes 		 */
3304b88c807SRodney W. Grimes 		if (ddflags & C_BS) {
3314b88c807SRodney W. Grimes 			out.dbcnt = in.dbcnt;
3324b88c807SRodney W. Grimes 			dd_out(1);
3334b88c807SRodney W. Grimes 			in.dbcnt = 0;
3344b88c807SRodney W. Grimes 			continue;
3354b88c807SRodney W. Grimes 		}
3364b88c807SRodney W. Grimes 
3374b88c807SRodney W. Grimes 		if (ddflags & C_SWAB) {
338a28ea077SJoerg Wunsch 			if ((n = in.dbrcnt) & 1) {
3394b88c807SRodney W. Grimes 				++st.swab;
3404b88c807SRodney W. Grimes 				--n;
3414b88c807SRodney W. Grimes 			}
342c15c898eSBrian Feldman 			swab(in.dbp, in.dbp, (size_t)n);
3434b88c807SRodney W. Grimes 		}
3444b88c807SRodney W. Grimes 
3454b88c807SRodney W. Grimes 		in.dbp += in.dbrcnt;
3464b88c807SRodney W. Grimes 		(*cfunc)();
3474b88c807SRodney W. Grimes 	}
3484b88c807SRodney W. Grimes }
3494b88c807SRodney W. Grimes 
3504b88c807SRodney W. Grimes /*
35158687472SBrian Feldman  * Clean up any remaining I/O and flush output.  If necessary, the output file
3524b88c807SRodney W. Grimes  * is truncated.
3534b88c807SRodney W. Grimes  */
3544b88c807SRodney W. Grimes static void
3554b88c807SRodney W. Grimes dd_close()
3564b88c807SRodney W. Grimes {
3574b88c807SRodney W. Grimes 	if (cfunc == def)
3584b88c807SRodney W. Grimes 		def_close();
3594b88c807SRodney W. Grimes 	else if (cfunc == block)
3604b88c807SRodney W. Grimes 		block_close();
3614b88c807SRodney W. Grimes 	else if (cfunc == unblock)
3624b88c807SRodney W. Grimes 		unblock_close();
363af041bf3SJonathan Lemon 	if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
364af041bf3SJonathan Lemon 		if (ddflags & (C_BLOCK | C_UNBLOCK))
365af041bf3SJonathan Lemon 			memset(out.dbp, ' ', out.dbsz - out.dbcnt);
366af041bf3SJonathan Lemon 		else
3674b88c807SRodney W. Grimes 			memset(out.dbp, 0, out.dbsz - out.dbcnt);
3684b88c807SRodney W. Grimes 		out.dbcnt = out.dbsz;
3694b88c807SRodney W. Grimes 	}
3704ddfeabdSJoerg Wunsch 	if (out.dbcnt || pending)
3714b88c807SRodney W. Grimes 		dd_out(1);
3724b88c807SRodney W. Grimes }
3734b88c807SRodney W. Grimes 
3744b88c807SRodney W. Grimes void
3754b88c807SRodney W. Grimes dd_out(force)
3764b88c807SRodney W. Grimes 	int force;
3774b88c807SRodney W. Grimes {
3784b88c807SRodney W. Grimes 	u_char *outp;
37958687472SBrian Feldman 	size_t cnt, i, n;
38058687472SBrian Feldman 	ssize_t nw;
38158687472SBrian Feldman 	static int warned;
38258687472SBrian Feldman 	int sparse;
3834b88c807SRodney W. Grimes 
3844b88c807SRodney W. Grimes 	/*
3854b88c807SRodney W. Grimes 	 * Write one or more blocks out.  The common case is writing a full
3864b88c807SRodney W. Grimes 	 * output block in a single write; increment the full block stats.
3874b88c807SRodney W. Grimes 	 * Otherwise, we're into partial block writes.  If a partial write,
3884b88c807SRodney W. Grimes 	 * and it's a character device, just warn.  If a tape device, quit.
3894b88c807SRodney W. Grimes 	 *
3904b88c807SRodney W. Grimes 	 * The partial writes represent two cases.  1: Where the input block
3914b88c807SRodney W. Grimes 	 * was less than expected so the output block was less than expected.
3924b88c807SRodney W. Grimes 	 * 2: Where the input block was the right size but we were forced to
3934b88c807SRodney W. Grimes 	 * write the block in multiple chunks.  The original versions of dd(1)
3944b88c807SRodney W. Grimes 	 * never wrote a block in more than a single write, so the latter case
3954b88c807SRodney W. Grimes 	 * never happened.
3964b88c807SRodney W. Grimes 	 *
3974b88c807SRodney W. Grimes 	 * One special case is if we're forced to do the write -- in that case
3984b88c807SRodney W. Grimes 	 * we play games with the buffer size, and it's usually a partial write.
3994b88c807SRodney W. Grimes 	 */
4004b88c807SRodney W. Grimes 	outp = out.db;
4014b88c807SRodney W. Grimes 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
4024b88c807SRodney W. Grimes 		for (cnt = n;; cnt -= nw) {
4034ddfeabdSJoerg Wunsch 			sparse = 0;
4044ddfeabdSJoerg Wunsch 			if (ddflags & C_SPARSE) {
4054ddfeabdSJoerg Wunsch 				sparse = 1;	/* Is buffer sparse? */
4064ddfeabdSJoerg Wunsch 				for (i = 0; i < cnt; i++)
4074ddfeabdSJoerg Wunsch 					if (outp[i] != 0) {
4084ddfeabdSJoerg Wunsch 						sparse = 0;
4094ddfeabdSJoerg Wunsch 						break;
4104ddfeabdSJoerg Wunsch 					}
4114ddfeabdSJoerg Wunsch 			}
4124ddfeabdSJoerg Wunsch 			if (sparse && !force) {
4134ddfeabdSJoerg Wunsch 				pending += cnt;
4144ddfeabdSJoerg Wunsch 				nw = cnt;
4154ddfeabdSJoerg Wunsch 			} else {
4164ddfeabdSJoerg Wunsch 				if (pending != 0) {
4174ddfeabdSJoerg Wunsch 					if (force)
4184ddfeabdSJoerg Wunsch 						pending--;
41954946e00SBrian Feldman 					if (lseek(out.fd, pending, SEEK_CUR) ==
42054946e00SBrian Feldman 					    -1)
42154946e00SBrian Feldman 						err(2, "%s: seek error creating sparse file",
42254946e00SBrian Feldman 						    out.name);
4234ddfeabdSJoerg Wunsch 					if (force)
4244ddfeabdSJoerg Wunsch 						write(out.fd, outp, 1);
4254ddfeabdSJoerg Wunsch 					pending = 0;
4264ddfeabdSJoerg Wunsch 				}
4274ddfeabdSJoerg Wunsch 				if (cnt)
4284b88c807SRodney W. Grimes 					nw = write(out.fd, outp, cnt);
4294ddfeabdSJoerg Wunsch 				else
4304ddfeabdSJoerg Wunsch 					return;
4314ddfeabdSJoerg Wunsch 			}
4324ddfeabdSJoerg Wunsch 
4334b88c807SRodney W. Grimes 			if (nw <= 0) {
4344b88c807SRodney W. Grimes 				if (nw == 0)
4354b88c807SRodney W. Grimes 					errx(1, "%s: end of device", out.name);
4364b88c807SRodney W. Grimes 				if (errno != EINTR)
4374b88c807SRodney W. Grimes 					err(1, "%s", out.name);
4384b88c807SRodney W. Grimes 				nw = 0;
4394b88c807SRodney W. Grimes 			}
4404b88c807SRodney W. Grimes 			outp += nw;
4414b88c807SRodney W. Grimes 			st.bytes += nw;
442c15c898eSBrian Feldman 			if ((size_t)nw == n) {
4434b88c807SRodney W. Grimes 				if (n != out.dbsz)
4444b88c807SRodney W. Grimes 					++st.out_part;
4454b88c807SRodney W. Grimes 				else
4464b88c807SRodney W. Grimes 					++st.out_full;
4474b88c807SRodney W. Grimes 				break;
4484b88c807SRodney W. Grimes 			}
4494b88c807SRodney W. Grimes 			++st.out_part;
450c15c898eSBrian Feldman 			if ((size_t)nw == cnt)
4514b88c807SRodney W. Grimes 				break;
4527599187eSBrian Feldman 			if (out.flags & ISTAPE)
4537599187eSBrian Feldman 				errx(1, "%s: short write on tape device",
4547599187eSBrian Feldman 				    out.name);
4554b88c807SRodney W. Grimes 			if (out.flags & ISCHR && !warned) {
4564b88c807SRodney W. Grimes 				warned = 1;
4574b88c807SRodney W. Grimes 				warnx("%s: short write on character device",
4584b88c807SRodney W. Grimes 				    out.name);
4594b88c807SRodney W. Grimes 			}
4604b88c807SRodney W. Grimes 		}
4614b88c807SRodney W. Grimes 		if ((out.dbcnt -= n) < out.dbsz)
4624b88c807SRodney W. Grimes 			break;
4634b88c807SRodney W. Grimes 	}
4644b88c807SRodney W. Grimes 
4654b88c807SRodney W. Grimes 	/* Reassemble the output block. */
4664b88c807SRodney W. Grimes 	if (out.dbcnt)
46758687472SBrian Feldman 		(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
4684b88c807SRodney W. Grimes 	out.dbp = out.db + out.dbcnt;
4694b88c807SRodney W. Grimes }
470