1 /* $NetBSD: dd.c,v 1.51 2016/09/05 01:00:07 sevan Exp $ */
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
39 The Regents of the University of California. All rights reserved.");
44 static char sccsid
[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
46 __RCSID("$NetBSD: dd.c,v 1.51 2016/09/05 01:00:07 sevan Exp $");
50 #include <sys/param.h>
52 #include <sys/ioctl.h>
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);
76 IO in
, out
; /* input/output state */
77 STAT st
; /* statistics */
78 void (*cfunc
)(void); /* conversion function */
79 uint64_t cpy_cnt
; /* # of blocks to copy */
80 static off_t pending
= 0; /* pending seek if sparse */
81 u_int ddflags
; /* conversion options */
83 #define iflag O_RDONLY
86 u_int iflag
= O_RDONLY
; /* open(2) flags for input file */
87 u_int oflag
= O_CREAT
; /* open(2) flags for output file */
88 #endif /* NO_IOFLAG */
89 uint64_t cbsz
; /* conversion block size */
90 u_int files_cnt
= 1; /* # of files to copy */
91 uint64_t progress
= 0; /* display sign of life */
92 const u_char
*ctab
; /* conversion table */
93 sigset_t infoset
; /* a set blocking SIGINFO */
94 const char *msgfmt
= "posix"; /* default summary() message format */
97 * Ops for stdin/stdout and crunch'd dd. These are always host ops.
99 static const struct ddfops ddfops_stdfd
= {
106 .op_ftruncate
= ftruncate
,
111 extern const struct ddfops ddfops_prog
;
114 main(int argc
, char *argv
[])
118 setprogname(argv
[0]);
119 (void)setlocale(LC_ALL
, "");
121 while ((ch
= getopt(argc
, argv
, "")) != -1) {
124 errx(EXIT_FAILURE
, "usage: dd [operand ...]");
128 argc
-= (optind
- 1);
129 argv
+= (optind
- 1);
133 if (ddfops_prog
.op_init
&& ddfops_prog
.op_init() == -1)
138 (void)signal(SIGINFO
, summaryx
);
139 (void)signal(SIGINT
, terminate
);
140 (void)sigemptyset(&infoset
);
141 (void)sigaddset(&infoset
, SIGINFO
);
143 (void)atexit(summary
);
157 const struct ddfops
*prog_ops
= &ddfops_stdfd
;
159 const struct ddfops
*prog_ops
= &ddfops_prog
;
162 if (in
.name
== NULL
) {
164 in
.fd
= STDIN_FILENO
;
165 in
.ops
= &ddfops_stdfd
;
168 in
.fd
= ddop_open(in
, in
.name
, iflag
, 0);
170 err(EXIT_FAILURE
, "%s", in
.name
);
173 /* Ensure in.fd is outside the stdio descriptor range */
179 if (files_cnt
> 1 && !(in
.flags
& ISTAPE
)) {
180 errx(EXIT_FAILURE
, "files is not supported for non-tape devices");
184 if (out
.name
== NULL
) {
185 /* No way to check for read access here. */
186 out
.fd
= STDOUT_FILENO
;
188 out
.ops
= &ddfops_stdfd
;
193 if ((oflag
& O_TRUNC
) && (ddflags
& C_SEEK
)) {
194 errx(EXIT_FAILURE
, "oflag=trunc is incompatible "
195 "with seek or oseek operands, giving up.");
198 if ((oflag
& O_TRUNC
) && (ddflags
& C_NOTRUNC
)) {
199 errx(EXIT_FAILURE
, "oflag=trunc is incompatible "
200 "with conv=notrunc operand, giving up.");
203 #endif /* NO_IOFLAG */
205 (oflag | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
206 out
.fd
= ddop_open(out
, out
.name
, O_RDWR
| OFLAGS
, DEFFILEMODE
);
208 * May not have read access, so try again with write only.
209 * Without read we may have a problem if output also does
213 out
.fd
= ddop_open(out
, out
.name
, O_WRONLY
| OFLAGS
,
218 err(EXIT_FAILURE
, "%s", out
.name
);
222 /* Ensure out.fd is outside the stdio descriptor range */
223 redup_clean_fd(&out
);
229 * Allocate space for the input and output buffers. If not doing
230 * record oriented I/O, only need a single buffer.
232 if (!(ddflags
& (C_BLOCK
|C_UNBLOCK
))) {
233 size_t dbsz
= out
.dbsz
;
234 if (!(ddflags
& C_BS
))
236 if ((in
.db
= malloc(dbsz
)) == NULL
) {
237 err(EXIT_FAILURE
, NULL
);
242 malloc((u_int
)(MAX(in
.dbsz
, cbsz
) + cbsz
))) == NULL
||
243 (out
.db
= malloc((u_int
)(out
.dbsz
+ cbsz
))) == NULL
) {
244 err(EXIT_FAILURE
, NULL
);
250 /* Position the input/output streams. */
257 * Truncate the output file; ignore errors because it fails on some
258 * kinds of output files, tapes, for example.
260 if ((ddflags
& (C_OF
| C_SEEK
| C_NOTRUNC
)) == (C_OF
| C_SEEK
))
261 (void)ddop_ftruncate(out
, out
.fd
, (off_t
)out
.offset
* out
.dbsz
);
264 * If converting case at the same time as another conversion, build a
265 * table that does both at once. If just converting case, use the
268 if (ddflags
& (C_LCASE
|C_UCASE
)) {
270 /* Should not get here, but just in case... */
271 errx(EXIT_FAILURE
, "case conv and -DNO_CONV");
276 if (ddflags
& C_ASCII
|| ddflags
& C_EBCDIC
) {
277 if (ddflags
& C_LCASE
) {
278 for (cnt
= 0; cnt
< 256; ++cnt
)
279 casetab
[cnt
] = tolower(ctab
[cnt
]);
281 for (cnt
= 0; cnt
< 256; ++cnt
)
282 casetab
[cnt
] = toupper(ctab
[cnt
]);
285 if (ddflags
& C_LCASE
) {
286 for (cnt
= 0; cnt
< 256; ++cnt
)
287 casetab
[cnt
] = tolower(cnt
);
289 for (cnt
= 0; cnt
< 256; ++cnt
)
290 casetab
[cnt
] = toupper(cnt
);
298 (void)gettimeofday(&st
.start
, NULL
); /* Statistics timestamp. */
307 if (io
->ops
->op_fstat(io
->fd
, &sb
)) {
308 err(EXIT_FAILURE
, "%s", io
->name
);
311 if (S_ISCHR(sb
.st_mode
))
312 io
->flags
|= io
->ops
->op_ioctl(io
->fd
, MTIOCGET
, &mt
)
314 else if (io
->ops
->op_lseek(io
->fd
, (off_t
)0, SEEK_CUR
) == -1
316 io
->flags
|= ISPIPE
; /* XXX fixed in 4.4BSD */
320 * Move the parameter file descriptor to a descriptor that is outside the
321 * stdio descriptor range, if necessary. This is required to avoid
322 * accidentally outputting completion or error messages into the
323 * output file that were intended for the tty.
326 redup_clean_fd(IO
*io
)
331 if (fd
!= STDIN_FILENO
&& fd
!= STDOUT_FILENO
&&
333 /* File descriptor is ok, return immediately. */
337 * 3 is the first descriptor greater than STD*_FILENO. Any
338 * free descriptor valued 3 or above is acceptable...
340 newfd
= io
->ops
->op_fcntl(fd
, F_DUPFD
, 3);
342 err(EXIT_FAILURE
, "dupfd IO");
346 io
->ops
->op_close(fd
);
356 for (flags
= ddflags
;;) {
357 if (cpy_cnt
&& (st
.in_full
+ st
.in_part
) >= cpy_cnt
)
361 * Clear the buffer first if doing "sync" on input.
362 * If doing block operations use spaces. This will
363 * affect not only the C_NOERROR case, but also the
364 * last partial input block which should be padded
365 * with zero and not garbage.
367 if (flags
& C_SYNC
) {
368 if (flags
& (C_BLOCK
|C_UNBLOCK
))
369 (void)memset(in
.dbp
, ' ', in
.dbsz
);
371 (void)memset(in
.dbp
, 0, in
.dbsz
);
374 n
= ddop_read(in
, in
.fd
, in
.dbp
, in
.dbsz
);
384 * If noerror not specified, die. POSIX requires that
385 * the warning message be followed by an I/O display.
387 if (!(flags
& C_NOERROR
)) {
388 err(EXIT_FAILURE
, "%s", in
.name
);
395 * If it's not a tape drive or a pipe, seek past the
396 * error. If your OS doesn't do the right thing for
397 * raw disks this section should be modified to re-read
398 * in sector size chunks.
400 if (!(in
.flags
& (ISPIPE
|ISTAPE
)) &&
401 ddop_lseek(in
, in
.fd
, (off_t
)in
.dbsz
, SEEK_CUR
))
404 /* If sync not specified, omit block and continue. */
405 if (!(ddflags
& C_SYNC
))
408 /* Read errors count as full blocks. */
409 in
.dbcnt
+= in
.dbrcnt
= in
.dbsz
;
412 /* Handle full input blocks. */
413 } else if ((uint64_t)n
== in
.dbsz
) {
414 in
.dbcnt
+= in
.dbrcnt
= n
;
417 /* Handle partial input blocks. */
419 /* If sync, use the entire block. */
420 if (ddflags
& C_SYNC
)
421 in
.dbcnt
+= in
.dbrcnt
= in
.dbsz
;
423 in
.dbcnt
+= in
.dbrcnt
= n
;
428 * POSIX states that if bs is set and no other conversions
429 * than noerror, notrunc or sync are specified, the block
430 * is output without buffering as it is read.
432 if (ddflags
& C_BS
) {
433 out
.dbcnt
= in
.dbcnt
;
439 if (ddflags
& C_SWAB
) {
440 if ((n
= in
.dbrcnt
) & 1) {
444 swab(in
.dbp
, in
.dbp
, n
);
453 * Cleanup any remaining I/O and flush output. If necessary, output file
462 else if (cfunc
== block
)
464 else if (cfunc
== unblock
)
466 if (ddflags
& C_OSYNC
&& out
.dbcnt
< out
.dbsz
) {
467 (void)memset(out
.dbp
, 0, out
.dbsz
- out
.dbcnt
);
468 out
.dbcnt
= out
.dbsz
;
470 /* If there are pending sparse blocks, make sure
471 * to write out the final block un-sparse
473 if ((out
.dbcnt
== 0) && pending
) {
474 memset(out
.db
, 0, out
.dbsz
);
475 out
.dbcnt
= out
.dbsz
;
476 out
.dbp
= out
.db
+ out
.dbcnt
;
483 * Reporting nfs write error may be deferred until next
484 * write(2) or close(2) system call. So, we need to do an
485 * extra check. If an output is stdout, the file structure
486 * may be shared with other processes and close(2) just
487 * decreases the reference count.
489 if (out
.fd
== STDOUT_FILENO
&& ddop_fsync(out
, out
.fd
) == -1
490 && errno
!= EINVAL
) {
491 err(EXIT_FAILURE
, "fsync stdout");
494 if (ddop_close(out
, out
.fd
) == -1) {
495 err(EXIT_FAILURE
, "close");
508 * Write one or more blocks out. The common case is writing a full
509 * output block in a single write; increment the full block stats.
510 * Otherwise, we're into partial block writes. If a partial write,
511 * and it's a character device, just warn. If a tape device, quit.
513 * The partial writes represent two cases. 1: Where the input block
514 * was less than expected so the output block was less than expected.
515 * 2: Where the input block was the right size but we were forced to
516 * write the block in multiple chunks. The original versions of dd(1)
517 * never wrote a block in more than a single write, so the latter case
520 * One special case is if we're forced to do the write -- in that case
521 * we play games with the buffer size, and it's usually a partial write.
524 for (n
= force
? out
.dbcnt
: out
.dbsz
;; n
= out
.dbsz
) {
525 for (cnt
= n
;; cnt
-= nw
) {
527 if (!force
&& ddflags
& C_SPARSE
) {
529 sparse
= 1; /* Is buffer sparse? */
530 for (i
= 0; i
< cnt
; i
++)
544 out
.fd
, pending
, SEEK_CUR
) == -1)
545 err(EXIT_FAILURE
, "%s: seek error creating sparse file",
548 nw
= bwrite(&out
, outp
, cnt
);
552 "%s: end of device", out
.name
);
555 err(EXIT_FAILURE
, "%s", out
.name
);
561 st
.sparse
+= pending
/out
.dbsz
;
562 st
.out_full
+= pending
/out
.dbsz
;
568 if ((uint64_t)n
!= out
.dbsz
)
577 if (out
.flags
& ISCHR
&& !warned
) {
579 warnx("%s: short write on character device", out
.name
);
581 if (out
.flags
& ISTAPE
)
583 "%s: short write on tape device", out
.name
);
587 if ((out
.dbcnt
-= n
) < out
.dbsz
)
591 /* Reassemble the output block. */
593 (void)memmove(out
.db
, out
.dbp
- out
.dbcnt
, out
.dbcnt
);
594 out
.dbp
= out
.db
+ out
.dbcnt
;
596 if (progress
&& (st
.out_full
+ st
.out_part
) % progress
== 0)
597 (void)write(STDERR_FILENO
, ".", 1);
601 * A protected against SIGINFO write
604 bwrite(IO
*io
, const void *buf
, size_t len
)
610 (void)sigprocmask(SIG_BLOCK
, &infoset
, &oset
);
611 rv
= io
->ops
->op_write(io
->fd
, buf
, len
);
613 (void)sigprocmask(SIG_SETMASK
, &oset
, NULL
);