dd.c revision 266488
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#ifndef lint
36static char const copyright[] =
37"@(#) Copyright (c) 1991, 1993, 1994\n\
38	The Regents of the University of California.  All rights reserved.\n";
39#endif /* not lint */
40
41#ifndef lint
42static char sccsid[] = "@(#)dd.c	8.5 (Berkeley) 4/2/94";
43#endif /* not lint */
44#endif
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: stable/10/bin/dd/dd.c 266488 2014-05-21 07:21:36Z thomas $");
47
48#include <sys/param.h>
49#include <sys/stat.h>
50#include <sys/conf.h>
51#include <sys/disklabel.h>
52#include <sys/filio.h>
53#include <sys/time.h>
54
55#include <assert.h>
56#include <ctype.h>
57#include <err.h>
58#include <errno.h>
59#include <fcntl.h>
60#include <inttypes.h>
61#include <locale.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
66
67#include "dd.h"
68#include "extern.h"
69
70static void dd_close(void);
71static void dd_in(void);
72static void getfdtype(IO *);
73static void setup(void);
74
75IO	in, out;		/* input/output state */
76STAT	st;			/* statistics */
77void	(*cfunc)(void);		/* conversion function */
78uintmax_t cpy_cnt;		/* # of blocks to copy */
79static off_t	pending = 0;	/* pending seek if sparse */
80static off_t	last_sp = 0;	/* size of last added sparse block */
81u_int	ddflags = 0;		/* conversion options */
82size_t	cbsz;			/* conversion block size */
83uintmax_t files_cnt = 1;	/* # of files to copy */
84const	u_char *ctab;		/* conversion table */
85char	fill_char;		/* Character to fill with if defined */
86volatile sig_atomic_t need_summary;
87
88int
89main(int argc __unused, char *argv[])
90{
91	(void)setlocale(LC_CTYPE, "");
92	jcl(argv);
93	setup();
94
95	(void)signal(SIGINFO, siginfo_handler);
96	(void)signal(SIGINT, terminate);
97
98	atexit(summary);
99
100	while (files_cnt--)
101		dd_in();
102
103	dd_close();
104	/*
105	 * Some devices such as cfi(4) may perform significant amounts
106	 * of work when a write descriptor is closed.  Close the out
107	 * descriptor explicitly so that the summary handler (called
108	 * from an atexit() hook) includes this work.
109	 */
110	close(out.fd);
111	exit(0);
112}
113
114static int
115parity(u_char c)
116{
117	int i;
118
119	i = c ^ (c >> 1) ^ (c >> 2) ^ (c >> 3) ^
120	    (c >> 4) ^ (c >> 5) ^ (c >> 6) ^ (c >> 7);
121	return (i & 1);
122}
123
124static void
125setup(void)
126{
127	u_int cnt;
128	struct timeval tv;
129
130	if (in.name == NULL) {
131		in.name = "stdin";
132		in.fd = STDIN_FILENO;
133	} else {
134		in.fd = open(in.name, O_RDONLY, 0);
135		if (in.fd == -1)
136			err(1, "%s", in.name);
137	}
138
139	getfdtype(&in);
140
141	if (files_cnt > 1 && !(in.flags & ISTAPE))
142		errx(1, "files is not supported for non-tape devices");
143
144	if (out.name == NULL) {
145		/* No way to check for read access here. */
146		out.fd = STDOUT_FILENO;
147		out.name = "stdout";
148	} else {
149#define	OFLAGS \
150    (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
151		out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
152		/*
153		 * May not have read access, so try again with write only.
154		 * Without read we may have a problem if output also does
155		 * not support seeks.
156		 */
157		if (out.fd == -1) {
158			out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
159			out.flags |= NOREAD;
160		}
161		if (out.fd == -1)
162			err(1, "%s", out.name);
163	}
164
165	getfdtype(&out);
166
167	/*
168	 * Allocate space for the input and output buffers.  If not doing
169	 * record oriented I/O, only need a single buffer.
170	 */
171	if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
172		if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
173			err(1, "input buffer");
174		out.db = in.db;
175	} else if ((in.db = malloc(MAX(in.dbsz, cbsz) + cbsz)) == NULL ||
176	    (out.db = malloc(out.dbsz + cbsz)) == NULL)
177		err(1, "output buffer");
178
179	/* dbp is the first free position in each buffer. */
180	in.dbp = in.db;
181	out.dbp = out.db;
182
183	/* Position the input/output streams. */
184	if (in.offset)
185		pos_in();
186	if (out.offset)
187		pos_out();
188
189	/*
190	 * Truncate the output file.  If it fails on a type of output file
191	 * that it should _not_ fail on, error out.
192	 */
193	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
194	    out.flags & ISTRUNC)
195		if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
196			err(1, "truncating %s", out.name);
197
198	if (ddflags & (C_LCASE  | C_UCASE | C_ASCII | C_EBCDIC | C_PARITY)) {
199		if (ctab != NULL) {
200			for (cnt = 0; cnt <= 0377; ++cnt)
201				casetab[cnt] = ctab[cnt];
202		} else {
203			for (cnt = 0; cnt <= 0377; ++cnt)
204				casetab[cnt] = cnt;
205		}
206		if ((ddflags & C_PARITY) && !(ddflags & C_ASCII)) {
207			/*
208			 * If the input is not EBCDIC, and we do parity
209			 * processing, strip input parity.
210			 */
211			for (cnt = 200; cnt <= 0377; ++cnt)
212				casetab[cnt] = casetab[cnt & 0x7f];
213		}
214		if (ddflags & C_LCASE) {
215			for (cnt = 0; cnt <= 0377; ++cnt)
216				casetab[cnt] = tolower(casetab[cnt]);
217		} else if (ddflags & C_UCASE) {
218			for (cnt = 0; cnt <= 0377; ++cnt)
219				casetab[cnt] = toupper(casetab[cnt]);
220		}
221		if ((ddflags & C_PARITY)) {
222			/*
223			 * This should strictly speaking be a no-op, but I
224			 * wonder what funny LANG settings could get us.
225			 */
226			for (cnt = 0; cnt <= 0377; ++cnt)
227				casetab[cnt] = casetab[cnt] & 0x7f;
228		}
229		if ((ddflags & C_PARSET)) {
230			for (cnt = 0; cnt <= 0377; ++cnt)
231				casetab[cnt] = casetab[cnt] | 0x80;
232		}
233		if ((ddflags & C_PAREVEN)) {
234			for (cnt = 0; cnt <= 0377; ++cnt)
235				if (parity(casetab[cnt]))
236					casetab[cnt] = casetab[cnt] | 0x80;
237		}
238		if ((ddflags & C_PARODD)) {
239			for (cnt = 0; cnt <= 0377; ++cnt)
240				if (!parity(casetab[cnt]))
241					casetab[cnt] = casetab[cnt] | 0x80;
242		}
243
244		ctab = casetab;
245	}
246
247	(void)gettimeofday(&tv, NULL);
248	st.start = tv.tv_sec + tv.tv_usec * 1e-6;
249}
250
251static void
252getfdtype(IO *io)
253{
254	struct stat sb;
255	int type;
256
257	if (fstat(io->fd, &sb) == -1)
258		err(1, "%s", io->name);
259	if (S_ISREG(sb.st_mode))
260		io->flags |= ISTRUNC;
261	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
262		if (ioctl(io->fd, FIODTYPE, &type) == -1) {
263			err(1, "%s", io->name);
264		} else {
265			if (type & D_TAPE)
266				io->flags |= ISTAPE;
267			else if (type & (D_DISK | D_MEM))
268				io->flags |= ISSEEK;
269			if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
270				io->flags |= ISCHR;
271		}
272		return;
273	}
274	errno = 0;
275	if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
276		io->flags |= ISPIPE;
277	else
278		io->flags |= ISSEEK;
279}
280
281static void
282dd_in(void)
283{
284	ssize_t n;
285
286	for (;;) {
287		switch (cpy_cnt) {
288		case -1:			/* count=0 was specified */
289			return;
290		case 0:
291			break;
292		default:
293			if (st.in_full + st.in_part >= (uintmax_t)cpy_cnt)
294				return;
295			break;
296		}
297
298		/*
299		 * Zero the buffer first if sync; if doing block operations,
300		 * use spaces.
301		 */
302		if (ddflags & C_SYNC) {
303			if (ddflags & C_FILL)
304				memset(in.dbp, fill_char, in.dbsz);
305			else if (ddflags & (C_BLOCK | C_UNBLOCK))
306				memset(in.dbp, ' ', in.dbsz);
307			else
308				memset(in.dbp, 0, in.dbsz);
309		}
310
311		n = read(in.fd, in.dbp, in.dbsz);
312		if (n == 0) {
313			in.dbrcnt = 0;
314			return;
315		}
316
317		/* Read error. */
318		if (n == -1) {
319			/*
320			 * If noerror not specified, die.  POSIX requires that
321			 * the warning message be followed by an I/O display.
322			 */
323			if (!(ddflags & C_NOERROR))
324				err(1, "%s", in.name);
325			warn("%s", in.name);
326			summary();
327
328			/*
329			 * If it's a seekable file descriptor, seek past the
330			 * error.  If your OS doesn't do the right thing for
331			 * raw disks this section should be modified to re-read
332			 * in sector size chunks.
333			 */
334			if (in.flags & ISSEEK &&
335			    lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
336				warn("%s", in.name);
337
338			/* If sync not specified, omit block and continue. */
339			if (!(ddflags & C_SYNC))
340				continue;
341
342			/* Read errors count as full blocks. */
343			in.dbcnt += in.dbrcnt = in.dbsz;
344			++st.in_full;
345
346		/* Handle full input blocks. */
347		} else if ((size_t)n == in.dbsz) {
348			in.dbcnt += in.dbrcnt = n;
349			++st.in_full;
350
351		/* Handle partial input blocks. */
352		} else {
353			/* If sync, use the entire block. */
354			if (ddflags & C_SYNC)
355				in.dbcnt += in.dbrcnt = in.dbsz;
356			else
357				in.dbcnt += in.dbrcnt = n;
358			++st.in_part;
359		}
360
361		/*
362		 * POSIX states that if bs is set and no other conversions
363		 * than noerror, notrunc or sync are specified, the block
364		 * is output without buffering as it is read.
365		 */
366		if ((ddflags & ~(C_NOERROR | C_NOTRUNC | C_SYNC)) == C_BS) {
367			out.dbcnt = in.dbcnt;
368			dd_out(1);
369			in.dbcnt = 0;
370			continue;
371		}
372
373		if (ddflags & C_SWAB) {
374			if ((n = in.dbrcnt) & 1) {
375				++st.swab;
376				--n;
377			}
378			swab(in.dbp, in.dbp, (size_t)n);
379		}
380
381		in.dbp += in.dbrcnt;
382		(*cfunc)();
383		if (need_summary) {
384			summary();
385		}
386	}
387}
388
389/*
390 * Clean up any remaining I/O and flush output.  If necessary, the output file
391 * is truncated.
392 */
393static void
394dd_close(void)
395{
396	if (cfunc == def)
397		def_close();
398	else if (cfunc == block)
399		block_close();
400	else if (cfunc == unblock)
401		unblock_close();
402	if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
403		if (ddflags & C_FILL)
404			memset(out.dbp, fill_char, out.dbsz - out.dbcnt);
405		else if (ddflags & (C_BLOCK | C_UNBLOCK))
406			memset(out.dbp, ' ', out.dbsz - out.dbcnt);
407		else
408			memset(out.dbp, 0, out.dbsz - out.dbcnt);
409		out.dbcnt = out.dbsz;
410	}
411	if (out.dbcnt || pending)
412		dd_out(1);
413}
414
415void
416dd_out(int force)
417{
418	u_char *outp;
419	size_t cnt, i, n;
420	ssize_t nw;
421	static int warned;
422	int sparse;
423
424	/*
425	 * Write one or more blocks out.  The common case is writing a full
426	 * output block in a single write; increment the full block stats.
427	 * Otherwise, we're into partial block writes.  If a partial write,
428	 * and it's a character device, just warn.  If a tape device, quit.
429	 *
430	 * The partial writes represent two cases.  1: Where the input block
431	 * was less than expected so the output block was less than expected.
432	 * 2: Where the input block was the right size but we were forced to
433	 * write the block in multiple chunks.  The original versions of dd(1)
434	 * never wrote a block in more than a single write, so the latter case
435	 * never happened.
436	 *
437	 * One special case is if we're forced to do the write -- in that case
438	 * we play games with the buffer size, and it's usually a partial write.
439	 */
440	outp = out.db;
441
442	/*
443	 * If force, first try to write all pending data, else try to write
444	 * just one block. Subsequently always write data one full block at
445	 * a time at most.
446	 */
447	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
448		cnt = n;
449		do {
450			sparse = 0;
451			if (ddflags & C_SPARSE) {
452				sparse = 1;	/* Is buffer sparse? */
453				for (i = 0; i < cnt; i++)
454					if (outp[i] != 0) {
455						sparse = 0;
456						break;
457					}
458			}
459			if (sparse && !force) {
460				pending += cnt;
461				last_sp = cnt;
462				nw = cnt;
463			} else {
464				if (pending != 0) {
465					/* If forced to write, and we have no
466					 * data left, we need to write the last
467					 * sparse block explicitly.
468					 */
469					if (force && cnt == 0) {
470						pending -= last_sp;
471						assert(outp == out.db);
472						memset(outp, 0, cnt);
473					}
474					if (lseek(out.fd, pending, SEEK_CUR) ==
475					    -1)
476						err(2, "%s: seek error creating sparse file",
477						    out.name);
478					pending = last_sp = 0;
479				}
480				if (cnt)
481					nw = write(out.fd, outp, cnt);
482				else
483					return;
484			}
485
486			if (nw <= 0) {
487				if (nw == 0)
488					errx(1, "%s: end of device", out.name);
489				if (errno != EINTR)
490					err(1, "%s", out.name);
491				nw = 0;
492			}
493
494			outp += nw;
495			st.bytes += nw;
496
497			if ((size_t)nw == n && n == out.dbsz)
498				++st.out_full;
499			else
500				++st.out_part;
501
502			if ((size_t) nw != cnt) {
503				if (out.flags & ISTAPE)
504					errx(1, "%s: short write on tape device",
505				    	out.name);
506				if (out.flags & ISCHR && !warned) {
507					warned = 1;
508					warnx("%s: short write on character device",
509				    	out.name);
510				}
511			}
512
513			cnt -= nw;
514		} while (cnt != 0);
515
516		if ((out.dbcnt -= n) < out.dbsz)
517			break;
518	}
519
520	/* Reassemble the output block. */
521	if (out.dbcnt)
522		(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
523	out.dbp = out.db + out.dbcnt;
524}
525