1 /* $NetBSD: dd.c,v 1.55 2026/08/29 00:05:18 kre 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.55 2026/08/29 00:05:18 kre 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 static void *buffer_alloc(size_t); 76 77 IO in, out; /* input/output state */ 78 STAT st; /* statistics */ 79 void (*cfunc)(void); /* conversion function */ 80 uint64_t cpy_cnt; /* # of blocks to copy */ 81 static off_t pending = 0; /* pending seek if sparse */ 82 u_int ddflags; /* conversion options */ 83 #ifdef NO_IOFLAG 84 #define iflag O_RDONLY 85 #define oflag O_CREAT 86 #else 87 u_int iflag = O_RDONLY; /* open(2) flags for input file */ 88 u_int oflag = O_CREAT; /* open(2) flags for output file */ 89 #endif /* NO_IOFLAG */ 90 uint64_t cbsz; /* conversion block size */ 91 u_int files_cnt = 1; /* # of files to copy */ 92 uint64_t progress = 0; /* display sign of life */ 93 const u_char *ctab; /* conversion table */ 94 sigset_t infoset; /* a set blocking SIGINFO */ 95 const char *msgfmt = "posix"; /* default summary() message format */ 96 97 /* 98 * Ops for stdin/stdout and crunch'd dd. These are always host ops. 99 */ 100 static const struct ddfops ddfops_stdfd = { 101 .op_open = open, 102 .op_close = close, 103 .op_fcntl = fcntl, 104 .op_ioctl = ioctl, 105 .op_fstat = fstat, 106 .op_fsync = fsync, 107 .op_ftruncate = ftruncate, 108 .op_lseek = lseek, 109 .op_read = read, 110 .op_write = write, 111 }; 112 extern const struct ddfops ddfops_prog; 113 114 int 115 main(int argc, char *argv[]) 116 { 117 int ch; 118 119 setprogname(argv[0]); 120 (void)setlocale(LC_ALL, ""); 121 122 while ((ch = getopt(argc, argv, "")) != -1) { 123 switch (ch) { 124 default: 125 errx(EXIT_FAILURE, "usage: dd [operand ...]"); 126 /* NOTREACHED */ 127 } 128 } 129 argc -= (optind - 1); 130 argv += (optind - 1); 131 132 jcl(argv); 133 #ifndef CRUNCHOPS 134 if (ddfops_prog.op_init && ddfops_prog.op_init() == -1) 135 err(1, "prog init"); 136 #endif 137 setup(); 138 139 (void)signal(SIGINFO, summaryx); 140 (void)signal(SIGINT, terminate); 141 (void)sigemptyset(&infoset); 142 (void)sigaddset(&infoset, SIGINFO); 143 144 (void)atexit(summary); 145 146 while (files_cnt--) 147 dd_in(); 148 149 dd_close(); 150 exit(0); 151 /* NOTREACHED */ 152 } 153 154 static void * 155 buffer_alloc(size_t sz) 156 { 157 size_t align = getpagesize(); 158 void *res; 159 160 if (sz < align) 161 res = malloc(sz); 162 else if (posix_memalign(&res, align, sz) != 0) 163 res = NULL; 164 165 return res; 166 } 167 168 static void 169 setup(void) 170 { 171 #ifdef CRUNCHOPS 172 const struct ddfops *prog_ops = &ddfops_stdfd; 173 #else 174 const struct ddfops *prog_ops = &ddfops_prog; 175 #endif 176 177 if (in.name == NULL) { 178 in.name = "stdin"; 179 in.fd = STDIN_FILENO; 180 in.ops = &ddfops_stdfd; 181 } else { 182 in.ops = prog_ops; 183 in.fd = ddop_open(in, in.name, iflag, 0); 184 if (in.fd < 0) 185 err(EXIT_FAILURE, "%s", in.name); 186 /* NOTREACHED */ 187 188 /* Ensure in.fd is outside the stdio descriptor range */ 189 redup_clean_fd(&in); 190 } 191 192 getfdtype(&in); 193 194 if (files_cnt > 1 && !(in.flags & ISTAPE)) { 195 errx(EXIT_FAILURE, "files is not supported for non-tape devices"); 196 /* NOTREACHED */ 197 } 198 199 if (out.name == NULL) { 200 /* No way to check for read access here. */ 201 out.fd = STDOUT_FILENO; 202 out.name = "stdout"; 203 out.ops = &ddfops_stdfd; 204 } else { 205 out.ops = prog_ops; 206 207 #ifndef NO_IOFLAG 208 if ((oflag & O_TRUNC) && (ddflags & C_SEEK)) { 209 errx(EXIT_FAILURE, "oflag=trunc is incompatible " 210 "with seek or oseek operands, giving up."); 211 /* NOTREACHED */ 212 } 213 if ((oflag & O_TRUNC) && (ddflags & C_NOTRUNC)) { 214 errx(EXIT_FAILURE, "oflag=trunc is incompatible " 215 "with conv=notrunc operand, giving up."); 216 /* NOTREACHED */ 217 } 218 #endif /* NO_IOFLAG */ 219 #define OFLAGS \ 220 (oflag | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) 221 out.fd = ddop_open(out, out.name, O_RDWR | OFLAGS, DEFFILEMODE); 222 /* 223 * May not have read access, so try again with write only. 224 * Without read we may have a problem if output also does 225 * not support seeks. 226 */ 227 if (out.fd < 0) { 228 out.fd = ddop_open(out, out.name, O_WRONLY | OFLAGS, 229 DEFFILEMODE); 230 out.flags |= NOREAD; 231 } 232 if (out.fd < 0) { 233 err(EXIT_FAILURE, "%s", out.name); 234 /* NOTREACHED */ 235 } 236 237 /* Ensure out.fd is outside the stdio descriptor range */ 238 redup_clean_fd(&out); 239 } 240 241 getfdtype(&out); 242 243 /* 244 * Allocate space for the input and output buffers. If not doing 245 * record oriented I/O, only need a single buffer. 246 */ 247 if (!(ddflags & (C_BLOCK|C_UNBLOCK))) { 248 size_t dbsz = out.dbsz; 249 if (!(ddflags & C_BS)) 250 dbsz += in.dbsz - 1; 251 if ((in.db = buffer_alloc(dbsz)) == NULL) { 252 err(EXIT_FAILURE, NULL); 253 /* NOTREACHED */ 254 } 255 out.db = in.db; 256 } else if ((in.db = 257 buffer_alloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL || 258 (out.db = buffer_alloc((u_int)(out.dbsz + cbsz))) == NULL) { 259 err(EXIT_FAILURE, NULL); 260 /* NOTREACHED */ 261 } 262 in.dbp = in.db; 263 out.dbp = out.db; 264 265 /* Position the input/output streams. */ 266 if (in.offset) 267 pos_in(); 268 if (out.offset) 269 pos_out(); 270 271 /* 272 * Truncate the output file; ignore errors because it fails on some 273 * kinds of output files, tapes, for example. 274 */ 275 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK)) 276 (void)ddop_ftruncate(out, out.fd, (off_t)out.offset * out.dbsz); 277 278 /* 279 * If converting case at the same time as another conversion, build a 280 * table that does both at once. If just converting case, use the 281 * built-in tables. 282 */ 283 if (ddflags & (C_LCASE|C_UCASE)) { 284 #ifdef NO_CONV 285 /* Should not get here, but just in case... */ 286 errx(EXIT_FAILURE, "case conv and -DNO_CONV"); 287 /* NOTREACHED */ 288 #else /* NO_CONV */ 289 u_int cnt; 290 291 if (ddflags & C_ASCII || ddflags & C_EBCDIC) { 292 if (ddflags & C_LCASE) { 293 for (cnt = 0; cnt < 256; ++cnt) 294 casetab[cnt] = tolower(ctab[cnt]); 295 } else { 296 for (cnt = 0; cnt < 256; ++cnt) 297 casetab[cnt] = toupper(ctab[cnt]); 298 } 299 } else { 300 if (ddflags & C_LCASE) { 301 for (cnt = 0; cnt < 256; ++cnt) 302 casetab[cnt] = tolower(cnt); 303 } else { 304 for (cnt = 0; cnt < 256; ++cnt) 305 casetab[cnt] = toupper(cnt); 306 } 307 } 308 309 ctab = casetab; 310 #endif /* NO_CONV */ 311 } 312 313 (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */ 314 } 315 316 static void 317 getfdtype(IO *io) 318 { 319 struct mtget mt; 320 struct stat sb; 321 322 if (io->ops->op_fstat(io->fd, &sb)) { 323 err(EXIT_FAILURE, "%s", io->name); 324 /* NOTREACHED */ 325 } 326 if (S_ISCHR(sb.st_mode)) 327 io->flags |= io->ops->op_ioctl(io->fd, MTIOCGET, &mt) 328 ? ISCHR : ISTAPE; 329 else if (io->ops->op_lseek(io->fd, (off_t)0, SEEK_CUR) == -1 330 && errno == ESPIPE) 331 io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */ 332 } 333 334 /* 335 * Move the parameter file descriptor to a descriptor that is outside the 336 * stdio descriptor range, if necessary. This is required to avoid 337 * accidentally outputting completion or error messages into the 338 * output file that were intended for the tty. 339 */ 340 static void 341 redup_clean_fd(IO *io) 342 { 343 int fd = io->fd; 344 int newfd; 345 346 if (fd != STDIN_FILENO && fd != STDOUT_FILENO && 347 fd != STDERR_FILENO) 348 /* File descriptor is ok, return immediately. */ 349 return; 350 351 /* 352 * 3 is the first descriptor greater than STD*_FILENO. Any 353 * free descriptor valued 3 or above is acceptable... 354 */ 355 newfd = io->ops->op_fcntl(fd, F_DUPFD, 3); 356 if (newfd < 0) { 357 err(EXIT_FAILURE, "dupfd IO"); 358 /* NOTREACHED */ 359 } 360 361 io->ops->op_close(fd); 362 io->fd = newfd; 363 } 364 365 static void 366 dd_in(void) 367 { 368 int flags; 369 int64_t n; 370 int64_t r; 371 u_char *b; 372 373 for (flags = ddflags;;) { 374 if ((flags & C_COUNT) && (st.in_full + st.in_part) >= cpy_cnt) 375 return; 376 377 /* 378 * Clear the buffer first if doing "sync" on input. 379 * If doing block operations use spaces. This will 380 * affect not only the C_NOERROR case, but also the 381 * last partial input block which should be padded 382 * with zero and not garbage. 383 */ 384 if (flags & C_SYNC) { 385 if (flags & (C_BLOCK|C_UNBLOCK)) 386 (void)memset(in.dbp, ' ', in.dbsz); 387 else 388 (void)memset(in.dbp, 0, in.dbsz); 389 } 390 391 n = 0; b = in.dbp; 392 do { 393 r = ddop_read(in, in.fd, b, in.dbsz - n); 394 if (r == 0) { 395 if (n != 0) 396 break; 397 in.dbrcnt = 0; 398 return; 399 } 400 if (r < 0) { 401 int64_t t = n; 402 403 n = r; 404 r = t; /* save the amount that was read */ 405 break; 406 } 407 n += r; 408 b += r; 409 } while ((uint64_t)n < in.dbsz && in.flags & FULLBLOCK); 410 411 /* Read error. */ 412 if (n < 0) { 413 414 /* 415 * If noerror not specified, die. POSIX requires that 416 * the warning message be followed by an I/O display. 417 */ 418 if (!(flags & C_NOERROR)) { 419 err(EXIT_FAILURE, "%s", in.name); 420 /* NOTREACHED */ 421 } 422 warn("%s", in.name); 423 summary(); 424 425 /* 426 * If it's not a tape drive or a pipe, seek past the 427 * error. If your OS doesn't do the right thing for 428 * raw disks this section should be modified to re-read 429 * in sector size chunks. 430 * 431 * ('r' specifies how much was successfully read). 432 */ 433 if (!(in.flags & (ISPIPE|ISTAPE)) && 434 ddop_lseek(in, in.fd, (off_t)(in.dbsz-r), SEEK_CUR)) 435 warn("%s", in.name); 436 437 /* If sync not specified, omit block and continue. */ 438 if (!(ddflags & C_SYNC)) 439 continue; 440 441 /* Read errors count as full blocks. */ 442 in.dbcnt += in.dbrcnt = in.dbsz; 443 ++st.in_full; 444 445 /* Handle full input blocks. */ 446 } else if ((uint64_t)n == in.dbsz) { 447 in.dbcnt += in.dbrcnt = n; 448 ++st.in_full; 449 450 /* Handle partial input blocks. */ 451 } else { 452 /* If sync, use the entire block. */ 453 if (ddflags & C_SYNC) 454 in.dbcnt += in.dbrcnt = in.dbsz; 455 else 456 in.dbcnt += in.dbrcnt = n; 457 ++st.in_part; 458 } 459 460 /* 461 * POSIX states that if bs is set and no other conversions 462 * than noerror, notrunc or sync are specified, the block 463 * is output without buffering as it is read. 464 */ 465 if (ddflags & C_BS) { 466 out.dbcnt = in.dbcnt; 467 dd_out(1); 468 in.dbcnt = 0; 469 continue; 470 } 471 472 if (ddflags & C_SWAB) { 473 if ((n = in.dbrcnt) & 1) { 474 ++st.swab; 475 --n; 476 } 477 dd_swab(in.dbp, in.dbp, n); 478 } 479 480 in.dbp += in.dbrcnt; 481 (*cfunc)(); 482 } 483 } 484 485 /* 486 * Cleanup any remaining I/O and flush output. If necessary, output file 487 * is truncated. 488 */ 489 static void 490 dd_close(void) 491 { 492 493 if (cfunc == def) 494 def_close(); 495 else if (cfunc == block) 496 block_close(); 497 else if (cfunc == unblock) 498 unblock_close(); 499 if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) { 500 (void)memset(out.dbp, 0, out.dbsz - out.dbcnt); 501 out.dbcnt = out.dbsz; 502 } 503 /* If there are pending sparse blocks, make sure 504 * to write out the final block un-sparse 505 */ 506 if ((out.dbcnt == 0) && pending) { 507 memset(out.db, 0, out.dbsz); 508 out.dbcnt = out.dbsz; 509 out.dbp = out.db + out.dbcnt; 510 pending -= out.dbsz; 511 } 512 if (out.dbcnt) 513 dd_out(1); 514 515 /* 516 * Reporting nfs write error may be deferred until next 517 * write(2) or close(2) system call. So, we need to do an 518 * extra check. If an output is stdout, the file structure 519 * may be shared with other processes and close(2) just 520 * decreases the reference count. 521 */ 522 if (out.fd == STDOUT_FILENO && ddop_fsync(out, out.fd) == -1 523 && errno != EINVAL) { 524 err(EXIT_FAILURE, "fsync stdout"); 525 /* NOTREACHED */ 526 } 527 if (ddop_close(out, out.fd) == -1) { 528 err(EXIT_FAILURE, "close"); 529 /* NOTREACHED */ 530 } 531 } 532 533 void 534 dd_out(int force) 535 { 536 static int warned; 537 int64_t cnt, n, nw; 538 u_char *outp; 539 540 /* 541 * Write one or more blocks out. The common case is writing a full 542 * output block in a single write; increment the full block stats. 543 * Otherwise, we're into partial block writes. If a partial write, 544 * and it's a character device, just warn. If a tape device, quit. 545 * 546 * The partial writes represent two cases. 1: Where the input block 547 * was less than expected so the output block was less than expected. 548 * 2: Where the input block was the right size but we were forced to 549 * write the block in multiple chunks. The original versions of dd(1) 550 * never wrote a block in more than a single write, so the latter case 551 * never happened. 552 * 553 * One special case is if we're forced to do the write -- in that case 554 * we play games with the buffer size, and it's usually a partial write. 555 */ 556 outp = out.db; 557 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { 558 for (cnt = n;; cnt -= nw) { 559 560 if (!force && ddflags & C_SPARSE) { 561 int sparse, i; 562 sparse = 1; /* Is buffer sparse? */ 563 for (i = 0; i < cnt; i++) 564 if (outp[i] != 0) { 565 sparse = 0; 566 break; 567 } 568 if (sparse) { 569 pending += cnt; 570 outp += cnt; 571 nw = 0; 572 break; 573 } 574 } 575 if (pending != 0) { 576 if (ddop_lseek(out, 577 out.fd, pending, SEEK_CUR) == -1) 578 err(EXIT_FAILURE, "%s: seek error creating sparse file", 579 out.name); 580 } 581 nw = bwrite(&out, outp, cnt); 582 if (nw <= 0) { 583 if (nw == 0) 584 errx(EXIT_FAILURE, 585 "%s: end of device", out.name); 586 /* NOTREACHED */ 587 if (errno != EINTR) 588 err(EXIT_FAILURE, "%s", out.name); 589 /* NOTREACHED */ 590 nw = 0; 591 } 592 if (pending) { 593 st.bytes += pending; 594 st.sparse += pending/out.dbsz; 595 st.out_full += pending/out.dbsz; 596 pending = 0; 597 } 598 outp += nw; 599 st.bytes += nw; 600 if (nw == n) { 601 if ((uint64_t)n != out.dbsz) 602 ++st.out_part; 603 else 604 ++st.out_full; 605 break; 606 } 607 ++st.out_part; 608 if (nw == cnt) 609 break; 610 if (out.flags & ISCHR && !warned) { 611 warned = 1; 612 warnx("%s: short write on character device", out.name); 613 } 614 if (out.flags & ISTAPE) 615 errx(EXIT_FAILURE, 616 "%s: short write on tape device", out.name); 617 /* NOTREACHED */ 618 619 } 620 if ((out.dbcnt -= n) < out.dbsz) 621 break; 622 } 623 624 /* Reassemble the output block. */ 625 if (out.dbcnt) 626 (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt); 627 out.dbp = out.db + out.dbcnt; 628 629 if (progress && (st.out_full + st.out_part) % progress == 0) 630 (void)write(STDERR_FILENO, ".", 1); 631 } 632 633 /* 634 * A protected against SIGINFO write 635 */ 636 ssize_t 637 bwrite(IO *io, const void *buf, size_t len) 638 { 639 sigset_t oset; 640 ssize_t rv; 641 int oerrno; 642 643 (void)sigprocmask(SIG_BLOCK, &infoset, &oset); 644 rv = io->ops->op_write(io->fd, buf, len); 645 oerrno = errno; 646 (void)sigprocmask(SIG_SETMASK, &oset, NULL); 647 errno = oerrno; 648 return (rv); 649 } 650