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