Home | History | Annotate | Line # | Download | only in dd
dd.c revision 1.37
      1  1.37       dbj /*	$NetBSD: dd.c,v 1.37 2004/01/17 21:00:16 dbj Exp $	*/
      2   1.4       cgd 
      3   1.1     glass /*-
      4   1.3   mycroft  * Copyright (c) 1991, 1993, 1994
      5   1.3   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1     glass  *
      7   1.1     glass  * This code is derived from software contributed to Berkeley by
      8   1.1     glass  * Keith Muller of the University of California, San Diego and Lance
      9   1.1     glass  * Visser of Convex Computer Corporation.
     10   1.1     glass  *
     11   1.1     glass  * Redistribution and use in source and binary forms, with or without
     12   1.1     glass  * modification, are permitted provided that the following conditions
     13   1.1     glass  * are met:
     14   1.1     glass  * 1. Redistributions of source code must retain the above copyright
     15   1.1     glass  *    notice, this list of conditions and the following disclaimer.
     16   1.1     glass  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1     glass  *    notice, this list of conditions and the following disclaimer in the
     18   1.1     glass  *    documentation and/or other materials provided with the distribution.
     19  1.31       agc  * 3. Neither the name of the University nor the names of its contributors
     20   1.1     glass  *    may be used to endorse or promote products derived from this software
     21   1.1     glass  *    without specific prior written permission.
     22   1.1     glass  *
     23   1.1     glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1     glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1     glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1     glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1     glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1     glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1     glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1     glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1     glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1     glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1     glass  * SUCH DAMAGE.
     34   1.1     glass  */
     35   1.1     glass 
     36   1.7  christos #include <sys/cdefs.h>
     37   1.1     glass #ifndef lint
     38   1.7  christos __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
     39   1.7  christos 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1     glass #endif /* not lint */
     41   1.1     glass 
     42   1.1     glass #ifndef lint
     43   1.4       cgd #if 0
     44   1.4       cgd static char sccsid[] = "@(#)dd.c	8.5 (Berkeley) 4/2/94";
     45   1.4       cgd #else
     46  1.37       dbj __RCSID("$NetBSD: dd.c,v 1.37 2004/01/17 21:00:16 dbj Exp $");
     47   1.4       cgd #endif
     48   1.1     glass #endif /* not lint */
     49   1.1     glass 
     50   1.1     glass #include <sys/param.h>
     51   1.1     glass #include <sys/stat.h>
     52   1.1     glass #include <sys/ioctl.h>
     53   1.1     glass #include <sys/mtio.h>
     54  1.20      ross #include <sys/time.h>
     55   1.1     glass 
     56   1.1     glass #include <ctype.h>
     57   1.3   mycroft #include <err.h>
     58   1.1     glass #include <errno.h>
     59   1.1     glass #include <fcntl.h>
     60   1.1     glass #include <signal.h>
     61   1.1     glass #include <stdio.h>
     62   1.1     glass #include <stdlib.h>
     63   1.1     glass #include <string.h>
     64   1.1     glass #include <unistd.h>
     65   1.1     glass 
     66   1.1     glass #include "dd.h"
     67   1.1     glass #include "extern.h"
     68   1.1     glass 
     69  1.21     lukem static void dd_close(void);
     70  1.21     lukem static void dd_in(void);
     71  1.21     lukem static void getfdtype(IO *);
     72  1.34   dsainty static int redup_clean_fd(int);
     73  1.21     lukem static void setup(void);
     74   1.1     glass 
     75  1.21     lukem int main(int, char *[]);
     76   1.7  christos 
     77  1.23     lukem IO		in, out;		/* input/output state */
     78  1.23     lukem STAT		st;			/* statistics */
     79  1.23     lukem void		(*cfunc)(void);		/* conversion function */
     80  1.24     lukem uint64_t	cpy_cnt;		/* # of blocks to copy */
     81  1.36       dbj static off_t	pending = 0;		/* pending seek if sparse */
     82  1.23     lukem u_int		ddflags;		/* conversion options */
     83  1.24     lukem uint64_t	cbsz;			/* conversion block size */
     84  1.23     lukem u_int		files_cnt = 1;		/* # of files to copy */
     85  1.23     lukem int		progress = 0;		/* display sign of life */
     86  1.23     lukem const u_char	*ctab;			/* conversion table */
     87  1.23     lukem sigset_t	infoset;		/* a set blocking SIGINFO */
     88   1.1     glass 
     89   1.1     glass int
     90  1.21     lukem main(int argc, char *argv[])
     91   1.1     glass {
     92  1.18    kleink 	int ch;
     93  1.18    kleink 
     94  1.18    kleink 	while ((ch = getopt(argc, argv, "")) != -1) {
     95  1.18    kleink 		switch (ch) {
     96  1.19    kleink 		default:
     97  1.19    kleink 			errx(EXIT_FAILURE, "usage: dd [operand ...]");
     98  1.19    kleink 			/* NOTREACHED */
     99  1.18    kleink 		}
    100  1.18    kleink 	}
    101  1.18    kleink 	argc -= (optind - 1);
    102  1.18    kleink 	argv += (optind - 1);
    103  1.18    kleink 
    104   1.1     glass 	jcl(argv);
    105   1.1     glass 	setup();
    106   1.1     glass 
    107   1.3   mycroft 	(void)signal(SIGINFO, summaryx);
    108   1.1     glass 	(void)signal(SIGINT, terminate);
    109  1.16  christos 	(void)sigemptyset(&infoset);
    110  1.16  christos 	(void)sigaddset(&infoset, SIGINFO);
    111   1.1     glass 
    112  1.10   mycroft 	(void)atexit(summary);
    113   1.3   mycroft 
    114   1.3   mycroft 	while (files_cnt--)
    115   1.1     glass 		dd_in();
    116   1.1     glass 
    117   1.1     glass 	dd_close();
    118   1.1     glass 	exit(0);
    119  1.11   mycroft 	/* NOTREACHED */
    120   1.1     glass }
    121   1.1     glass 
    122   1.1     glass static void
    123  1.21     lukem setup(void)
    124   1.1     glass {
    125  1.25     enami 
    126   1.1     glass 	if (in.name == NULL) {
    127   1.1     glass 		in.name = "stdin";
    128   1.1     glass 		in.fd = STDIN_FILENO;
    129   1.1     glass 	} else {
    130   1.1     glass 		in.fd = open(in.name, O_RDONLY, 0);
    131   1.1     glass 		if (in.fd < 0)
    132  1.33  jschauma 			err(EXIT_FAILURE, "%s", in.name);
    133  1.30  jschauma 			/* NOTREACHED */
    134  1.34   dsainty 
    135  1.34   dsainty 		/* Ensure in.fd is outside the stdio descriptor range */
    136  1.34   dsainty 		in.fd = redup_clean_fd(in.fd);
    137   1.1     glass 	}
    138   1.1     glass 
    139   1.3   mycroft 	getfdtype(&in);
    140   1.1     glass 
    141  1.30  jschauma 	if (files_cnt > 1 && !(in.flags & ISTAPE)) {
    142  1.30  jschauma 		errx(EXIT_FAILURE, "files is not supported for non-tape devices");
    143  1.30  jschauma 		/* NOTREACHED */
    144  1.30  jschauma 	}
    145   1.1     glass 
    146   1.1     glass 	if (out.name == NULL) {
    147   1.1     glass 		/* No way to check for read access here. */
    148   1.1     glass 		out.fd = STDOUT_FILENO;
    149   1.1     glass 		out.name = "stdout";
    150   1.1     glass 	} else {
    151   1.1     glass #define	OFLAGS \
    152   1.1     glass     (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
    153   1.1     glass 		out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
    154   1.1     glass 		/*
    155   1.1     glass 		 * May not have read access, so try again with write only.
    156   1.1     glass 		 * Without read we may have a problem if output also does
    157   1.1     glass 		 * not support seeks.
    158   1.1     glass 		 */
    159   1.1     glass 		if (out.fd < 0) {
    160   1.1     glass 			out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
    161   1.1     glass 			out.flags |= NOREAD;
    162   1.1     glass 		}
    163  1.30  jschauma 		if (out.fd < 0) {
    164  1.33  jschauma 			err(EXIT_FAILURE, "%s", out.name);
    165  1.30  jschauma 			/* NOTREACHED */
    166  1.30  jschauma 		}
    167  1.34   dsainty 
    168  1.34   dsainty 		/* Ensure out.fd is outside the stdio descriptor range */
    169  1.34   dsainty 		out.fd = redup_clean_fd(out.fd);
    170   1.1     glass 	}
    171   1.1     glass 
    172   1.3   mycroft 	getfdtype(&out);
    173   1.1     glass 
    174   1.1     glass 	/*
    175   1.1     glass 	 * Allocate space for the input and output buffers.  If not doing
    176   1.1     glass 	 * record oriented I/O, only need a single buffer.
    177   1.1     glass 	 */
    178   1.1     glass 	if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
    179  1.30  jschauma 		if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) {
    180  1.30  jschauma 			err(EXIT_FAILURE, NULL);
    181  1.30  jschauma 			/* NOTREACHED */
    182  1.30  jschauma 		}
    183   1.1     glass 		out.db = in.db;
    184   1.1     glass 	} else if ((in.db =
    185   1.1     glass 	    malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
    186  1.30  jschauma 	    (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
    187  1.30  jschauma 		err(EXIT_FAILURE, NULL);
    188  1.30  jschauma 		/* NOTREACHED */
    189  1.30  jschauma 	}
    190   1.1     glass 	in.dbp = in.db;
    191   1.1     glass 	out.dbp = out.db;
    192   1.1     glass 
    193   1.1     glass 	/* Position the input/output streams. */
    194   1.1     glass 	if (in.offset)
    195   1.1     glass 		pos_in();
    196   1.1     glass 	if (out.offset)
    197   1.1     glass 		pos_out();
    198   1.1     glass 
    199   1.1     glass 	/*
    200   1.1     glass 	 * Truncate the output file; ignore errors because it fails on some
    201   1.1     glass 	 * kinds of output files, tapes, for example.
    202   1.1     glass 	 */
    203   1.7  christos 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
    204   1.1     glass 		(void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
    205   1.1     glass 
    206   1.1     glass 	/*
    207   1.1     glass 	 * If converting case at the same time as another conversion, build a
    208   1.1     glass 	 * table that does both at once.  If just converting case, use the
    209   1.1     glass 	 * built-in tables.
    210   1.1     glass 	 */
    211   1.5       gwr 	if (ddflags & (C_LCASE|C_UCASE)) {
    212   1.5       gwr #ifdef	NO_CONV
    213   1.5       gwr 		/* Should not get here, but just in case... */
    214  1.30  jschauma 		errx(EXIT_FAILURE, "case conv and -DNO_CONV");
    215  1.30  jschauma 		/* NOTREACHED */
    216   1.5       gwr #else	/* NO_CONV */
    217  1.22     lukem 		u_int cnt;
    218  1.22     lukem 
    219   1.6       jtc 		if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
    220   1.1     glass 			if (ddflags & C_LCASE) {
    221   1.1     glass 				for (cnt = 0; cnt < 0377; ++cnt)
    222   1.6       jtc 					casetab[cnt] = tolower(ctab[cnt]);
    223   1.1     glass 			} else {
    224   1.1     glass 				for (cnt = 0; cnt < 0377; ++cnt)
    225   1.6       jtc 					casetab[cnt] = toupper(ctab[cnt]);
    226   1.1     glass 			}
    227   1.6       jtc 		} else {
    228   1.1     glass 			if (ddflags & C_LCASE) {
    229   1.1     glass 				for (cnt = 0; cnt < 0377; ++cnt)
    230   1.6       jtc 					casetab[cnt] = tolower(cnt);
    231   1.1     glass 			} else {
    232   1.1     glass 				for (cnt = 0; cnt < 0377; ++cnt)
    233   1.6       jtc 					casetab[cnt] = toupper(cnt);
    234   1.1     glass 			}
    235   1.6       jtc 		}
    236   1.6       jtc 
    237   1.6       jtc 		ctab = casetab;
    238   1.5       gwr #endif	/* NO_CONV */
    239   1.5       gwr 	}
    240   1.5       gwr 
    241  1.20      ross 	(void)gettimeofday(&st.start, NULL);	/* Statistics timestamp. */
    242   1.1     glass }
    243   1.1     glass 
    244   1.1     glass static void
    245  1.21     lukem getfdtype(IO *io)
    246   1.3   mycroft {
    247   1.3   mycroft 	struct mtget mt;
    248   1.3   mycroft 	struct stat sb;
    249   1.3   mycroft 
    250  1.30  jschauma 	if (fstat(io->fd, &sb)) {
    251  1.33  jschauma 		err(EXIT_FAILURE, "%s", io->name);
    252  1.30  jschauma 		/* NOTREACHED */
    253  1.30  jschauma 	}
    254   1.3   mycroft 	if (S_ISCHR(sb.st_mode))
    255   1.3   mycroft 		io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
    256   1.3   mycroft 	else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
    257   1.3   mycroft 		io->flags |= ISPIPE;		/* XXX fixed in 4.4BSD */
    258  1.34   dsainty }
    259  1.34   dsainty 
    260  1.34   dsainty /*
    261  1.34   dsainty  * Move the parameter file descriptor to a descriptor that is outside the
    262  1.34   dsainty  * stdio descriptor range, if necessary.  This is required to avoid
    263  1.34   dsainty  * accidentally outputting completion or error messages into the
    264  1.34   dsainty  * output file that were intended for the tty.
    265  1.34   dsainty  */
    266  1.34   dsainty static int
    267  1.34   dsainty redup_clean_fd(int fd)
    268  1.34   dsainty {
    269  1.34   dsainty 	int newfd;
    270  1.34   dsainty 
    271  1.34   dsainty 	if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
    272  1.34   dsainty 	    fd != STDERR_FILENO)
    273  1.34   dsainty 		/* File descriptor is ok, return immediately. */
    274  1.34   dsainty 		return fd;
    275  1.34   dsainty 
    276  1.35   dsainty 	/*
    277  1.35   dsainty 	 * 3 is the first descriptor greater than STD*_FILENO.  Any
    278  1.35   dsainty 	 * free descriptor valued 3 or above is acceptable...
    279  1.35   dsainty 	 */
    280  1.35   dsainty 	newfd = fcntl(fd, F_DUPFD, 3);
    281  1.34   dsainty 	if (newfd < 0) {
    282  1.35   dsainty 		err(EXIT_FAILURE, "dupfd IO");
    283  1.34   dsainty 		/* NOTREACHED */
    284  1.34   dsainty 	}
    285  1.34   dsainty 
    286  1.34   dsainty 	close(fd);
    287  1.34   dsainty 
    288  1.34   dsainty 	return newfd;
    289   1.3   mycroft }
    290   1.3   mycroft 
    291   1.3   mycroft static void
    292  1.21     lukem dd_in(void)
    293   1.1     glass {
    294  1.23     lukem 	int flags;
    295  1.26     lukem 	int64_t n;
    296   1.1     glass 
    297   1.1     glass 	for (flags = ddflags;;) {
    298   1.1     glass 		if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
    299   1.1     glass 			return;
    300   1.1     glass 
    301   1.1     glass 		/*
    302   1.9       gwr 		 * Clear the buffer first if doing "sync" on input.
    303   1.9       gwr 		 * If doing block operations use spaces.  This will
    304   1.9       gwr 		 * affect not only the C_NOERROR case, but also the
    305   1.9       gwr 		 * last partial input block which should be padded
    306   1.9       gwr 		 * with zero and not garbage.
    307   1.1     glass 		 */
    308  1.12   thorpej 		if (flags & C_SYNC) {
    309   1.1     glass 			if (flags & (C_BLOCK|C_UNBLOCK))
    310  1.10   mycroft 				(void)memset(in.dbp, ' ', in.dbsz);
    311   1.1     glass 			else
    312  1.10   mycroft 				(void)memset(in.dbp, 0, in.dbsz);
    313  1.12   thorpej 		}
    314   1.1     glass 
    315   1.1     glass 		n = read(in.fd, in.dbp, in.dbsz);
    316   1.1     glass 		if (n == 0) {
    317   1.1     glass 			in.dbrcnt = 0;
    318   1.1     glass 			return;
    319   1.1     glass 		}
    320   1.1     glass 
    321   1.1     glass 		/* Read error. */
    322   1.1     glass 		if (n < 0) {
    323  1.30  jschauma 
    324   1.1     glass 			/*
    325   1.1     glass 			 * If noerror not specified, die.  POSIX requires that
    326   1.1     glass 			 * the warning message be followed by an I/O display.
    327   1.1     glass 			 */
    328  1.30  jschauma 			if (!(flags & C_NOERROR)) {
    329  1.33  jschauma 				err(EXIT_FAILURE, "%s", in.name);
    330  1.30  jschauma 				/* NOTREACHED */
    331  1.30  jschauma 			}
    332  1.33  jschauma 			warn("%s", in.name);
    333   1.3   mycroft 			summary();
    334   1.1     glass 
    335   1.1     glass 			/*
    336   1.1     glass 			 * If it's not a tape drive or a pipe, seek past the
    337   1.1     glass 			 * error.  If your OS doesn't do the right thing for
    338   1.1     glass 			 * raw disks this section should be modified to re-read
    339   1.1     glass 			 * in sector size chunks.
    340   1.1     glass 			 */
    341   1.1     glass 			if (!(in.flags & (ISPIPE|ISTAPE)) &&
    342   1.1     glass 			    lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
    343  1.33  jschauma 				warn("%s", in.name);
    344  1.32  jschauma 
    345   1.1     glass 			/* If sync not specified, omit block and continue. */
    346   1.1     glass 			if (!(ddflags & C_SYNC))
    347   1.1     glass 				continue;
    348   1.1     glass 
    349   1.1     glass 			/* Read errors count as full blocks. */
    350   1.1     glass 			in.dbcnt += in.dbrcnt = in.dbsz;
    351   1.1     glass 			++st.in_full;
    352   1.1     glass 
    353   1.1     glass 		/* Handle full input blocks. */
    354   1.1     glass 		} else if (n == in.dbsz) {
    355   1.1     glass 			in.dbcnt += in.dbrcnt = n;
    356   1.1     glass 			++st.in_full;
    357   1.1     glass 
    358   1.1     glass 		/* Handle partial input blocks. */
    359   1.1     glass 		} else {
    360   1.1     glass 			/* If sync, use the entire block. */
    361   1.1     glass 			if (ddflags & C_SYNC)
    362   1.1     glass 				in.dbcnt += in.dbrcnt = in.dbsz;
    363   1.1     glass 			else
    364   1.1     glass 				in.dbcnt += in.dbrcnt = n;
    365   1.1     glass 			++st.in_part;
    366   1.1     glass 		}
    367   1.1     glass 
    368   1.1     glass 		/*
    369   1.1     glass 		 * POSIX states that if bs is set and no other conversions
    370   1.1     glass 		 * than noerror, notrunc or sync are specified, the block
    371   1.1     glass 		 * is output without buffering as it is read.
    372   1.1     glass 		 */
    373   1.1     glass 		if (ddflags & C_BS) {
    374   1.1     glass 			out.dbcnt = in.dbcnt;
    375   1.1     glass 			dd_out(1);
    376   1.1     glass 			in.dbcnt = 0;
    377   1.1     glass 			continue;
    378   1.1     glass 		}
    379   1.1     glass 
    380   1.1     glass 		if (ddflags & C_SWAB) {
    381  1.17      matt 			if ((n = in.dbrcnt) & 1) {
    382   1.1     glass 				++st.swab;
    383   1.1     glass 				--n;
    384   1.1     glass 			}
    385   1.1     glass 			swab(in.dbp, in.dbp, n);
    386   1.1     glass 		}
    387   1.1     glass 
    388   1.1     glass 		in.dbp += in.dbrcnt;
    389   1.1     glass 		(*cfunc)();
    390   1.1     glass 	}
    391   1.1     glass }
    392   1.1     glass 
    393   1.1     glass /*
    394   1.1     glass  * Cleanup any remaining I/O and flush output.  If necesssary, output file
    395   1.1     glass  * is truncated.
    396   1.1     glass  */
    397   1.1     glass static void
    398  1.21     lukem dd_close(void)
    399   1.1     glass {
    400  1.21     lukem 
    401   1.1     glass 	if (cfunc == def)
    402   1.1     glass 		def_close();
    403   1.1     glass 	else if (cfunc == block)
    404   1.1     glass 		block_close();
    405   1.1     glass 	else if (cfunc == unblock)
    406   1.1     glass 		unblock_close();
    407   1.3   mycroft 	if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
    408  1.10   mycroft 		(void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
    409   1.3   mycroft 		out.dbcnt = out.dbsz;
    410   1.3   mycroft 	}
    411  1.36       dbj 	/* If there are pending sparse blocks, make sure
    412  1.36       dbj 	 * to write out the final block un-sparse
    413  1.36       dbj 	 */
    414  1.36       dbj 	if ((out.dbcnt == 0) && pending) {
    415  1.36       dbj 		memset(out.db, 0, out.dbsz);
    416  1.36       dbj 		out.dbcnt = out.dbsz;
    417  1.37       dbj 		out.dbp = out.db + out.dbcnt;
    418  1.36       dbj 		pending -= out.dbsz;
    419  1.36       dbj 	}
    420   1.1     glass 	if (out.dbcnt)
    421   1.1     glass 		dd_out(1);
    422  1.29     enami 
    423  1.29     enami 	/*
    424  1.29     enami 	 * Reporting nfs write error may be defered until next
    425  1.29     enami 	 * write(2) or close(2) system call.  So, we need to do an
    426  1.29     enami 	 * extra check.  If an output is stdout, the file structure
    427  1.29     enami 	 * may be shared among with other processes and close(2) just
    428  1.29     enami 	 * decreases the reference count.
    429  1.29     enami 	 */
    430  1.30  jschauma 	if (out.fd == STDOUT_FILENO && fsync(out.fd) == -1 && errno != EINVAL) {
    431  1.30  jschauma 		err(EXIT_FAILURE, "fsync stdout");
    432  1.30  jschauma 		/* NOTREACHED */
    433  1.30  jschauma 	}
    434  1.30  jschauma 	if (close(out.fd) == -1) {
    435  1.30  jschauma 		err(EXIT_FAILURE, "close");
    436  1.30  jschauma 		/* NOTREACHED */
    437  1.30  jschauma 	}
    438   1.1     glass }
    439   1.1     glass 
    440   1.1     glass void
    441  1.21     lukem dd_out(int force)
    442   1.1     glass {
    443   1.1     glass 	static int warned;
    444  1.26     lukem 	int64_t cnt, n, nw;
    445   1.3   mycroft 	u_char *outp;
    446   1.1     glass 
    447   1.1     glass 	/*
    448   1.1     glass 	 * Write one or more blocks out.  The common case is writing a full
    449   1.1     glass 	 * output block in a single write; increment the full block stats.
    450   1.1     glass 	 * Otherwise, we're into partial block writes.  If a partial write,
    451   1.1     glass 	 * and it's a character device, just warn.  If a tape device, quit.
    452   1.1     glass 	 *
    453   1.1     glass 	 * The partial writes represent two cases.  1: Where the input block
    454   1.1     glass 	 * was less than expected so the output block was less than expected.
    455   1.1     glass 	 * 2: Where the input block was the right size but we were forced to
    456   1.1     glass 	 * write the block in multiple chunks.  The original versions of dd(1)
    457   1.1     glass 	 * never wrote a block in more than a single write, so the latter case
    458   1.1     glass 	 * never happened.
    459   1.1     glass 	 *
    460   1.1     glass 	 * One special case is if we're forced to do the write -- in that case
    461   1.1     glass 	 * we play games with the buffer size, and it's usually a partial write.
    462   1.1     glass 	 */
    463   1.1     glass 	outp = out.db;
    464   1.1     glass 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
    465   1.1     glass 		for (cnt = n;; cnt -= nw) {
    466  1.30  jschauma 
    467  1.36       dbj 			if (!force && ddflags & C_SPARSE) {
    468  1.36       dbj 				int sparse, i;
    469  1.36       dbj 				sparse = 1;	/* Is buffer sparse? */
    470  1.36       dbj 				for (i = 0; i < cnt; i++)
    471  1.36       dbj 					if (outp[i] != 0) {
    472  1.36       dbj 						sparse = 0;
    473  1.36       dbj 						break;
    474  1.36       dbj 					}
    475  1.36       dbj 				if (sparse) {
    476  1.36       dbj 					pending += cnt;
    477  1.36       dbj 					outp += cnt;
    478  1.36       dbj 					nw = 0;
    479  1.36       dbj 					break;
    480  1.36       dbj 				}
    481  1.36       dbj 			}
    482  1.36       dbj 			if (pending != 0) {
    483  1.36       dbj 				if (lseek(out.fd, pending, SEEK_CUR) ==
    484  1.36       dbj 				    -1)
    485  1.36       dbj 					err(EXIT_FAILURE, "%s: seek error creating sparse file",
    486  1.36       dbj 					    out.name);
    487  1.36       dbj 			}
    488  1.15  christos 			nw = bwrite(out.fd, outp, cnt);
    489   1.3   mycroft 			if (nw <= 0) {
    490   1.3   mycroft 				if (nw == 0)
    491  1.30  jschauma 					errx(EXIT_FAILURE,
    492  1.33  jschauma 						"%s: end of device", out.name);
    493  1.30  jschauma 					/* NOTREACHED */
    494   1.3   mycroft 				if (errno != EINTR)
    495  1.33  jschauma 					err(EXIT_FAILURE, "%s", out.name);
    496  1.30  jschauma 					/* NOTREACHED */
    497   1.3   mycroft 				nw = 0;
    498   1.3   mycroft 			}
    499  1.36       dbj 			if (pending) {
    500  1.36       dbj 				st.bytes += pending;
    501  1.36       dbj 				st.sparse += pending/out.dbsz;
    502  1.36       dbj 				st.out_full += pending/out.dbsz;
    503  1.36       dbj 				pending = 0;
    504  1.36       dbj 			}
    505   1.1     glass 			outp += nw;
    506   1.1     glass 			st.bytes += nw;
    507   1.1     glass 			if (nw == n) {
    508   1.1     glass 				if (n != out.dbsz)
    509   1.1     glass 					++st.out_part;
    510   1.1     glass 				else
    511   1.1     glass 					++st.out_full;
    512   1.1     glass 				break;
    513   1.1     glass 			}
    514   1.1     glass 			++st.out_part;
    515  1.33  jschauma 			if (nw == cnt)
    516   1.1     glass 				break;
    517   1.1     glass 			if (out.flags & ISCHR && !warned) {
    518   1.1     glass 				warned = 1;
    519  1.33  jschauma 				warnx("%s: short write on character device", out.name);
    520   1.1     glass 			}
    521   1.1     glass 			if (out.flags & ISTAPE)
    522  1.30  jschauma 				errx(EXIT_FAILURE,
    523  1.33  jschauma 					"%s: short write on tape device", out.name);
    524  1.30  jschauma 				/* NOTREACHED */
    525  1.30  jschauma 
    526   1.1     glass 		}
    527   1.1     glass 		if ((out.dbcnt -= n) < out.dbsz)
    528   1.1     glass 			break;
    529   1.1     glass 	}
    530   1.1     glass 
    531   1.1     glass 	/* Reassemble the output block. */
    532   1.1     glass 	if (out.dbcnt)
    533  1.10   mycroft 		(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
    534   1.1     glass 	out.dbp = out.db + out.dbcnt;
    535  1.13   hubertf 
    536  1.13   hubertf 	if (progress)
    537  1.13   hubertf 		(void)write(STDERR_FILENO, ".", 1);
    538  1.15  christos }
    539  1.15  christos 
    540  1.15  christos /*
    541  1.15  christos  * A protected against SIGINFO write
    542  1.15  christos  */
    543  1.15  christos ssize_t
    544  1.21     lukem bwrite(int fd, const void *buf, size_t len)
    545  1.15  christos {
    546  1.15  christos 	sigset_t oset;
    547  1.15  christos 	ssize_t rv;
    548  1.15  christos 	int oerrno;
    549  1.15  christos 
    550  1.16  christos 	(void)sigprocmask(SIG_BLOCK, &infoset, &oset);
    551  1.15  christos 	rv = write(fd, buf, len);
    552  1.15  christos 	oerrno = errno;
    553  1.15  christos 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    554  1.15  christos 	errno = oerrno;
    555  1.25     enami 	return (rv);
    556   1.1     glass }
    557