/*- * Copyright (c) 1991 The Regents of the University of California. * All rights reserved. * * This code is derived from software contributed to Berkeley by * Keith Muller of the University of California, San Diego and Lance * Visser of Convex Computer Corporation. * * %sccs.include.redist.c% */ #ifndef lint char copyright[] = "@(#) Copyright (c) 1991 The Regents of the University of California.\n\ All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)dd.c 5.9 (Berkeley) 07/29/91"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include "dd.h" #include "extern.h" static void dd_close __P((void)); static void dd_in __P((void)); static void setup __P((void)); IO in, out; /* input/output state */ STAT st; /* statistics */ void (*cfunc)(); /* conversion function */ u_long cpy_cnt; /* # of blocks to copy */ u_int ddflags; /* conversion options */ u_int cbsz; /* conversion block size */ u_int files_cnt = 1; /* # of files to copy */ u_char *ctab; /* conversion table */ main(argc, argv) int argc; char **argv; { jcl(argv); setup(); (void)signal(SIGINFO, summary); (void)signal(SIGINT, terminate); while (files_cnt--) dd_in(); dd_close(); summary(0); exit(0); } static void setup() { register u_int cnt; struct stat sb; struct mtget mt; if (in.name == NULL) { in.name = "stdin"; in.fd = STDIN_FILENO; } else { in.fd = open(in.name, O_RDONLY, 0); if (in.fd < 0) err("%s: %s", in.name, strerror(errno)); } if (fstat(in.fd, &sb)) err("%s: %s", in.name, strerror(errno)); if (S_ISCHR(sb.st_mode)) in.flags |= ioctl(in.fd, MTIOCGET, &mt) ? ISCHR : ISTAPE; else if (lseek(in.fd, 0L, SEEK_CUR) == -1 && errno == ESPIPE) in.flags |= ISPIPE; /* XXX fixed in 4.4BSD */ if (files_cnt > 1 && !(in.flags & ISTAPE)) err("files is not supported for non-tape devices"); if (out.name == NULL) { /* No way to check for read access here. */ out.fd = STDOUT_FILENO; out.name = "stdout"; } else { out.fd = open(out.name, O_RDWR|O_CREAT, DEFFILEMODE); /* * May not have read access, so try again with write only. * Without read we may have a problem if output also does * not support seeks. */ if (out.fd < 0) { out.fd = open(out.name, O_WRONLY|O_CREAT, DEFFILEMODE); out.flags |= NOREAD; } if (out.fd < 0) err("%s: %s", out.name, strerror(errno)); } if (fstat(out.fd, &sb)) err("%s: %s", out.name, strerror(errno)); if (S_ISCHR(sb.st_mode)) out.flags |= ioctl(out.fd, MTIOCGET, &mt) ? ISCHR : ISTAPE; else if (lseek(out.fd, 0L, SEEK_CUR) == -1 && errno == ESPIPE) out.flags |= ISPIPE; /* XXX fixed in 4.4BSD */ /* * Allocate space for the input and output buffers. If not doing * record oriented I/O, only need a single buffer. */ if (!(ddflags & (C_BLOCK|C_UNBLOCK))) { if (in.dbsz > out.dbsz) cnt = in.dbsz - 1 - in.dbsz / 2 + in.dbsz; else if (in.dbsz < out.dbsz) cnt = out.dbsz + in.dbsz - 1; else cnt = in.dbsz; if ((in.db = malloc(cnt)) == NULL) err("%s", strerror(errno)); out.db = in.db; } else if ((in.db = malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL || (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) err("%s", strerror(errno)); in.dbp = in.db; out.dbp = out.db; /* Position the input/output streams. */ if (in.offset) pos_in(); if (out.offset) pos_out(); /* Truncate the output file. */ if (ddflags & C_NOTRUNC) { if (out.flags & ISTAPE) err("notrunc is not supported for tape devices"); } else if (S_ISREG(sb.st_mode) && ftruncate(out.fd, (off_t)out.offset * out.dbsz)) err("%s: truncate: %s", out.name, strerror(errno)); /* * If converting case at the same time as another conversion, build a * table that does both at once. If just converting case, use the * built-in tables. */ if (ddflags & (C_LCASE|C_UCASE)) if (ddflags & C_ASCII) if (ddflags & C_LCASE) { for (cnt = 0; cnt < 0377; ++cnt) if (isupper(ctab[cnt])) ctab[cnt] = tolower(ctab[cnt]); } else { for (cnt = 0; cnt < 0377; ++cnt) if (islower(ctab[cnt])) ctab[cnt] = toupper(ctab[cnt]); } else if (ddflags & C_EBCDIC) if (ddflags & C_LCASE) { for (cnt = 0; cnt < 0377; ++cnt) if (isupper(cnt)) ctab[cnt] = ctab[tolower(cnt)]; } else { for (cnt = 0; cnt < 0377; ++cnt) if (islower(cnt)) ctab[cnt] = ctab[toupper(cnt)]; } else ctab = ddflags & C_LCASE ? u2l : l2u; } static void dd_in() { register int flags, n; for(flags = ddflags;;) { if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt) return; /* * Zero the buffer first if trying to recover from errors so * lose the minimum amount of data. If doing block operations * use spaces. */ if (flags & (C_NOERROR|C_SYNC)) if (flags & (C_BLOCK|C_UNBLOCK)) memset(in.dbp, ' ', in.dbsz); else bzero(in.dbp, in.dbsz); n = read(in.fd, in.dbp, in.dbsz); if (n == 0) { in.dbrcnt = 0; return; } /* Read error. */ if (n < 0) { /* * If noerror not specified, die. POSIX requires that * the warning message be followed by an I/O display. */ if (!(flags & C_NOERROR)) err("%s: %s", in.name, strerror(errno)); warn("%s: %s", in.name, strerror(errno)); summary(0); /* * If it's not a tape drive or a pipe, seek past the * error. If your OS doesn't do the right thing for * raw disks this section should be modified to re-read * in sector size chunks. */ if (!(in.flags & (ISPIPE|ISTAPE)) && lseek(in.fd, (off_t)in.dbsz, SEEK_CUR)) warn("%s: %s", in.name, strerror(errno)); /* If sync not specified, omit block and continue. */ if (!(ddflags & C_SYNC)) continue; /* Read errors count as full blocks. */ in.dbcnt += in.dbrcnt = in.dbsz; ++st.in_full; /* Handle full input blocks. */ } else if (n == in.dbsz) { in.dbcnt += in.dbrcnt = n; ++st.in_full; /* Handle partial input blocks. */ } else if (n != in.dbsz) { /* If sync, use the entire block. */ if (ddflags & C_SYNC) in.dbcnt += in.dbrcnt = in.dbsz; else in.dbcnt += in.dbrcnt = n; ++st.in_part; } /* * POSIX states that if bs is set and no other conversions * than noerror, notrunc or sync are specified, the block * is output without buffering as it is read. */ if (ddflags & C_BS) { out.dbcnt = in.dbcnt; dd_out(1); in.dbcnt = 0; continue; } if (ddflags & C_SWAB) { if ((n = in.dbcnt) & 1) { ++st.swab; --n; } swab(in.dbp, in.dbp, n); } in.dbp += in.dbrcnt; (*cfunc)(); } } /* * Cleanup any remaining I/O and flush output. If necesssary, output file * is truncated. */ static void dd_close() { if (cfunc == def) def_close(); else if (cfunc == block) block_close(); else if (cfunc == unblock) unblock_close(); if (out.dbcnt) dd_out(1); } void dd_out(force) int force; { static int warned; register int cnt, n, nw; register u_char *outp; /* * Write one or more blocks out. The common case is writing a full * output block in a single write; increment the full block stats. * Otherwise, we're into partial block writes. If a partial write, * and it's a character device, just warn. If a tape device, quit. * * The partial writes represent two cases. 1: Where the input block * was less than expected so the output block was less than expected. * 2: Where the input block was the right size but we were forced to * write the block in multiple chunks. The original versions of dd(1) * never wrote a block in more than a single write, so the latter case * never happened. * * One special case is if we're forced to do the write -- in that case * we play games with the buffer size, and it's usually a partial write. */ outp = out.db; for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { for (cnt = n;; cnt -= nw) { outp += nw = write(out.fd, outp, cnt); if (nw == n) { if (n != out.dbsz) ++st.out_part; else ++st.out_full; break; } if (nw < 0) err("%s: %s", out.name, strerror(errno)); ++st.out_part; if (nw == cnt) break; if (out.flags & ISCHR && !warned) { warned = 1; warn("%s: short write on character device", out.name); } if (out.flags & ISTAPE) err("%s: short write on tape device", out.name); } if ((out.dbcnt -= n) < out.dbsz) break; } /* Reassemble the output block. */ if (out.dbcnt) bcopy(out.dbp - out.dbcnt, out.db, out.dbcnt); out.dbp = out.db + out.dbcnt; }