Home | History | Annotate | Line # | Download | only in dd
dd.c revision 1.7
      1  1.7  christos /*	$NetBSD: dd.c,v 1.7 1997/07/20 21:58:39 christos 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.1     glass  * 3. All advertising materials mentioning features or use of this software
     20  1.1     glass  *    must display the following acknowledgement:
     21  1.1     glass  *	This product includes software developed by the University of
     22  1.1     glass  *	California, Berkeley and its contributors.
     23  1.1     glass  * 4. Neither the name of the University nor the names of its contributors
     24  1.1     glass  *    may be used to endorse or promote products derived from this software
     25  1.1     glass  *    without specific prior written permission.
     26  1.1     glass  *
     27  1.1     glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1     glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1     glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1     glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1     glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1     glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1     glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1     glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1     glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1     glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1     glass  * SUCH DAMAGE.
     38  1.1     glass  */
     39  1.1     glass 
     40  1.7  christos #include <sys/cdefs.h>
     41  1.1     glass #ifndef lint
     42  1.7  christos __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
     43  1.7  christos 	The Regents of the University of California.  All rights reserved.\n");
     44  1.1     glass #endif /* not lint */
     45  1.1     glass 
     46  1.1     glass #ifndef lint
     47  1.4       cgd #if 0
     48  1.4       cgd static char sccsid[] = "@(#)dd.c	8.5 (Berkeley) 4/2/94";
     49  1.4       cgd #else
     50  1.7  christos __RCSID("$NetBSD: dd.c,v 1.7 1997/07/20 21:58:39 christos Exp $");
     51  1.4       cgd #endif
     52  1.1     glass #endif /* not lint */
     53  1.1     glass 
     54  1.1     glass #include <sys/param.h>
     55  1.1     glass #include <sys/stat.h>
     56  1.1     glass #include <sys/ioctl.h>
     57  1.1     glass #include <sys/mtio.h>
     58  1.1     glass 
     59  1.1     glass #include <ctype.h>
     60  1.3   mycroft #include <err.h>
     61  1.1     glass #include <errno.h>
     62  1.1     glass #include <fcntl.h>
     63  1.1     glass #include <signal.h>
     64  1.1     glass #include <stdio.h>
     65  1.1     glass #include <stdlib.h>
     66  1.1     glass #include <string.h>
     67  1.1     glass #include <unistd.h>
     68  1.1     glass 
     69  1.1     glass #include "dd.h"
     70  1.1     glass #include "extern.h"
     71  1.1     glass 
     72  1.1     glass static void dd_close __P((void));
     73  1.1     glass static void dd_in __P((void));
     74  1.3   mycroft static void getfdtype __P((IO *));
     75  1.1     glass static void setup __P((void));
     76  1.1     glass 
     77  1.7  christos int main __P((int, char *[]));
     78  1.7  christos 
     79  1.1     glass IO	in, out;		/* input/output state */
     80  1.1     glass STAT	st;			/* statistics */
     81  1.1     glass void	(*cfunc) __P((void));	/* conversion function */
     82  1.1     glass u_long	cpy_cnt;		/* # of blocks to copy */
     83  1.1     glass u_int	ddflags;		/* conversion options */
     84  1.1     glass u_int	cbsz;			/* conversion block size */
     85  1.1     glass u_int	files_cnt = 1;		/* # of files to copy */
     86  1.6       jtc const u_char	*ctab;		/* conversion table */
     87  1.1     glass 
     88  1.1     glass int
     89  1.1     glass main(argc, argv)
     90  1.1     glass 	int argc;
     91  1.1     glass 	char *argv[];
     92  1.1     glass {
     93  1.1     glass 	jcl(argv);
     94  1.1     glass 	setup();
     95  1.1     glass 
     96  1.3   mycroft 	(void)signal(SIGINFO, summaryx);
     97  1.1     glass 	(void)signal(SIGINT, terminate);
     98  1.1     glass 
     99  1.3   mycroft 	atexit(summary);
    100  1.3   mycroft 
    101  1.3   mycroft 	while (files_cnt--)
    102  1.1     glass 		dd_in();
    103  1.1     glass 
    104  1.1     glass 	dd_close();
    105  1.1     glass 	exit(0);
    106  1.1     glass }
    107  1.1     glass 
    108  1.1     glass static void
    109  1.1     glass setup()
    110  1.1     glass {
    111  1.3   mycroft 	u_int cnt;
    112  1.1     glass 
    113  1.1     glass 	if (in.name == NULL) {
    114  1.1     glass 		in.name = "stdin";
    115  1.1     glass 		in.fd = STDIN_FILENO;
    116  1.1     glass 	} else {
    117  1.1     glass 		in.fd = open(in.name, O_RDONLY, 0);
    118  1.1     glass 		if (in.fd < 0)
    119  1.3   mycroft 			err(1, "%s", in.name);
    120  1.1     glass 	}
    121  1.1     glass 
    122  1.3   mycroft 	getfdtype(&in);
    123  1.1     glass 
    124  1.1     glass 	if (files_cnt > 1 && !(in.flags & ISTAPE))
    125  1.3   mycroft 		errx(1, "files is not supported for non-tape devices");
    126  1.1     glass 
    127  1.1     glass 	if (out.name == NULL) {
    128  1.1     glass 		/* No way to check for read access here. */
    129  1.1     glass 		out.fd = STDOUT_FILENO;
    130  1.1     glass 		out.name = "stdout";
    131  1.1     glass 	} else {
    132  1.1     glass #define	OFLAGS \
    133  1.1     glass     (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
    134  1.1     glass 		out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
    135  1.1     glass 		/*
    136  1.1     glass 		 * May not have read access, so try again with write only.
    137  1.1     glass 		 * Without read we may have a problem if output also does
    138  1.1     glass 		 * not support seeks.
    139  1.1     glass 		 */
    140  1.1     glass 		if (out.fd < 0) {
    141  1.1     glass 			out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
    142  1.1     glass 			out.flags |= NOREAD;
    143  1.1     glass 		}
    144  1.1     glass 		if (out.fd < 0)
    145  1.3   mycroft 			err(1, "%s", out.name);
    146  1.1     glass 	}
    147  1.1     glass 
    148  1.3   mycroft 	getfdtype(&out);
    149  1.1     glass 
    150  1.1     glass 	/*
    151  1.1     glass 	 * Allocate space for the input and output buffers.  If not doing
    152  1.1     glass 	 * record oriented I/O, only need a single buffer.
    153  1.1     glass 	 */
    154  1.1     glass 	if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
    155  1.1     glass 		if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
    156  1.7  christos 			err(1, "%s", "");
    157  1.1     glass 		out.db = in.db;
    158  1.1     glass 	} else if ((in.db =
    159  1.1     glass 	    malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
    160  1.1     glass 	    (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL)
    161  1.7  christos 		err(1, "%s", "");
    162  1.1     glass 	in.dbp = in.db;
    163  1.1     glass 	out.dbp = out.db;
    164  1.1     glass 
    165  1.1     glass 	/* Position the input/output streams. */
    166  1.1     glass 	if (in.offset)
    167  1.1     glass 		pos_in();
    168  1.1     glass 	if (out.offset)
    169  1.1     glass 		pos_out();
    170  1.1     glass 
    171  1.1     glass 	/*
    172  1.1     glass 	 * Truncate the output file; ignore errors because it fails on some
    173  1.1     glass 	 * kinds of output files, tapes, for example.
    174  1.1     glass 	 */
    175  1.7  christos 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
    176  1.1     glass 		(void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
    177  1.1     glass 
    178  1.1     glass 	/*
    179  1.1     glass 	 * If converting case at the same time as another conversion, build a
    180  1.1     glass 	 * table that does both at once.  If just converting case, use the
    181  1.1     glass 	 * built-in tables.
    182  1.1     glass 	 */
    183  1.5       gwr 	if (ddflags & (C_LCASE|C_UCASE)) {
    184  1.5       gwr #ifdef	NO_CONV
    185  1.5       gwr 		/* Should not get here, but just in case... */
    186  1.5       gwr 		errx(1, "case conv and -DNO_CONV");
    187  1.5       gwr #else	/* NO_CONV */
    188  1.6       jtc 		if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
    189  1.1     glass 			if (ddflags & C_LCASE) {
    190  1.1     glass 				for (cnt = 0; cnt < 0377; ++cnt)
    191  1.6       jtc 					casetab[cnt] = tolower(ctab[cnt]);
    192  1.1     glass 			} else {
    193  1.1     glass 				for (cnt = 0; cnt < 0377; ++cnt)
    194  1.6       jtc 					casetab[cnt] = toupper(ctab[cnt]);
    195  1.1     glass 			}
    196  1.6       jtc 		} else {
    197  1.1     glass 			if (ddflags & C_LCASE) {
    198  1.1     glass 				for (cnt = 0; cnt < 0377; ++cnt)
    199  1.6       jtc 					casetab[cnt] = tolower(cnt);
    200  1.1     glass 			} else {
    201  1.1     glass 				for (cnt = 0; cnt < 0377; ++cnt)
    202  1.6       jtc 					casetab[cnt] = toupper(cnt);
    203  1.1     glass 			}
    204  1.6       jtc 		}
    205  1.6       jtc 
    206  1.6       jtc 		ctab = casetab;
    207  1.5       gwr #endif	/* NO_CONV */
    208  1.5       gwr 	}
    209  1.5       gwr 
    210  1.1     glass 	(void)time(&st.start);			/* Statistics timestamp. */
    211  1.1     glass }
    212  1.1     glass 
    213  1.1     glass static void
    214  1.3   mycroft getfdtype(io)
    215  1.3   mycroft 	IO *io;
    216  1.3   mycroft {
    217  1.3   mycroft 	struct mtget mt;
    218  1.3   mycroft 	struct stat sb;
    219  1.3   mycroft 
    220  1.3   mycroft 	if (fstat(io->fd, &sb))
    221  1.3   mycroft 		err(1, "%s", io->name);
    222  1.3   mycroft 	if (S_ISCHR(sb.st_mode))
    223  1.3   mycroft 		io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
    224  1.3   mycroft 	else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
    225  1.3   mycroft 		io->flags |= ISPIPE;		/* XXX fixed in 4.4BSD */
    226  1.3   mycroft }
    227  1.3   mycroft 
    228  1.3   mycroft static void
    229  1.1     glass dd_in()
    230  1.1     glass {
    231  1.3   mycroft 	int flags, n;
    232  1.1     glass 
    233  1.1     glass 	for (flags = ddflags;;) {
    234  1.1     glass 		if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
    235  1.1     glass 			return;
    236  1.1     glass 
    237  1.1     glass 		/*
    238  1.1     glass 		 * Zero the buffer first if trying to recover from errors so
    239  1.1     glass 		 * lose the minimum amount of data.  If doing block operations
    240  1.1     glass 		 * use spaces.
    241  1.1     glass 		 */
    242  1.3   mycroft 		if ((flags & (C_NOERROR|C_SYNC)) == (C_NOERROR|C_SYNC))
    243  1.1     glass 			if (flags & (C_BLOCK|C_UNBLOCK))
    244  1.1     glass 				memset(in.dbp, ' ', in.dbsz);
    245  1.1     glass 			else
    246  1.1     glass 				memset(in.dbp, 0, in.dbsz);
    247  1.1     glass 
    248  1.1     glass 		n = read(in.fd, in.dbp, in.dbsz);
    249  1.1     glass 		if (n == 0) {
    250  1.1     glass 			in.dbrcnt = 0;
    251  1.1     glass 			return;
    252  1.1     glass 		}
    253  1.1     glass 
    254  1.1     glass 		/* Read error. */
    255  1.1     glass 		if (n < 0) {
    256  1.1     glass 			/*
    257  1.1     glass 			 * If noerror not specified, die.  POSIX requires that
    258  1.1     glass 			 * the warning message be followed by an I/O display.
    259  1.1     glass 			 */
    260  1.1     glass 			if (!(flags & C_NOERROR))
    261  1.3   mycroft 				err(1, "%s", in.name);
    262  1.3   mycroft 			warn("%s", in.name);
    263  1.3   mycroft 			summary();
    264  1.1     glass 
    265  1.1     glass 			/*
    266  1.1     glass 			 * If it's not a tape drive or a pipe, seek past the
    267  1.1     glass 			 * error.  If your OS doesn't do the right thing for
    268  1.1     glass 			 * raw disks this section should be modified to re-read
    269  1.1     glass 			 * in sector size chunks.
    270  1.1     glass 			 */
    271  1.1     glass 			if (!(in.flags & (ISPIPE|ISTAPE)) &&
    272  1.1     glass 			    lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
    273  1.3   mycroft 				warn("%s", in.name);
    274  1.1     glass 
    275  1.1     glass 			/* If sync not specified, omit block and continue. */
    276  1.1     glass 			if (!(ddflags & C_SYNC))
    277  1.1     glass 				continue;
    278  1.1     glass 
    279  1.1     glass 			/* Read errors count as full blocks. */
    280  1.1     glass 			in.dbcnt += in.dbrcnt = in.dbsz;
    281  1.1     glass 			++st.in_full;
    282  1.1     glass 
    283  1.1     glass 		/* Handle full input blocks. */
    284  1.1     glass 		} else if (n == in.dbsz) {
    285  1.1     glass 			in.dbcnt += in.dbrcnt = n;
    286  1.1     glass 			++st.in_full;
    287  1.1     glass 
    288  1.1     glass 		/* Handle partial input blocks. */
    289  1.1     glass 		} else {
    290  1.1     glass 			/* If sync, use the entire block. */
    291  1.1     glass 			if (ddflags & C_SYNC)
    292  1.1     glass 				in.dbcnt += in.dbrcnt = in.dbsz;
    293  1.1     glass 			else
    294  1.1     glass 				in.dbcnt += in.dbrcnt = n;
    295  1.1     glass 			++st.in_part;
    296  1.1     glass 		}
    297  1.1     glass 
    298  1.1     glass 		/*
    299  1.1     glass 		 * POSIX states that if bs is set and no other conversions
    300  1.1     glass 		 * than noerror, notrunc or sync are specified, the block
    301  1.1     glass 		 * is output without buffering as it is read.
    302  1.1     glass 		 */
    303  1.1     glass 		if (ddflags & C_BS) {
    304  1.1     glass 			out.dbcnt = in.dbcnt;
    305  1.1     glass 			dd_out(1);
    306  1.1     glass 			in.dbcnt = 0;
    307  1.1     glass 			continue;
    308  1.1     glass 		}
    309  1.1     glass 
    310  1.1     glass 		if (ddflags & C_SWAB) {
    311  1.1     glass 			if ((n = in.dbcnt) & 1) {
    312  1.1     glass 				++st.swab;
    313  1.1     glass 				--n;
    314  1.1     glass 			}
    315  1.1     glass 			swab(in.dbp, in.dbp, n);
    316  1.1     glass 		}
    317  1.1     glass 
    318  1.1     glass 		in.dbp += in.dbrcnt;
    319  1.1     glass 		(*cfunc)();
    320  1.1     glass 	}
    321  1.1     glass }
    322  1.1     glass 
    323  1.1     glass /*
    324  1.1     glass  * Cleanup any remaining I/O and flush output.  If necesssary, output file
    325  1.1     glass  * is truncated.
    326  1.1     glass  */
    327  1.1     glass static void
    328  1.1     glass dd_close()
    329  1.1     glass {
    330  1.1     glass 	if (cfunc == def)
    331  1.1     glass 		def_close();
    332  1.1     glass 	else if (cfunc == block)
    333  1.1     glass 		block_close();
    334  1.1     glass 	else if (cfunc == unblock)
    335  1.1     glass 		unblock_close();
    336  1.3   mycroft 	if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
    337  1.3   mycroft 		memset(out.dbp, 0, out.dbsz - out.dbcnt);
    338  1.3   mycroft 		out.dbcnt = out.dbsz;
    339  1.3   mycroft 	}
    340  1.1     glass 	if (out.dbcnt)
    341  1.1     glass 		dd_out(1);
    342  1.1     glass }
    343  1.1     glass 
    344  1.1     glass void
    345  1.1     glass dd_out(force)
    346  1.1     glass 	int force;
    347  1.1     glass {
    348  1.1     glass 	static int warned;
    349  1.3   mycroft 	int cnt, n, nw;
    350  1.3   mycroft 	u_char *outp;
    351  1.1     glass 
    352  1.1     glass 	/*
    353  1.1     glass 	 * Write one or more blocks out.  The common case is writing a full
    354  1.1     glass 	 * output block in a single write; increment the full block stats.
    355  1.1     glass 	 * Otherwise, we're into partial block writes.  If a partial write,
    356  1.1     glass 	 * and it's a character device, just warn.  If a tape device, quit.
    357  1.1     glass 	 *
    358  1.1     glass 	 * The partial writes represent two cases.  1: Where the input block
    359  1.1     glass 	 * was less than expected so the output block was less than expected.
    360  1.1     glass 	 * 2: Where the input block was the right size but we were forced to
    361  1.1     glass 	 * write the block in multiple chunks.  The original versions of dd(1)
    362  1.1     glass 	 * never wrote a block in more than a single write, so the latter case
    363  1.1     glass 	 * never happened.
    364  1.1     glass 	 *
    365  1.1     glass 	 * One special case is if we're forced to do the write -- in that case
    366  1.1     glass 	 * we play games with the buffer size, and it's usually a partial write.
    367  1.1     glass 	 */
    368  1.1     glass 	outp = out.db;
    369  1.1     glass 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
    370  1.1     glass 		for (cnt = n;; cnt -= nw) {
    371  1.1     glass 			nw = write(out.fd, outp, cnt);
    372  1.3   mycroft 			if (nw <= 0) {
    373  1.3   mycroft 				if (nw == 0)
    374  1.3   mycroft 					errx(1, "%s: end of device", out.name);
    375  1.3   mycroft 				if (errno != EINTR)
    376  1.3   mycroft 					err(1, "%s", out.name);
    377  1.3   mycroft 				nw = 0;
    378  1.3   mycroft 			}
    379  1.1     glass 			outp += nw;
    380  1.1     glass 			st.bytes += nw;
    381  1.1     glass 			if (nw == n) {
    382  1.1     glass 				if (n != out.dbsz)
    383  1.1     glass 					++st.out_part;
    384  1.1     glass 				else
    385  1.1     glass 					++st.out_full;
    386  1.1     glass 				break;
    387  1.1     glass 			}
    388  1.1     glass 			++st.out_part;
    389  1.1     glass 			if (nw == cnt)
    390  1.1     glass 				break;
    391  1.1     glass 			if (out.flags & ISCHR && !warned) {
    392  1.1     glass 				warned = 1;
    393  1.3   mycroft 				warnx("%s: short write on character device",
    394  1.1     glass 				    out.name);
    395  1.1     glass 			}
    396  1.1     glass 			if (out.flags & ISTAPE)
    397  1.3   mycroft 				errx(1, "%s: short write on tape device", out.name);
    398  1.1     glass 		}
    399  1.1     glass 		if ((out.dbcnt -= n) < out.dbsz)
    400  1.1     glass 			break;
    401  1.1     glass 	}
    402  1.1     glass 
    403  1.1     glass 	/* Reassemble the output block. */
    404  1.1     glass 	if (out.dbcnt)
    405  1.1     glass 		memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
    406  1.1     glass 	out.dbp = out.db + out.dbcnt;
    407  1.1     glass }
    408