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