The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* $NetBSD: dd.c,v 1.37 2004/01/17 21:00:16 dbj 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\n\ |
| 39 | The Regents of the University of California. All rights reserved.\n"); |
| 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.37 2004/01/17 21:00:16 dbj 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/time.h> |
| 54 | |
| 55 | #include <ctype.h> |
| 56 | #include <err.h> |
| 57 | #include <errno.h> |
| 58 | #include <fcntl.h> |
| 59 | #include <signal.h> |
| 60 | #include <stdio.h> |
| 61 | #include <stdlib.h> |
| 62 | #include <string.h> |
| 63 | #include <unistd.h> |
| 64 | |
| 65 | #include "dd.h" |
| 66 | |
Jeff Sharkey | 393e559 | 2012-05-29 14:25:04 -0700 | [diff] [blame] | 67 | //#define NO_CONV |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | |
| 69 | //#include "extern.h" |
| 70 | void block(void); |
| 71 | void block_close(void); |
| 72 | void dd_out(int); |
| 73 | void def(void); |
| 74 | void def_close(void); |
| 75 | void jcl(char **); |
| 76 | void pos_in(void); |
| 77 | void pos_out(void); |
| 78 | void summary(void); |
| 79 | void summaryx(int); |
| 80 | void terminate(int); |
| 81 | void unblock(void); |
| 82 | void unblock_close(void); |
| 83 | ssize_t bwrite(int, const void *, size_t); |
| 84 | |
| 85 | extern IO in, out; |
| 86 | extern STAT st; |
| 87 | extern void (*cfunc)(void); |
| 88 | extern uint64_t cpy_cnt; |
| 89 | extern uint64_t cbsz; |
| 90 | extern u_int ddflags; |
| 91 | extern u_int files_cnt; |
| 92 | extern int progress; |
| 93 | extern const u_char *ctab; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | |
Nick Kralevich | 3f2b0a5 | 2012-06-26 14:59:36 -0700 | [diff] [blame] | 95 | #define DEFFILEMODE (S_IRUSR | S_IWUSR) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 | |
| 97 | static void dd_close(void); |
| 98 | static void dd_in(void); |
| 99 | static void getfdtype(IO *); |
| 100 | static int redup_clean_fd(int); |
| 101 | static void setup(void); |
| 102 | |
| 103 | |
| 104 | IO in, out; /* input/output state */ |
| 105 | STAT st; /* statistics */ |
| 106 | void (*cfunc)(void); /* conversion function */ |
| 107 | uint64_t cpy_cnt; /* # of blocks to copy */ |
| 108 | static off_t pending = 0; /* pending seek if sparse */ |
| 109 | u_int ddflags; /* conversion options */ |
| 110 | uint64_t cbsz; /* conversion block size */ |
| 111 | u_int files_cnt = 1; /* # of files to copy */ |
| 112 | int progress = 0; /* display sign of life */ |
| 113 | const u_char *ctab; /* conversion table */ |
| 114 | sigset_t infoset; /* a set blocking SIGINFO */ |
| 115 | |
| 116 | int |
| 117 | dd_main(int argc, char *argv[]) |
| 118 | { |
| 119 | int ch; |
| 120 | |
| 121 | while ((ch = getopt(argc, argv, "")) != -1) { |
| 122 | switch (ch) { |
| 123 | default: |
| 124 | fprintf(stderr, "usage: dd [operand ...]\n"); |
| 125 | exit(1); |
| 126 | /* NOTREACHED */ |
| 127 | } |
| 128 | } |
| 129 | argc -= (optind - 1); |
| 130 | argv += (optind - 1); |
| 131 | |
| 132 | jcl(argv); |
| 133 | setup(); |
| 134 | |
| 135 | // (void)signal(SIGINFO, summaryx); |
| 136 | (void)signal(SIGINT, terminate); |
| 137 | (void)sigemptyset(&infoset); |
| 138 | // (void)sigaddset(&infoset, SIGINFO); |
| 139 | |
| 140 | (void)atexit(summary); |
| 141 | |
| 142 | while (files_cnt--) |
| 143 | dd_in(); |
| 144 | |
| 145 | dd_close(); |
| 146 | exit(0); |
| 147 | /* NOTREACHED */ |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | setup(void) |
| 152 | { |
| 153 | |
| 154 | if (in.name == NULL) { |
| 155 | in.name = "stdin"; |
| 156 | in.fd = STDIN_FILENO; |
| 157 | } else { |
| 158 | in.fd = open(in.name, O_RDONLY, 0); |
| 159 | if (in.fd < 0) { |
| 160 | fprintf(stderr, "%s: cannot open for read: %s\n", |
| 161 | in.name, strerror(errno)); |
| 162 | exit(1); |
| 163 | /* NOTREACHED */ |
| 164 | } |
| 165 | |
| 166 | /* Ensure in.fd is outside the stdio descriptor range */ |
| 167 | in.fd = redup_clean_fd(in.fd); |
| 168 | } |
| 169 | |
| 170 | getfdtype(&in); |
| 171 | |
| 172 | if (files_cnt > 1 && !(in.flags & ISTAPE)) { |
| 173 | fprintf(stderr, |
| 174 | "files is not supported for non-tape devices\n"); |
| 175 | exit(1); |
| 176 | /* NOTREACHED */ |
| 177 | } |
| 178 | |
| 179 | if (out.name == NULL) { |
| 180 | /* No way to check for read access here. */ |
| 181 | out.fd = STDOUT_FILENO; |
| 182 | out.name = "stdout"; |
| 183 | } else { |
| 184 | #define OFLAGS \ |
| 185 | (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) |
Nick Kralevich | 3f2b0a5 | 2012-06-26 14:59:36 -0700 | [diff] [blame] | 186 | out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | /* |
| 188 | * May not have read access, so try again with write only. |
| 189 | * Without read we may have a problem if output also does |
| 190 | * not support seeks. |
| 191 | */ |
| 192 | if (out.fd < 0) { |
Nick Kralevich | 3f2b0a5 | 2012-06-26 14:59:36 -0700 | [diff] [blame] | 193 | out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | out.flags |= NOREAD; |
| 195 | } |
| 196 | if (out.fd < 0) { |
| 197 | fprintf(stderr, "%s: cannot open for write: %s\n", |
| 198 | out.name, strerror(errno)); |
| 199 | exit(1); |
| 200 | /* NOTREACHED */ |
| 201 | } |
| 202 | |
| 203 | /* Ensure out.fd is outside the stdio descriptor range */ |
| 204 | out.fd = redup_clean_fd(out.fd); |
| 205 | } |
| 206 | |
| 207 | getfdtype(&out); |
| 208 | |
| 209 | /* |
| 210 | * Allocate space for the input and output buffers. If not doing |
| 211 | * record oriented I/O, only need a single buffer. |
| 212 | */ |
| 213 | if (!(ddflags & (C_BLOCK|C_UNBLOCK))) { |
| 214 | if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) { |
| 215 | exit(1); |
| 216 | /* NOTREACHED */ |
| 217 | } |
| 218 | out.db = in.db; |
| 219 | } else if ((in.db = |
| 220 | malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL || |
| 221 | (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) { |
| 222 | exit(1); |
| 223 | /* NOTREACHED */ |
| 224 | } |
| 225 | in.dbp = in.db; |
| 226 | out.dbp = out.db; |
| 227 | |
| 228 | /* Position the input/output streams. */ |
| 229 | if (in.offset) |
| 230 | pos_in(); |
| 231 | if (out.offset) |
| 232 | pos_out(); |
| 233 | |
| 234 | /* |
| 235 | * Truncate the output file; ignore errors because it fails on some |
| 236 | * kinds of output files, tapes, for example. |
| 237 | */ |
| 238 | if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK)) |
| 239 | (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz); |
| 240 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */ |
| 242 | } |
| 243 | |
| 244 | static void |
| 245 | getfdtype(IO *io) |
| 246 | { |
| 247 | // struct mtget mt; |
| 248 | struct stat sb; |
| 249 | |
| 250 | if (fstat(io->fd, &sb)) { |
| 251 | fprintf(stderr, "%s: cannot fstat: %s\n", |
| 252 | io->name, strerror(errno)); |
| 253 | exit(1); |
| 254 | /* NOTREACHED */ |
| 255 | } |
| 256 | if (S_ISCHR(sb.st_mode)) |
| 257 | io->flags |= /*ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE; */ ISCHR; |
| 258 | else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) |
| 259 | io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */ |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Move the parameter file descriptor to a descriptor that is outside the |
| 264 | * stdio descriptor range, if necessary. This is required to avoid |
| 265 | * accidentally outputting completion or error messages into the |
| 266 | * output file that were intended for the tty. |
| 267 | */ |
| 268 | static int |
| 269 | redup_clean_fd(int fd) |
| 270 | { |
| 271 | int newfd; |
| 272 | |
| 273 | if (fd != STDIN_FILENO && fd != STDOUT_FILENO && |
| 274 | fd != STDERR_FILENO) |
| 275 | /* File descriptor is ok, return immediately. */ |
| 276 | return fd; |
| 277 | |
| 278 | /* |
| 279 | * 3 is the first descriptor greater than STD*_FILENO. Any |
| 280 | * free descriptor valued 3 or above is acceptable... |
| 281 | */ |
| 282 | newfd = fcntl(fd, F_DUPFD, 3); |
| 283 | if (newfd < 0) { |
| 284 | fprintf(stderr, "dupfd IO: %s\n", strerror(errno)); |
| 285 | exit(1); |
| 286 | /* NOTREACHED */ |
| 287 | } |
| 288 | |
| 289 | close(fd); |
| 290 | |
| 291 | return newfd; |
| 292 | } |
| 293 | |
| 294 | static void |
| 295 | dd_in(void) |
| 296 | { |
| 297 | int flags; |
| 298 | int64_t n; |
| 299 | |
| 300 | for (flags = ddflags;;) { |
| 301 | if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt) |
| 302 | return; |
| 303 | |
| 304 | /* |
| 305 | * Clear the buffer first if doing "sync" on input. |
| 306 | * If doing block operations use spaces. This will |
| 307 | * affect not only the C_NOERROR case, but also the |
| 308 | * last partial input block which should be padded |
| 309 | * with zero and not garbage. |
| 310 | */ |
| 311 | if (flags & C_SYNC) { |
| 312 | if (flags & (C_BLOCK|C_UNBLOCK)) |
| 313 | (void)memset(in.dbp, ' ', in.dbsz); |
| 314 | else |
| 315 | (void)memset(in.dbp, 0, in.dbsz); |
| 316 | } |
| 317 | |
| 318 | n = read(in.fd, in.dbp, in.dbsz); |
| 319 | if (n == 0) { |
| 320 | in.dbrcnt = 0; |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | /* Read error. */ |
| 325 | if (n < 0) { |
| 326 | |
| 327 | /* |
| 328 | * If noerror not specified, die. POSIX requires that |
| 329 | * the warning message be followed by an I/O display. |
| 330 | */ |
| 331 | fprintf(stderr, "%s: read error: %s\n", |
| 332 | in.name, strerror(errno)); |
| 333 | if (!(flags & C_NOERROR)) { |
| 334 | exit(1); |
| 335 | /* NOTREACHED */ |
| 336 | } |
| 337 | summary(); |
| 338 | |
| 339 | /* |
| 340 | * If it's not a tape drive or a pipe, seek past the |
| 341 | * error. If your OS doesn't do the right thing for |
| 342 | * raw disks this section should be modified to re-read |
| 343 | * in sector size chunks. |
| 344 | */ |
| 345 | if (!(in.flags & (ISPIPE|ISTAPE)) && |
| 346 | lseek(in.fd, (off_t)in.dbsz, SEEK_CUR)) |
| 347 | fprintf(stderr, "%s: seek error: %s\n", |
| 348 | in.name, strerror(errno)); |
| 349 | |
| 350 | /* If sync not specified, omit block and continue. */ |
| 351 | if (!(ddflags & C_SYNC)) |
| 352 | continue; |
| 353 | |
| 354 | /* Read errors count as full blocks. */ |
| 355 | in.dbcnt += in.dbrcnt = in.dbsz; |
| 356 | ++st.in_full; |
| 357 | |
| 358 | /* Handle full input blocks. */ |
| 359 | } else if (n == in.dbsz) { |
| 360 | in.dbcnt += in.dbrcnt = n; |
| 361 | ++st.in_full; |
| 362 | |
| 363 | /* Handle partial input blocks. */ |
| 364 | } else { |
| 365 | /* If sync, use the entire block. */ |
| 366 | if (ddflags & C_SYNC) |
| 367 | in.dbcnt += in.dbrcnt = in.dbsz; |
| 368 | else |
| 369 | in.dbcnt += in.dbrcnt = n; |
| 370 | ++st.in_part; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | * POSIX states that if bs is set and no other conversions |
| 375 | * than noerror, notrunc or sync are specified, the block |
| 376 | * is output without buffering as it is read. |
| 377 | */ |
| 378 | if (ddflags & C_BS) { |
| 379 | out.dbcnt = in.dbcnt; |
| 380 | dd_out(1); |
| 381 | in.dbcnt = 0; |
| 382 | continue; |
| 383 | } |
| 384 | |
| 385 | /* if (ddflags & C_SWAB) { |
| 386 | if ((n = in.dbrcnt) & 1) { |
| 387 | ++st.swab; |
| 388 | --n; |
| 389 | } |
| 390 | swab(in.dbp, in.dbp, n); |
| 391 | } |
| 392 | */ |
| 393 | in.dbp += in.dbrcnt; |
| 394 | (*cfunc)(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Cleanup any remaining I/O and flush output. If necesssary, output file |
| 400 | * is truncated. |
| 401 | */ |
| 402 | static void |
| 403 | dd_close(void) |
| 404 | { |
| 405 | |
| 406 | if (cfunc == def) |
| 407 | def_close(); |
| 408 | else if (cfunc == block) |
| 409 | block_close(); |
| 410 | else if (cfunc == unblock) |
| 411 | unblock_close(); |
| 412 | if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) { |
| 413 | (void)memset(out.dbp, 0, out.dbsz - out.dbcnt); |
| 414 | out.dbcnt = out.dbsz; |
| 415 | } |
| 416 | /* If there are pending sparse blocks, make sure |
| 417 | * to write out the final block un-sparse |
| 418 | */ |
| 419 | if ((out.dbcnt == 0) && pending) { |
| 420 | memset(out.db, 0, out.dbsz); |
| 421 | out.dbcnt = out.dbsz; |
| 422 | out.dbp = out.db + out.dbcnt; |
| 423 | pending -= out.dbsz; |
| 424 | } |
| 425 | if (out.dbcnt) |
| 426 | dd_out(1); |
| 427 | |
| 428 | /* |
| 429 | * Reporting nfs write error may be defered until next |
| 430 | * write(2) or close(2) system call. So, we need to do an |
| 431 | * extra check. If an output is stdout, the file structure |
| 432 | * may be shared among with other processes and close(2) just |
| 433 | * decreases the reference count. |
| 434 | */ |
| 435 | if (out.fd == STDOUT_FILENO && fsync(out.fd) == -1 && errno != EINVAL) { |
| 436 | fprintf(stderr, "fsync stdout: %s\n", strerror(errno)); |
| 437 | exit(1); |
| 438 | /* NOTREACHED */ |
| 439 | } |
| 440 | if (close(out.fd) == -1) { |
| 441 | fprintf(stderr, "close: %s\n", strerror(errno)); |
| 442 | exit(1); |
| 443 | /* NOTREACHED */ |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void |
| 448 | dd_out(int force) |
| 449 | { |
| 450 | static int warned; |
| 451 | int64_t cnt, n, nw; |
| 452 | u_char *outp; |
| 453 | |
| 454 | /* |
| 455 | * Write one or more blocks out. The common case is writing a full |
| 456 | * output block in a single write; increment the full block stats. |
| 457 | * Otherwise, we're into partial block writes. If a partial write, |
| 458 | * and it's a character device, just warn. If a tape device, quit. |
| 459 | * |
| 460 | * The partial writes represent two cases. 1: Where the input block |
| 461 | * was less than expected so the output block was less than expected. |
| 462 | * 2: Where the input block was the right size but we were forced to |
| 463 | * write the block in multiple chunks. The original versions of dd(1) |
| 464 | * never wrote a block in more than a single write, so the latter case |
| 465 | * never happened. |
| 466 | * |
| 467 | * One special case is if we're forced to do the write -- in that case |
| 468 | * we play games with the buffer size, and it's usually a partial write. |
| 469 | */ |
| 470 | outp = out.db; |
| 471 | for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { |
| 472 | for (cnt = n;; cnt -= nw) { |
| 473 | |
| 474 | if (!force && ddflags & C_SPARSE) { |
| 475 | int sparse, i; |
| 476 | sparse = 1; /* Is buffer sparse? */ |
| 477 | for (i = 0; i < cnt; i++) |
| 478 | if (outp[i] != 0) { |
| 479 | sparse = 0; |
| 480 | break; |
| 481 | } |
| 482 | if (sparse) { |
| 483 | pending += cnt; |
| 484 | outp += cnt; |
| 485 | nw = 0; |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | if (pending != 0) { |
| 490 | if (lseek(out.fd, pending, SEEK_CUR) == |
| 491 | -1) { |
| 492 | fprintf(stderr, |
| 493 | "%s: seek error creating " |
| 494 | "sparse file: %s\n", |
| 495 | out.name, strerror(errno)); |
| 496 | exit(1); |
| 497 | } |
| 498 | } |
| 499 | nw = bwrite(out.fd, outp, cnt); |
| 500 | if (nw <= 0) { |
| 501 | if (nw == 0) { |
| 502 | fprintf(stderr, "%s: end of device\n", |
| 503 | out.name); |
| 504 | exit(1); |
| 505 | /* NOTREACHED */ |
| 506 | } |
| 507 | if (errno != EINTR) { |
| 508 | fprintf(stderr, "%s: write error: %s\n", |
| 509 | out.name, strerror(errno)); |
| 510 | /* NOTREACHED */ |
| 511 | exit(1); |
| 512 | } |
| 513 | nw = 0; |
| 514 | } |
| 515 | if (pending) { |
| 516 | st.bytes += pending; |
| 517 | st.sparse += pending/out.dbsz; |
| 518 | st.out_full += pending/out.dbsz; |
| 519 | pending = 0; |
| 520 | } |
| 521 | outp += nw; |
| 522 | st.bytes += nw; |
| 523 | if (nw == n) { |
| 524 | if (n != out.dbsz) |
| 525 | ++st.out_part; |
| 526 | else |
| 527 | ++st.out_full; |
| 528 | break; |
| 529 | } |
| 530 | ++st.out_part; |
| 531 | if (nw == cnt) |
| 532 | break; |
| 533 | if (out.flags & ISCHR && !warned) { |
| 534 | warned = 1; |
| 535 | fprintf(stderr, "%s: short write on character " |
| 536 | "device\n", out.name); |
| 537 | } |
| 538 | if (out.flags & ISTAPE) { |
| 539 | fprintf(stderr, |
| 540 | "%s: short write on tape device", |
| 541 | out.name); |
| 542 | exit(1); |
| 543 | /* NOTREACHED */ |
| 544 | } |
| 545 | } |
| 546 | if ((out.dbcnt -= n) < out.dbsz) |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | /* Reassemble the output block. */ |
| 551 | if (out.dbcnt) |
| 552 | (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt); |
| 553 | out.dbp = out.db + out.dbcnt; |
| 554 | |
| 555 | if (progress) |
| 556 | (void)write(STDERR_FILENO, ".", 1); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * A protected against SIGINFO write |
| 561 | */ |
| 562 | ssize_t |
| 563 | bwrite(int fd, const void *buf, size_t len) |
| 564 | { |
| 565 | sigset_t oset; |
| 566 | ssize_t rv; |
| 567 | int oerrno; |
| 568 | |
| 569 | (void)sigprocmask(SIG_BLOCK, &infoset, &oset); |
| 570 | rv = write(fd, buf, len); |
| 571 | oerrno = errno; |
| 572 | (void)sigprocmask(SIG_SETMASK, &oset, NULL); |
| 573 | errno = oerrno; |
| 574 | return (rv); |
| 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Position input/output data streams before starting the copy. Device type |
| 579 | * dependent. Seekable devices use lseek, and the rest position by reading. |
| 580 | * Seeking past the end of file can cause null blocks to be written to the |
| 581 | * output. |
| 582 | */ |
| 583 | void |
| 584 | pos_in(void) |
| 585 | { |
| 586 | int bcnt, cnt, nr, warned; |
| 587 | |
| 588 | /* If not a pipe or tape device, try to seek on it. */ |
| 589 | if (!(in.flags & (ISPIPE|ISTAPE))) { |
Jongrak Kwon | c05aae4 | 2013-04-16 23:00:35 -0700 | [diff] [blame] | 590 | if (lseek64(in.fd, |
| 591 | (off64_t)in.offset * (off64_t)in.dbsz, SEEK_CUR) == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 592 | fprintf(stderr, "%s: seek error: %s", |
| 593 | in.name, strerror(errno)); |
| 594 | exit(1); |
| 595 | /* NOTREACHED */ |
| 596 | } |
| 597 | return; |
| 598 | /* NOTREACHED */ |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * Read the data. If a pipe, read until satisfy the number of bytes |
| 603 | * being skipped. No differentiation for reading complete and partial |
| 604 | * blocks for other devices. |
| 605 | */ |
| 606 | for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) { |
| 607 | if ((nr = read(in.fd, in.db, bcnt)) > 0) { |
| 608 | if (in.flags & ISPIPE) { |
| 609 | if (!(bcnt -= nr)) { |
| 610 | bcnt = in.dbsz; |
| 611 | --cnt; |
| 612 | } |
| 613 | } else |
| 614 | --cnt; |
| 615 | continue; |
| 616 | } |
| 617 | |
| 618 | if (nr == 0) { |
| 619 | if (files_cnt > 1) { |
| 620 | --files_cnt; |
| 621 | continue; |
| 622 | } |
| 623 | fprintf(stderr, "skip reached end of input\n"); |
| 624 | exit(1); |
| 625 | /* NOTREACHED */ |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Input error -- either EOF with no more files, or I/O error. |
| 630 | * If noerror not set die. POSIX requires that the warning |
| 631 | * message be followed by an I/O display. |
| 632 | */ |
| 633 | if (ddflags & C_NOERROR) { |
| 634 | if (!warned) { |
| 635 | |
| 636 | fprintf(stderr, "%s: error occurred\n", |
| 637 | in.name); |
| 638 | warned = 1; |
| 639 | summary(); |
| 640 | } |
| 641 | continue; |
| 642 | } |
| 643 | fprintf(stderr, "%s: read error: %s", in.name, strerror(errno)); |
| 644 | exit(1); |
| 645 | /* NOTREACHED */ |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | void |
| 650 | pos_out(void) |
| 651 | { |
| 652 | // struct mtop t_op; |
| 653 | int cnt, n; |
| 654 | |
| 655 | /* |
| 656 | * If not a tape, try seeking on the file. Seeking on a pipe is |
| 657 | * going to fail, but don't protect the user -- they shouldn't |
| 658 | * have specified the seek operand. |
| 659 | */ |
| 660 | if (!(out.flags & ISTAPE)) { |
Jongrak Kwon | c05aae4 | 2013-04-16 23:00:35 -0700 | [diff] [blame] | 661 | if (lseek64(out.fd, |
| 662 | (off64_t)out.offset * (off64_t)out.dbsz, SEEK_SET) == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 663 | fprintf(stderr, "%s: seek error: %s\n", |
| 664 | out.name, strerror(errno)); |
| 665 | exit(1); |
| 666 | /* NOTREACHED */ |
| 667 | } |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | /* If no read access, try using mtio. */ |
| 672 | if (out.flags & NOREAD) { |
| 673 | /* t_op.mt_op = MTFSR; |
| 674 | t_op.mt_count = out.offset; |
| 675 | |
| 676 | if (ioctl(out.fd, MTIOCTOP, &t_op) < 0)*/ |
| 677 | fprintf(stderr, "%s: cannot read", out.name); |
| 678 | exit(1); |
| 679 | /* NOTREACHED */ |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | /* Read it. */ |
| 684 | for (cnt = 0; cnt < out.offset; ++cnt) { |
| 685 | if ((n = read(out.fd, out.db, out.dbsz)) > 0) |
| 686 | continue; |
| 687 | |
| 688 | if (n < 0) { |
| 689 | fprintf(stderr, "%s: cannot position by reading: %s\n", |
| 690 | out.name, strerror(errno)); |
| 691 | exit(1); |
| 692 | /* NOTREACHED */ |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * If reach EOF, fill with NUL characters; first, back up over |
| 697 | * the EOF mark. Note, cnt has not yet been incremented, so |
| 698 | * the EOF read does not count as a seek'd block. |
| 699 | */ |
| 700 | /* t_op.mt_op = MTBSR; |
| 701 | t_op.mt_count = 1; |
| 702 | if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) */ { |
| 703 | fprintf(stderr, "%s: cannot position\n", out.name); |
| 704 | exit(1); |
| 705 | /* NOTREACHED */ |
| 706 | } |
| 707 | |
| 708 | while (cnt++ < out.offset) |
| 709 | if ((n = bwrite(out.fd, out.db, out.dbsz)) != out.dbsz) { |
| 710 | fprintf(stderr, "%s: cannot position " |
| 711 | "by writing: %s\n", |
| 712 | out.name, strerror(errno)); |
| 713 | exit(1); |
| 714 | /* NOTREACHED */ |
| 715 | } |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * def -- |
| 722 | * Copy input to output. Input is buffered until reaches obs, and then |
| 723 | * output until less than obs remains. Only a single buffer is used. |
| 724 | * Worst case buffer calculation is (ibs + obs - 1). |
| 725 | */ |
| 726 | void |
| 727 | def(void) |
| 728 | { |
| 729 | uint64_t cnt; |
| 730 | u_char *inp; |
| 731 | const u_char *t; |
| 732 | |
| 733 | if ((t = ctab) != NULL) |
| 734 | for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp) |
| 735 | *inp = t[*inp]; |
| 736 | |
| 737 | /* Make the output buffer look right. */ |
| 738 | out.dbp = in.dbp; |
| 739 | out.dbcnt = in.dbcnt; |
| 740 | |
| 741 | if (in.dbcnt >= out.dbsz) { |
| 742 | /* If the output buffer is full, write it. */ |
| 743 | dd_out(0); |
| 744 | |
| 745 | /* |
| 746 | * Ddout copies the leftover output to the beginning of |
| 747 | * the buffer and resets the output buffer. Reset the |
| 748 | * input buffer to match it. |
| 749 | */ |
| 750 | in.dbp = out.dbp; |
| 751 | in.dbcnt = out.dbcnt; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | void |
| 756 | def_close(void) |
| 757 | { |
Jeff Sharkey | 393e559 | 2012-05-29 14:25:04 -0700 | [diff] [blame] | 758 | if (ddflags & C_FDATASYNC) { |
| 759 | fdatasync(out.fd); |
| 760 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 761 | |
| 762 | /* Just update the count, everything is already in the buffer. */ |
| 763 | if (in.dbcnt) |
| 764 | out.dbcnt = in.dbcnt; |
| 765 | } |
| 766 | |
| 767 | #ifdef NO_CONV |
| 768 | /* Build a smaller version (i.e. for a miniroot) */ |
| 769 | /* These can not be called, but just in case... */ |
| 770 | static const char no_block[] = "unblock and -DNO_CONV?\n"; |
| 771 | void block(void) { fprintf(stderr, "%s", no_block + 2); exit(1); } |
| 772 | void block_close(void) { fprintf(stderr, "%s", no_block + 2); exit(1); } |
| 773 | void unblock(void) { fprintf(stderr, "%s", no_block); exit(1); } |
| 774 | void unblock_close(void) { fprintf(stderr, "%s", no_block); exit(1); } |
| 775 | #else /* NO_CONV */ |
| 776 | |
| 777 | /* |
| 778 | * Copy variable length newline terminated records with a max size cbsz |
| 779 | * bytes to output. Records less than cbs are padded with spaces. |
| 780 | * |
| 781 | * max in buffer: MAX(ibs, cbsz) |
| 782 | * max out buffer: obs + cbsz |
| 783 | */ |
| 784 | void |
| 785 | block(void) |
| 786 | { |
| 787 | static int intrunc; |
| 788 | int ch = 0; /* pacify gcc */ |
| 789 | uint64_t cnt, maxlen; |
| 790 | u_char *inp, *outp; |
| 791 | const u_char *t; |
| 792 | |
| 793 | /* |
| 794 | * Record truncation can cross block boundaries. If currently in a |
| 795 | * truncation state, keep tossing characters until reach a newline. |
| 796 | * Start at the beginning of the buffer, as the input buffer is always |
| 797 | * left empty. |
| 798 | */ |
| 799 | if (intrunc) { |
| 800 | for (inp = in.db, cnt = in.dbrcnt; |
| 801 | cnt && *inp++ != '\n'; --cnt); |
| 802 | if (!cnt) { |
| 803 | in.dbcnt = 0; |
| 804 | in.dbp = in.db; |
| 805 | return; |
| 806 | } |
| 807 | intrunc = 0; |
| 808 | /* Adjust the input buffer numbers. */ |
| 809 | in.dbcnt = cnt - 1; |
| 810 | in.dbp = inp + cnt - 1; |
| 811 | } |
| 812 | |
| 813 | /* |
| 814 | * Copy records (max cbsz size chunks) into the output buffer. The |
| 815 | * translation is done as we copy into the output buffer. |
| 816 | */ |
| 817 | for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) { |
| 818 | maxlen = MIN(cbsz, in.dbcnt); |
| 819 | if ((t = ctab) != NULL) |
| 820 | for (cnt = 0; |
| 821 | cnt < maxlen && (ch = *inp++) != '\n'; ++cnt) |
| 822 | *outp++ = t[ch]; |
| 823 | else |
| 824 | for (cnt = 0; |
| 825 | cnt < maxlen && (ch = *inp++) != '\n'; ++cnt) |
| 826 | *outp++ = ch; |
| 827 | /* |
| 828 | * Check for short record without a newline. Reassemble the |
| 829 | * input block. |
| 830 | */ |
| 831 | if (ch != '\n' && in.dbcnt < cbsz) { |
| 832 | (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt); |
| 833 | break; |
| 834 | } |
| 835 | |
| 836 | /* Adjust the input buffer numbers. */ |
| 837 | in.dbcnt -= cnt; |
| 838 | if (ch == '\n') |
| 839 | --in.dbcnt; |
| 840 | |
| 841 | /* Pad short records with spaces. */ |
| 842 | if (cnt < cbsz) |
| 843 | (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt); |
| 844 | else { |
| 845 | /* |
| 846 | * If the next character wouldn't have ended the |
| 847 | * block, it's a truncation. |
| 848 | */ |
| 849 | if (!in.dbcnt || *inp != '\n') |
| 850 | ++st.trunc; |
| 851 | |
| 852 | /* Toss characters to a newline. */ |
| 853 | for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt); |
| 854 | if (!in.dbcnt) |
| 855 | intrunc = 1; |
| 856 | else |
| 857 | --in.dbcnt; |
| 858 | } |
| 859 | |
| 860 | /* Adjust output buffer numbers. */ |
| 861 | out.dbp += cbsz; |
| 862 | if ((out.dbcnt += cbsz) >= out.dbsz) |
| 863 | dd_out(0); |
| 864 | outp = out.dbp; |
| 865 | } |
| 866 | in.dbp = in.db + in.dbcnt; |
| 867 | } |
| 868 | |
| 869 | void |
| 870 | block_close(void) |
| 871 | { |
| 872 | |
| 873 | /* |
| 874 | * Copy any remaining data into the output buffer and pad to a record. |
| 875 | * Don't worry about truncation or translation, the input buffer is |
| 876 | * always empty when truncating, and no characters have been added for |
| 877 | * translation. The bottom line is that anything left in the input |
| 878 | * buffer is a truncated record. Anything left in the output buffer |
| 879 | * just wasn't big enough. |
| 880 | */ |
| 881 | if (in.dbcnt) { |
| 882 | ++st.trunc; |
| 883 | (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt); |
| 884 | (void)memset(out.dbp + in.dbcnt, |
| 885 | ctab ? ctab[' '] : ' ', cbsz - in.dbcnt); |
| 886 | out.dbcnt += cbsz; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Convert fixed length (cbsz) records to variable length. Deletes any |
| 892 | * trailing blanks and appends a newline. |
| 893 | * |
| 894 | * max in buffer: MAX(ibs, cbsz) + cbsz |
| 895 | * max out buffer: obs + cbsz |
| 896 | */ |
| 897 | void |
| 898 | unblock(void) |
| 899 | { |
| 900 | uint64_t cnt; |
| 901 | u_char *inp; |
| 902 | const u_char *t; |
| 903 | |
| 904 | /* Translation and case conversion. */ |
| 905 | if ((t = ctab) != NULL) |
| 906 | for (cnt = in.dbrcnt, inp = in.dbp - 1; cnt--; inp--) |
| 907 | *inp = t[*inp]; |
| 908 | /* |
| 909 | * Copy records (max cbsz size chunks) into the output buffer. The |
| 910 | * translation has to already be done or we might not recognize the |
| 911 | * spaces. |
| 912 | */ |
| 913 | for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) { |
| 914 | for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t); |
| 915 | if (t >= inp) { |
| 916 | cnt = t - inp + 1; |
| 917 | (void)memmove(out.dbp, inp, cnt); |
| 918 | out.dbp += cnt; |
| 919 | out.dbcnt += cnt; |
| 920 | } |
| 921 | ++out.dbcnt; |
| 922 | *out.dbp++ = '\n'; |
| 923 | if (out.dbcnt >= out.dbsz) |
| 924 | dd_out(0); |
| 925 | } |
| 926 | if (in.dbcnt) |
| 927 | (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt); |
| 928 | in.dbp = in.db + in.dbcnt; |
| 929 | } |
| 930 | |
| 931 | void |
| 932 | unblock_close(void) |
| 933 | { |
| 934 | uint64_t cnt; |
| 935 | u_char *t; |
| 936 | |
| 937 | if (in.dbcnt) { |
| 938 | warnx("%s: short input record", in.name); |
| 939 | for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t); |
| 940 | if (t >= in.db) { |
| 941 | cnt = t - in.db + 1; |
| 942 | (void)memmove(out.dbp, in.db, cnt); |
| 943 | out.dbp += cnt; |
| 944 | out.dbcnt += cnt; |
| 945 | } |
| 946 | ++out.dbcnt; |
| 947 | *out.dbp++ = '\n'; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | #endif /* NO_CONV */ |
| 952 | |
| 953 | #define tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000) |
| 954 | |
| 955 | void |
| 956 | summary(void) |
| 957 | { |
| 958 | char buf[100]; |
| 959 | int64_t mS; |
| 960 | struct timeval tv; |
| 961 | |
| 962 | if (progress) |
| 963 | (void)write(STDERR_FILENO, "\n", 1); |
| 964 | |
| 965 | (void)gettimeofday(&tv, NULL); |
| 966 | mS = tv2mS(tv) - tv2mS(st.start); |
| 967 | if (mS == 0) |
| 968 | mS = 1; |
| 969 | /* Use snprintf(3) so that we don't reenter stdio(3). */ |
| 970 | (void)snprintf(buf, sizeof(buf), |
| 971 | "%llu+%llu records in\n%llu+%llu records out\n", |
| 972 | (unsigned long long)st.in_full, (unsigned long long)st.in_part, |
| 973 | (unsigned long long)st.out_full, (unsigned long long)st.out_part); |
| 974 | (void)write(STDERR_FILENO, buf, strlen(buf)); |
| 975 | if (st.swab) { |
| 976 | (void)snprintf(buf, sizeof(buf), "%llu odd length swab %s\n", |
| 977 | (unsigned long long)st.swab, |
| 978 | (st.swab == 1) ? "block" : "blocks"); |
| 979 | (void)write(STDERR_FILENO, buf, strlen(buf)); |
| 980 | } |
| 981 | if (st.trunc) { |
| 982 | (void)snprintf(buf, sizeof(buf), "%llu truncated %s\n", |
| 983 | (unsigned long long)st.trunc, |
| 984 | (st.trunc == 1) ? "block" : "blocks"); |
| 985 | (void)write(STDERR_FILENO, buf, strlen(buf)); |
| 986 | } |
| 987 | if (st.sparse) { |
| 988 | (void)snprintf(buf, sizeof(buf), "%llu sparse output %s\n", |
| 989 | (unsigned long long)st.sparse, |
| 990 | (st.sparse == 1) ? "block" : "blocks"); |
| 991 | (void)write(STDERR_FILENO, buf, strlen(buf)); |
| 992 | } |
| 993 | (void)snprintf(buf, sizeof(buf), |
| 994 | "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n", |
| 995 | (unsigned long long) st.bytes, |
| 996 | (long) (mS / 1000), |
| 997 | (int) (mS % 1000), |
| 998 | (unsigned long long) (st.bytes * 1000LL / mS)); |
| 999 | (void)write(STDERR_FILENO, buf, strlen(buf)); |
| 1000 | } |
| 1001 | |
| 1002 | void |
| 1003 | terminate(int notused) |
| 1004 | { |
| 1005 | |
| 1006 | exit(0); |
| 1007 | /* NOTREACHED */ |
| 1008 | } |
| 1009 | |
| 1010 | static int c_arg(const void *, const void *); |
| 1011 | #ifndef NO_CONV |
| 1012 | static int c_conv(const void *, const void *); |
| 1013 | #endif |
| 1014 | static void f_bs(char *); |
| 1015 | static void f_cbs(char *); |
| 1016 | static void f_conv(char *); |
| 1017 | static void f_count(char *); |
| 1018 | static void f_files(char *); |
| 1019 | static void f_ibs(char *); |
| 1020 | static void f_if(char *); |
| 1021 | static void f_obs(char *); |
| 1022 | static void f_of(char *); |
| 1023 | static void f_seek(char *); |
| 1024 | static void f_skip(char *); |
| 1025 | static void f_progress(char *); |
| 1026 | |
| 1027 | static const struct arg { |
| 1028 | const char *name; |
| 1029 | void (*f)(char *); |
| 1030 | u_int set, noset; |
| 1031 | } args[] = { |
| 1032 | /* the array needs to be sorted by the first column so |
| 1033 | bsearch() can be used to find commands quickly */ |
| 1034 | { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC }, |
| 1035 | { "cbs", f_cbs, C_CBS, C_CBS }, |
| 1036 | { "conv", f_conv, 0, 0 }, |
| 1037 | { "count", f_count, C_COUNT, C_COUNT }, |
| 1038 | { "files", f_files, C_FILES, C_FILES }, |
| 1039 | { "ibs", f_ibs, C_IBS, C_BS|C_IBS }, |
| 1040 | { "if", f_if, C_IF, C_IF }, |
| 1041 | { "obs", f_obs, C_OBS, C_BS|C_OBS }, |
| 1042 | { "of", f_of, C_OF, C_OF }, |
| 1043 | { "progress", f_progress, 0, 0 }, |
| 1044 | { "seek", f_seek, C_SEEK, C_SEEK }, |
| 1045 | { "skip", f_skip, C_SKIP, C_SKIP }, |
| 1046 | }; |
| 1047 | |
| 1048 | /* |
| 1049 | * args -- parse JCL syntax of dd. |
| 1050 | */ |
| 1051 | void |
| 1052 | jcl(char **argv) |
| 1053 | { |
| 1054 | struct arg *ap, tmp; |
| 1055 | char *oper, *arg; |
| 1056 | |
| 1057 | in.dbsz = out.dbsz = 512; |
| 1058 | |
| 1059 | while ((oper = *++argv) != NULL) { |
| 1060 | if ((arg = strchr(oper, '=')) == NULL) { |
| 1061 | fprintf(stderr, "unknown operand %s\n", oper); |
| 1062 | exit(1); |
| 1063 | /* NOTREACHED */ |
| 1064 | } |
| 1065 | *arg++ = '\0'; |
| 1066 | if (!*arg) { |
| 1067 | fprintf(stderr, "no value specified for %s\n", oper); |
| 1068 | exit(1); |
| 1069 | /* NOTREACHED */ |
| 1070 | } |
| 1071 | tmp.name = oper; |
| 1072 | if (!(ap = (struct arg *)bsearch(&tmp, args, |
| 1073 | sizeof(args)/sizeof(struct arg), sizeof(struct arg), |
| 1074 | c_arg))) { |
| 1075 | fprintf(stderr, "unknown operand %s\n", tmp.name); |
| 1076 | exit(1); |
| 1077 | /* NOTREACHED */ |
| 1078 | } |
| 1079 | if (ddflags & ap->noset) { |
| 1080 | fprintf(stderr, |
| 1081 | "%s: illegal argument combination or already set\n", |
| 1082 | tmp.name); |
| 1083 | exit(1); |
| 1084 | /* NOTREACHED */ |
| 1085 | } |
| 1086 | ddflags |= ap->set; |
| 1087 | ap->f(arg); |
| 1088 | } |
| 1089 | |
| 1090 | /* Final sanity checks. */ |
| 1091 | |
| 1092 | if (ddflags & C_BS) { |
| 1093 | /* |
| 1094 | * Bs is turned off by any conversion -- we assume the user |
| 1095 | * just wanted to set both the input and output block sizes |
| 1096 | * and didn't want the bs semantics, so we don't warn. |
| 1097 | */ |
| 1098 | if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE | |
| 1099 | C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) { |
| 1100 | ddflags &= ~C_BS; |
| 1101 | ddflags |= C_IBS|C_OBS; |
| 1102 | } |
| 1103 | |
| 1104 | /* Bs supersedes ibs and obs. */ |
| 1105 | if (ddflags & C_BS && ddflags & (C_IBS|C_OBS)) |
| 1106 | fprintf(stderr, "bs supersedes ibs and obs\n"); |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * Ascii/ebcdic and cbs implies block/unblock. |
| 1111 | * Block/unblock requires cbs and vice-versa. |
| 1112 | */ |
| 1113 | if (ddflags & (C_BLOCK|C_UNBLOCK)) { |
| 1114 | if (!(ddflags & C_CBS)) { |
| 1115 | fprintf(stderr, "record operations require cbs\n"); |
| 1116 | exit(1); |
| 1117 | /* NOTREACHED */ |
| 1118 | } |
| 1119 | cfunc = ddflags & C_BLOCK ? block : unblock; |
| 1120 | } else if (ddflags & C_CBS) { |
| 1121 | if (ddflags & (C_ASCII|C_EBCDIC)) { |
| 1122 | if (ddflags & C_ASCII) { |
| 1123 | ddflags |= C_UNBLOCK; |
| 1124 | cfunc = unblock; |
| 1125 | } else { |
| 1126 | ddflags |= C_BLOCK; |
| 1127 | cfunc = block; |
| 1128 | } |
| 1129 | } else { |
| 1130 | fprintf(stderr, |
| 1131 | "cbs meaningless if not doing record operations\n"); |
| 1132 | exit(1); |
| 1133 | /* NOTREACHED */ |
| 1134 | } |
| 1135 | } else |
| 1136 | cfunc = def; |
| 1137 | |
| 1138 | /* Read, write and seek calls take off_t as arguments. |
| 1139 | * |
| 1140 | * The following check is not done because an off_t is a quad |
| 1141 | * for current NetBSD implementations. |
| 1142 | * |
| 1143 | * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz) |
| 1144 | * errx(1, "seek offsets cannot be larger than %d", INT_MAX); |
| 1145 | */ |
| 1146 | } |
| 1147 | |
| 1148 | static int |
| 1149 | c_arg(const void *a, const void *b) |
| 1150 | { |
| 1151 | |
| 1152 | return (strcmp(((const struct arg *)a)->name, |
| 1153 | ((const struct arg *)b)->name)); |
| 1154 | } |
| 1155 | |
| 1156 | static long long strsuftoll(const char* name, const char* arg, int def, unsigned int max) |
| 1157 | { |
| 1158 | long long result; |
| 1159 | |
| 1160 | if (sscanf(arg, "%lld", &result) == 0) |
| 1161 | result = def; |
| 1162 | return result; |
| 1163 | } |
| 1164 | |
| 1165 | static void |
| 1166 | f_bs(char *arg) |
| 1167 | { |
| 1168 | |
| 1169 | in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX); |
| 1170 | } |
| 1171 | |
| 1172 | static void |
| 1173 | f_cbs(char *arg) |
| 1174 | { |
| 1175 | |
| 1176 | cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX); |
| 1177 | } |
| 1178 | |
| 1179 | static void |
| 1180 | f_count(char *arg) |
| 1181 | { |
| 1182 | |
| 1183 | cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX); |
| 1184 | if (!cpy_cnt) |
| 1185 | terminate(0); |
| 1186 | } |
| 1187 | |
| 1188 | static void |
| 1189 | f_files(char *arg) |
| 1190 | { |
| 1191 | |
| 1192 | files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX); |
| 1193 | if (!files_cnt) |
| 1194 | terminate(0); |
| 1195 | } |
| 1196 | |
| 1197 | static void |
| 1198 | f_ibs(char *arg) |
| 1199 | { |
| 1200 | |
| 1201 | if (!(ddflags & C_BS)) |
| 1202 | in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX); |
| 1203 | } |
| 1204 | |
| 1205 | static void |
| 1206 | f_if(char *arg) |
| 1207 | { |
| 1208 | |
| 1209 | in.name = arg; |
| 1210 | } |
| 1211 | |
| 1212 | static void |
| 1213 | f_obs(char *arg) |
| 1214 | { |
| 1215 | |
| 1216 | if (!(ddflags & C_BS)) |
| 1217 | out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX); |
| 1218 | } |
| 1219 | |
| 1220 | static void |
| 1221 | f_of(char *arg) |
| 1222 | { |
| 1223 | |
| 1224 | out.name = arg; |
| 1225 | } |
| 1226 | |
| 1227 | static void |
| 1228 | f_seek(char *arg) |
| 1229 | { |
| 1230 | |
| 1231 | out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX); |
| 1232 | } |
| 1233 | |
| 1234 | static void |
| 1235 | f_skip(char *arg) |
| 1236 | { |
| 1237 | |
| 1238 | in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX); |
| 1239 | } |
| 1240 | |
| 1241 | static void |
| 1242 | f_progress(char *arg) |
| 1243 | { |
| 1244 | |
| 1245 | if (*arg != '0') |
| 1246 | progress = 1; |
| 1247 | } |
| 1248 | |
| 1249 | #ifdef NO_CONV |
| 1250 | /* Build a small version (i.e. for a ramdisk root) */ |
| 1251 | static void |
| 1252 | f_conv(char *arg) |
| 1253 | { |
| 1254 | |
| 1255 | fprintf(stderr, "conv option disabled\n"); |
| 1256 | exit(1); |
| 1257 | /* NOTREACHED */ |
| 1258 | } |
| 1259 | #else /* NO_CONV */ |
| 1260 | |
| 1261 | static const struct conv { |
| 1262 | const char *name; |
| 1263 | u_int set, noset; |
| 1264 | const u_char *ctab; |
| 1265 | } clist[] = { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1266 | { "block", C_BLOCK, C_UNBLOCK, NULL }, |
Jeff Sharkey | 393e559 | 2012-05-29 14:25:04 -0700 | [diff] [blame] | 1267 | { "fdatasync", C_FDATASYNC, 0, NULL }, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1268 | { "noerror", C_NOERROR, 0, NULL }, |
| 1269 | { "notrunc", C_NOTRUNC, 0, NULL }, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1270 | { "osync", C_OSYNC, C_BS, NULL }, |
| 1271 | { "sparse", C_SPARSE, 0, NULL }, |
| 1272 | { "swab", C_SWAB, 0, NULL }, |
| 1273 | { "sync", C_SYNC, 0, NULL }, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1274 | { "unblock", C_UNBLOCK, C_BLOCK, NULL }, |
| 1275 | /* If you add items to this table, be sure to add the |
| 1276 | * conversions to the C_BS check in the jcl routine above. |
| 1277 | */ |
| 1278 | }; |
| 1279 | |
| 1280 | static void |
| 1281 | f_conv(char *arg) |
| 1282 | { |
| 1283 | struct conv *cp, tmp; |
| 1284 | |
| 1285 | while (arg != NULL) { |
| 1286 | tmp.name = strsep(&arg, ","); |
| 1287 | if (!(cp = (struct conv *)bsearch(&tmp, clist, |
| 1288 | sizeof(clist)/sizeof(struct conv), sizeof(struct conv), |
| 1289 | c_conv))) { |
| 1290 | errx(EXIT_FAILURE, "unknown conversion %s", tmp.name); |
| 1291 | /* NOTREACHED */ |
| 1292 | } |
| 1293 | if (ddflags & cp->noset) { |
| 1294 | errx(EXIT_FAILURE, "%s: illegal conversion combination", tmp.name); |
| 1295 | /* NOTREACHED */ |
| 1296 | } |
| 1297 | ddflags |= cp->set; |
| 1298 | if (cp->ctab) |
| 1299 | ctab = cp->ctab; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | static int |
| 1304 | c_conv(const void *a, const void *b) |
| 1305 | { |
| 1306 | |
| 1307 | return (strcmp(((const struct conv *)a)->name, |
| 1308 | ((const struct conv *)b)->name)); |
| 1309 | } |
| 1310 | |
| 1311 | #endif /* NO_CONV */ |
| 1312 | |
| 1313 | |