xref: /minix/bin/dd/dd.c (revision 0a6a1f1d)
1 /*	$NetBSD: dd.c,v 1.50 2015/03/18 13:23:49 manu Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Keith Muller of the University of California, San Diego and Lance
9  * Visser of Convex Computer Corporation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
39  The Regents of the University of California.  All rights reserved.");
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)dd.c	8.5 (Berkeley) 4/2/94";
45 #else
46 __RCSID("$NetBSD: dd.c,v 1.50 2015/03/18 13:23:49 manu Exp $");
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53 #include <sys/mtio.h>
54 #include <sys/time.h>
55 
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <locale.h>
61 #include <signal.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 
70 static void dd_close(void);
71 static void dd_in(void);
72 static void getfdtype(IO *);
73 static void redup_clean_fd(IO *);
74 static void setup(void);
75 
76 int main(int, char *[]);
77 
78 IO		in, out;		/* input/output state */
79 STAT		st;			/* statistics */
80 void		(*cfunc)(void);		/* conversion function */
81 uint64_t	cpy_cnt;		/* # of blocks to copy */
82 static off_t	pending = 0;		/* pending seek if sparse */
83 u_int		ddflags;		/* conversion options */
84 #ifdef NO_IOFLAG
85 #define		iflag	O_RDONLY
86 #define		oflag	O_CREAT
87 #else
88 u_int		iflag = O_RDONLY;	/* open(2) flags for input file */
89 u_int		oflag = O_CREAT;	/* open(2) flags for output file */
90 #endif /* NO_IOFLAG */
91 uint64_t	cbsz;			/* conversion block size */
92 u_int		files_cnt = 1;		/* # of files to copy */
93 uint64_t	progress = 0;		/* display sign of life */
94 const u_char	*ctab;			/* conversion table */
95 sigset_t	infoset;		/* a set blocking SIGINFO */
96 const char	*msgfmt = "posix";	/* default summary() message format */
97 
98 /*
99  * Ops for stdin/stdout and crunch'd dd.  These are always host ops.
100  */
101 static const struct ddfops ddfops_stdfd = {
102 	.op_open = open,
103 	.op_close = close,
104 	.op_fcntl = fcntl,
105 	.op_ioctl = ioctl,
106 	.op_fstat = fstat,
107 	.op_fsync = fsync,
108 	.op_ftruncate = ftruncate,
109 	.op_lseek = lseek,
110 	.op_read = read,
111 	.op_write = write,
112 };
113 extern const struct ddfops ddfops_prog;
114 
115 int
116 main(int argc, char *argv[])
117 {
118 	int ch;
119 
120 	setprogname(argv[0]);
121 	(void)setlocale(LC_ALL, "");
122 
123 	while ((ch = getopt(argc, argv, "")) != -1) {
124 		switch (ch) {
125 		default:
126 			errx(EXIT_FAILURE, "usage: dd [operand ...]");
127 			/* NOTREACHED */
128 		}
129 	}
130 	argc -= (optind - 1);
131 	argv += (optind - 1);
132 
133 	jcl(argv);
134 #ifndef CRUNCHOPS
135 	if (ddfops_prog.op_init && ddfops_prog.op_init() == -1)
136 		err(1, "prog init");
137 #endif
138 	setup();
139 
140 	(void)signal(SIGINFO, summaryx);
141 	(void)signal(SIGINT, terminate);
142 	(void)sigemptyset(&infoset);
143 	(void)sigaddset(&infoset, SIGINFO);
144 
145 	(void)atexit(summary);
146 
147 	while (files_cnt--)
148 		dd_in();
149 
150 	dd_close();
151 	exit(0);
152 	/* NOTREACHED */
153 }
154 
155 static void
156 setup(void)
157 {
158 #ifdef CRUNCHOPS
159 	const struct ddfops *prog_ops = &ddfops_stdfd;
160 #else
161 	const struct ddfops *prog_ops = &ddfops_prog;
162 #endif
163 
164 	if (in.name == NULL) {
165 		in.name = "stdin";
166 		in.fd = STDIN_FILENO;
167 		in.ops = &ddfops_stdfd;
168 	} else {
169 		in.ops = prog_ops;
170 		in.fd = ddop_open(in, in.name, iflag, 0);
171 		if (in.fd < 0)
172 			err(EXIT_FAILURE, "%s", in.name);
173 			/* NOTREACHED */
174 
175 		/* Ensure in.fd is outside the stdio descriptor range */
176 		redup_clean_fd(&in);
177 	}
178 
179 	getfdtype(&in);
180 
181 	if (files_cnt > 1 && !(in.flags & ISTAPE)) {
182 		errx(EXIT_FAILURE, "files is not supported for non-tape devices");
183 		/* NOTREACHED */
184 	}
185 
186 	if (out.name == NULL) {
187 		/* No way to check for read access here. */
188 		out.fd = STDOUT_FILENO;
189 		out.name = "stdout";
190 		out.ops = &ddfops_stdfd;
191 	} else {
192 		out.ops = prog_ops;
193 
194 #ifndef NO_IOFLAG
195 		if ((oflag & O_TRUNC) && (ddflags & C_SEEK)) {
196 			errx(EXIT_FAILURE, "oflag=trunc is incompatible "
197 			     "with seek or oseek operands, giving up.");
198 			/* NOTREACHED */
199 		}
200 		if ((oflag & O_TRUNC) && (ddflags & C_NOTRUNC)) {
201 			errx(EXIT_FAILURE, "oflag=trunc is incompatible "
202 			     "with conv=notrunc operand, giving up.");
203 			/* NOTREACHED */
204 		}
205 #endif /* NO_IOFLAG */
206 #define	OFLAGS \
207     (oflag | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
208 		out.fd = ddop_open(out, out.name, O_RDWR | OFLAGS, DEFFILEMODE);
209 		/*
210 		 * May not have read access, so try again with write only.
211 		 * Without read we may have a problem if output also does
212 		 * not support seeks.
213 		 */
214 		if (out.fd < 0) {
215 			out.fd = ddop_open(out, out.name, O_WRONLY | OFLAGS,
216 			    DEFFILEMODE);
217 			out.flags |= NOREAD;
218 		}
219 		if (out.fd < 0) {
220 			err(EXIT_FAILURE, "%s", out.name);
221 			/* NOTREACHED */
222 		}
223 
224 		/* Ensure out.fd is outside the stdio descriptor range */
225 		redup_clean_fd(&out);
226 	}
227 
228 	getfdtype(&out);
229 
230 	/*
231 	 * Allocate space for the input and output buffers.  If not doing
232 	 * record oriented I/O, only need a single buffer.
233 	 */
234 	if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
235 		size_t dbsz = out.dbsz;
236 		if (!(ddflags & C_BS))
237 			dbsz += in.dbsz - 1;
238 		if ((in.db = malloc(dbsz)) == NULL) {
239 			err(EXIT_FAILURE, NULL);
240 			/* NOTREACHED */
241 		}
242 		out.db = in.db;
243 	} else if ((in.db =
244 	    malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
245 	    (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
246 		err(EXIT_FAILURE, NULL);
247 		/* NOTREACHED */
248 	}
249 	in.dbp = in.db;
250 	out.dbp = out.db;
251 
252 	/* Position the input/output streams. */
253 	if (in.offset)
254 		pos_in();
255 	if (out.offset)
256 		pos_out();
257 
258 	/*
259 	 * Truncate the output file; ignore errors because it fails on some
260 	 * kinds of output files, tapes, for example.
261 	 */
262 	if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
263 		(void)ddop_ftruncate(out, out.fd, (off_t)out.offset * out.dbsz);
264 
265 	/*
266 	 * If converting case at the same time as another conversion, build a
267 	 * table that does both at once.  If just converting case, use the
268 	 * built-in tables.
269 	 */
270 	if (ddflags & (C_LCASE|C_UCASE)) {
271 #ifdef	NO_CONV
272 		/* Should not get here, but just in case... */
273 		errx(EXIT_FAILURE, "case conv and -DNO_CONV");
274 		/* NOTREACHED */
275 #else	/* NO_CONV */
276 		u_int cnt;
277 
278 		if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
279 			if (ddflags & C_LCASE) {
280 				for (cnt = 0; cnt < 256; ++cnt)
281 					casetab[cnt] = tolower(ctab[cnt]);
282 			} else {
283 				for (cnt = 0; cnt < 256; ++cnt)
284 					casetab[cnt] = toupper(ctab[cnt]);
285 			}
286 		} else {
287 			if (ddflags & C_LCASE) {
288 				for (cnt = 0; cnt < 256; ++cnt)
289 					casetab[cnt] = tolower(cnt);
290 			} else {
291 				for (cnt = 0; cnt < 256; ++cnt)
292 					casetab[cnt] = toupper(cnt);
293 			}
294 		}
295 
296 		ctab = casetab;
297 #endif	/* NO_CONV */
298 	}
299 
300 	(void)gettimeofday(&st.start, NULL);	/* Statistics timestamp. */
301 }
302 
303 static void
304 getfdtype(IO *io)
305 {
306 	struct mtget mt;
307 	struct stat sb;
308 
309 	if (io->ops->op_fstat(io->fd, &sb)) {
310 		err(EXIT_FAILURE, "%s", io->name);
311 		/* NOTREACHED */
312 	}
313 	if (S_ISCHR(sb.st_mode))
314 		io->flags |= io->ops->op_ioctl(io->fd, MTIOCGET, &mt)
315 		    ? ISCHR : ISTAPE;
316 	else if (io->ops->op_lseek(io->fd, (off_t)0, SEEK_CUR) == -1
317 	    && errno == ESPIPE)
318 		io->flags |= ISPIPE;		/* XXX fixed in 4.4BSD */
319 }
320 
321 /*
322  * Move the parameter file descriptor to a descriptor that is outside the
323  * stdio descriptor range, if necessary.  This is required to avoid
324  * accidentally outputting completion or error messages into the
325  * output file that were intended for the tty.
326  */
327 static void
328 redup_clean_fd(IO *io)
329 {
330 	int fd = io->fd;
331 	int newfd;
332 
333 	if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
334 	    fd != STDERR_FILENO)
335 		/* File descriptor is ok, return immediately. */
336 		return;
337 
338 	/*
339 	 * 3 is the first descriptor greater than STD*_FILENO.  Any
340 	 * free descriptor valued 3 or above is acceptable...
341 	 */
342 	newfd = io->ops->op_fcntl(fd, F_DUPFD, 3);
343 	if (newfd < 0) {
344 		err(EXIT_FAILURE, "dupfd IO");
345 		/* NOTREACHED */
346 	}
347 
348 	io->ops->op_close(fd);
349 	io->fd = newfd;
350 }
351 
352 static void
353 dd_in(void)
354 {
355 	int flags;
356 	int64_t n;
357 
358 	for (flags = ddflags;;) {
359 		if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
360 			return;
361 
362 		/*
363 		 * Clear the buffer first if doing "sync" on input.
364 		 * If doing block operations use spaces.  This will
365 		 * affect not only the C_NOERROR case, but also the
366 		 * last partial input block which should be padded
367 		 * with zero and not garbage.
368 		 */
369 		if (flags & C_SYNC) {
370 			if (flags & (C_BLOCK|C_UNBLOCK))
371 				(void)memset(in.dbp, ' ', in.dbsz);
372 			else
373 				(void)memset(in.dbp, 0, in.dbsz);
374 		}
375 
376 		n = ddop_read(in, in.fd, in.dbp, in.dbsz);
377 		if (n == 0) {
378 			in.dbrcnt = 0;
379 			return;
380 		}
381 
382 		/* Read error. */
383 		if (n < 0) {
384 
385 			/*
386 			 * If noerror not specified, die.  POSIX requires that
387 			 * the warning message be followed by an I/O display.
388 			 */
389 			if (!(flags & C_NOERROR)) {
390 				err(EXIT_FAILURE, "%s", in.name);
391 				/* NOTREACHED */
392 			}
393 			warn("%s", in.name);
394 			summary();
395 
396 			/*
397 			 * If it's not a tape drive or a pipe, seek past the
398 			 * error.  If your OS doesn't do the right thing for
399 			 * raw disks this section should be modified to re-read
400 			 * in sector size chunks.
401 			 */
402 			if (!(in.flags & (ISPIPE|ISTAPE)) &&
403 			    ddop_lseek(in, in.fd, (off_t)in.dbsz, SEEK_CUR))
404 				warn("%s", in.name);
405 
406 			/* If sync not specified, omit block and continue. */
407 			if (!(ddflags & C_SYNC))
408 				continue;
409 
410 			/* Read errors count as full blocks. */
411 			in.dbcnt += in.dbrcnt = in.dbsz;
412 			++st.in_full;
413 
414 		/* Handle full input blocks. */
415 		} else if ((uint64_t)n == in.dbsz) {
416 			in.dbcnt += in.dbrcnt = n;
417 			++st.in_full;
418 
419 		/* Handle partial input blocks. */
420 		} else {
421 			/* If sync, use the entire block. */
422 			if (ddflags & C_SYNC)
423 				in.dbcnt += in.dbrcnt = in.dbsz;
424 			else
425 				in.dbcnt += in.dbrcnt = n;
426 			++st.in_part;
427 		}
428 
429 		/*
430 		 * POSIX states that if bs is set and no other conversions
431 		 * than noerror, notrunc or sync are specified, the block
432 		 * is output without buffering as it is read.
433 		 */
434 		if (ddflags & C_BS) {
435 			out.dbcnt = in.dbcnt;
436 			dd_out(1);
437 			in.dbcnt = 0;
438 			continue;
439 		}
440 
441 		if (ddflags & C_SWAB) {
442 			if ((n = in.dbrcnt) & 1) {
443 				++st.swab;
444 				--n;
445 			}
446 			swab(in.dbp, in.dbp, n);
447 		}
448 
449 		in.dbp += in.dbrcnt;
450 		(*cfunc)();
451 	}
452 }
453 
454 /*
455  * Cleanup any remaining I/O and flush output.  If necessary, output file
456  * is truncated.
457  */
458 static void
459 dd_close(void)
460 {
461 
462 	if (cfunc == def)
463 		def_close();
464 	else if (cfunc == block)
465 		block_close();
466 	else if (cfunc == unblock)
467 		unblock_close();
468 	if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
469 		(void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
470 		out.dbcnt = out.dbsz;
471 	}
472 	/* If there are pending sparse blocks, make sure
473 	 * to write out the final block un-sparse
474 	 */
475 	if ((out.dbcnt == 0) && pending) {
476 		memset(out.db, 0, out.dbsz);
477 		out.dbcnt = out.dbsz;
478 		out.dbp = out.db + out.dbcnt;
479 		pending -= out.dbsz;
480 	}
481 	if (out.dbcnt)
482 		dd_out(1);
483 
484 	/*
485 	 * Reporting nfs write error may be deferred until next
486 	 * write(2) or close(2) system call.  So, we need to do an
487 	 * extra check.  If an output is stdout, the file structure
488 	 * may be shared with other processes and close(2) just
489 	 * decreases the reference count.
490 	 */
491 	if (out.fd == STDOUT_FILENO && ddop_fsync(out, out.fd) == -1
492 	    && errno != EINVAL) {
493 		err(EXIT_FAILURE, "fsync stdout");
494 		/* NOTREACHED */
495 	}
496 	if (ddop_close(out, out.fd) == -1) {
497 		err(EXIT_FAILURE, "close");
498 		/* NOTREACHED */
499 	}
500 }
501 
502 void
503 dd_out(int force)
504 {
505 	static int warned;
506 	int64_t cnt, n, nw;
507 	u_char *outp;
508 
509 	/*
510 	 * Write one or more blocks out.  The common case is writing a full
511 	 * output block in a single write; increment the full block stats.
512 	 * Otherwise, we're into partial block writes.  If a partial write,
513 	 * and it's a character device, just warn.  If a tape device, quit.
514 	 *
515 	 * The partial writes represent two cases.  1: Where the input block
516 	 * was less than expected so the output block was less than expected.
517 	 * 2: Where the input block was the right size but we were forced to
518 	 * write the block in multiple chunks.  The original versions of dd(1)
519 	 * never wrote a block in more than a single write, so the latter case
520 	 * never happened.
521 	 *
522 	 * One special case is if we're forced to do the write -- in that case
523 	 * we play games with the buffer size, and it's usually a partial write.
524 	 */
525 	outp = out.db;
526 	for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
527 		for (cnt = n;; cnt -= nw) {
528 
529 			if (!force && ddflags & C_SPARSE) {
530 				int sparse, i;
531 				sparse = 1;	/* Is buffer sparse? */
532 				for (i = 0; i < cnt; i++)
533 					if (outp[i] != 0) {
534 						sparse = 0;
535 						break;
536 					}
537 				if (sparse) {
538 					pending += cnt;
539 					outp += cnt;
540 					nw = 0;
541 					break;
542 				}
543 			}
544 			if (pending != 0) {
545 				if (ddop_lseek(out,
546 				    out.fd, pending, SEEK_CUR) == -1)
547 					err(EXIT_FAILURE, "%s: seek error creating sparse file",
548 					    out.name);
549 			}
550 			nw = bwrite(&out, outp, cnt);
551 			if (nw <= 0) {
552 				if (nw == 0)
553 					errx(EXIT_FAILURE,
554 						"%s: end of device", out.name);
555 					/* NOTREACHED */
556 				if (errno != EINTR)
557 					err(EXIT_FAILURE, "%s", out.name);
558 					/* NOTREACHED */
559 				nw = 0;
560 			}
561 			if (pending) {
562 				st.bytes += pending;
563 				st.sparse += pending/out.dbsz;
564 				st.out_full += pending/out.dbsz;
565 				pending = 0;
566 			}
567 			outp += nw;
568 			st.bytes += nw;
569 			if (nw == n) {
570 				if ((uint64_t)n != out.dbsz)
571 					++st.out_part;
572 				else
573 					++st.out_full;
574 				break;
575 			}
576 			++st.out_part;
577 			if (nw == cnt)
578 				break;
579 			if (out.flags & ISCHR && !warned) {
580 				warned = 1;
581 				warnx("%s: short write on character device", out.name);
582 			}
583 			if (out.flags & ISTAPE)
584 				errx(EXIT_FAILURE,
585 					"%s: short write on tape device", out.name);
586 				/* NOTREACHED */
587 
588 		}
589 		if ((out.dbcnt -= n) < out.dbsz)
590 			break;
591 	}
592 
593 	/* Reassemble the output block. */
594 	if (out.dbcnt)
595 		(void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
596 	out.dbp = out.db + out.dbcnt;
597 
598 	if (progress && (st.out_full + st.out_part) % progress == 0)
599 		(void)write(STDERR_FILENO, ".", 1);
600 }
601 
602 /*
603  * A protected against SIGINFO write
604  */
605 ssize_t
606 bwrite(IO *io, const void *buf, size_t len)
607 {
608 	sigset_t oset;
609 	ssize_t rv;
610 	int oerrno;
611 
612 	(void)sigprocmask(SIG_BLOCK, &infoset, &oset);
613 	rv = io->ops->op_write(io->fd, buf, len);
614 	oerrno = errno;
615 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
616 	errno = oerrno;
617 	return (rv);
618 }
619