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