14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993, 1994 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Keith Muller of the University of California, San Diego and Lance 74b88c807SRodney W. Grimes * Visser of Convex Computer Corporation. 84b88c807SRodney W. Grimes * 94b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 104b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 114b88c807SRodney W. Grimes * are met: 124b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 134b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 144b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 154b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 164b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 174b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 184b88c807SRodney W. Grimes * must display the following acknowledgement: 194b88c807SRodney W. Grimes * This product includes software developed by the University of 204b88c807SRodney W. Grimes * California, Berkeley and its contributors. 214b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 224b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 234b88c807SRodney W. Grimes * without specific prior written permission. 244b88c807SRodney W. Grimes * 254b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 264b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 274b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 284b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 294b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 304b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 314b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 324b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 334b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 344b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 354b88c807SRodney W. Grimes * SUCH DAMAGE. 364b88c807SRodney W. Grimes */ 374b88c807SRodney W. Grimes 384b88c807SRodney W. Grimes #ifndef lint 3978b09ffeSSteve Price static char const copyright[] = 404b88c807SRodney W. Grimes "@(#) Copyright (c) 1991, 1993, 1994\n\ 414b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 424b88c807SRodney W. Grimes #endif /* not lint */ 434b88c807SRodney W. Grimes 444b88c807SRodney W. Grimes #ifndef lint 45cbf6f7d3SPhilippe Charnier #if 0 461ba0e048SPhilippe Charnier static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94"; 47cbf6f7d3SPhilippe Charnier #endif 48cbf6f7d3SPhilippe Charnier static const char rcsid[] = 492a456239SPeter Wemm "$FreeBSD$"; 504b88c807SRodney W. Grimes #endif /* not lint */ 514b88c807SRodney W. Grimes 524b88c807SRodney W. Grimes #include <sys/param.h> 534b88c807SRodney W. Grimes #include <sys/stat.h> 54e08d7384SBrian Feldman #include <sys/conf.h> 55015a53cfSDavid E. O'Brien #include <sys/disklabel.h> 56e08d7384SBrian Feldman #include <sys/filio.h> 574b88c807SRodney W. Grimes 584b88c807SRodney W. Grimes #include <ctype.h> 594b88c807SRodney W. Grimes #include <err.h> 604b88c807SRodney W. Grimes #include <errno.h> 614b88c807SRodney W. Grimes #include <fcntl.h> 62ad66f7eeSPoul-Henning Kamp #include <locale.h> 634b88c807SRodney W. Grimes #include <stdio.h> 644b88c807SRodney W. Grimes #include <stdlib.h> 654b88c807SRodney W. Grimes #include <string.h> 664b88c807SRodney W. Grimes #include <unistd.h> 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes #include "dd.h" 694b88c807SRodney W. Grimes #include "extern.h" 704b88c807SRodney W. Grimes 71f9bcb0beSWarner Losh static void dd_close(void); 72f9bcb0beSWarner Losh static void dd_in(void); 73f9bcb0beSWarner Losh static void getfdtype(IO *); 74f9bcb0beSWarner Losh static void setup(void); 754b88c807SRodney W. Grimes 764b88c807SRodney W. Grimes IO in, out; /* input/output state */ 774b88c807SRodney W. Grimes STAT st; /* statistics */ 78f9bcb0beSWarner Losh void (*cfunc)(void); /* conversion function */ 794ed95537SBrian Feldman u_quad_t cpy_cnt; /* # of blocks to copy */ 809afa09cdSMark Murray static off_t pending = 0; /* pending seek if sparse */ 819afa09cdSMark Murray u_int ddflags = 0; /* conversion options */ 8258687472SBrian Feldman size_t cbsz; /* conversion block size */ 8354946e00SBrian Feldman quad_t files_cnt = 1; /* # of files to copy */ 8458687472SBrian Feldman const u_char *ctab; /* conversion table */ 854b88c807SRodney W. Grimes 864b88c807SRodney W. Grimes int 87f9bcb0beSWarner Losh main(int argc __unused, char *argv[]) 884b88c807SRodney W. Grimes { 89f7c627b2SAndrey A. Chernov (void)setlocale(LC_CTYPE, ""); 904b88c807SRodney W. Grimes jcl(argv); 914b88c807SRodney W. Grimes setup(); 924b88c807SRodney W. Grimes 934b88c807SRodney W. Grimes (void)signal(SIGINFO, summaryx); 944b88c807SRodney W. Grimes (void)signal(SIGINT, terminate); 954b88c807SRodney W. Grimes 964b88c807SRodney W. Grimes atexit(summary); 974b88c807SRodney W. Grimes 984b88c807SRodney W. Grimes while (files_cnt--) 994b88c807SRodney W. Grimes dd_in(); 1004b88c807SRodney W. Grimes 1014b88c807SRodney W. Grimes dd_close(); 1024b88c807SRodney W. Grimes exit(0); 1034b88c807SRodney W. Grimes } 1044b88c807SRodney W. Grimes 1054b88c807SRodney W. Grimes static void 106f9bcb0beSWarner Losh setup(void) 1074b88c807SRodney W. Grimes { 1084b88c807SRodney W. Grimes u_int cnt; 109ad66f7eeSPoul-Henning Kamp struct timeval tv; 1104b88c807SRodney W. Grimes 1114b88c807SRodney W. Grimes if (in.name == NULL) { 1124b88c807SRodney W. Grimes in.name = "stdin"; 1134b88c807SRodney W. Grimes in.fd = STDIN_FILENO; 1144b88c807SRodney W. Grimes } else { 11554946e00SBrian Feldman in.fd = open(in.name, O_RDONLY, 0); 116767bc8adSBrian Feldman if (in.fd == -1) 1174b88c807SRodney W. Grimes err(1, "%s", in.name); 1184b88c807SRodney W. Grimes } 1194b88c807SRodney W. Grimes 1204b88c807SRodney W. Grimes getfdtype(&in); 1214b88c807SRodney W. Grimes 1224b88c807SRodney W. Grimes if (files_cnt > 1 && !(in.flags & ISTAPE)) 1234b88c807SRodney W. Grimes errx(1, "files is not supported for non-tape devices"); 1244b88c807SRodney W. Grimes 1254b88c807SRodney W. Grimes if (out.name == NULL) { 1264b88c807SRodney W. Grimes /* No way to check for read access here. */ 1274b88c807SRodney W. Grimes out.fd = STDOUT_FILENO; 1284b88c807SRodney W. Grimes out.name = "stdout"; 1294b88c807SRodney W. Grimes } else { 1304b88c807SRodney W. Grimes #define OFLAGS \ 1314b88c807SRodney W. Grimes (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) 1324b88c807SRodney W. Grimes out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE); 1334b88c807SRodney W. Grimes /* 1344b88c807SRodney W. Grimes * May not have read access, so try again with write only. 1354b88c807SRodney W. Grimes * Without read we may have a problem if output also does 1364b88c807SRodney W. Grimes * not support seeks. 1374b88c807SRodney W. Grimes */ 138767bc8adSBrian Feldman if (out.fd == -1) { 1394b88c807SRodney W. Grimes out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); 1404b88c807SRodney W. Grimes out.flags |= NOREAD; 1414b88c807SRodney W. Grimes } 142767bc8adSBrian Feldman if (out.fd == -1) 1434b88c807SRodney W. Grimes err(1, "%s", out.name); 1444b88c807SRodney W. Grimes } 1454b88c807SRodney W. Grimes 1464b88c807SRodney W. Grimes getfdtype(&out); 1474b88c807SRodney W. Grimes 1484b88c807SRodney W. Grimes /* 1494b88c807SRodney W. Grimes * Allocate space for the input and output buffers. If not doing 1504b88c807SRodney W. Grimes * record oriented I/O, only need a single buffer. 1514b88c807SRodney W. Grimes */ 1524b88c807SRodney W. Grimes if (!(ddflags & (C_BLOCK | C_UNBLOCK))) { 1534b88c807SRodney W. Grimes if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) 15458687472SBrian Feldman err(1, "input buffer"); 1554b88c807SRodney W. Grimes out.db = in.db; 15658687472SBrian Feldman } else if ((in.db = malloc(MAX(in.dbsz, cbsz) + cbsz)) == NULL || 15758687472SBrian Feldman (out.db = malloc(out.dbsz + cbsz)) == NULL) 15858687472SBrian Feldman err(1, "output buffer"); 1594b88c807SRodney W. Grimes in.dbp = in.db; 1604b88c807SRodney W. Grimes out.dbp = out.db; 1614b88c807SRodney W. Grimes 1624b88c807SRodney W. Grimes /* Position the input/output streams. */ 1634b88c807SRodney W. Grimes if (in.offset) 1644b88c807SRodney W. Grimes pos_in(); 1654b88c807SRodney W. Grimes if (out.offset) 1664b88c807SRodney W. Grimes pos_out(); 1674b88c807SRodney W. Grimes 1684b88c807SRodney W. Grimes /* 1695976ee7eSBrian Feldman * Truncate the output file. If it fails on a type of output file 1705976ee7eSBrian Feldman * that it should _not_ fail on, error out. 1714b88c807SRodney W. Grimes */ 172c15c898eSBrian Feldman if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) && 173c15c898eSBrian Feldman out.flags & ISTRUNC) 174c15c898eSBrian Feldman if (ftruncate(out.fd, out.offset * out.dbsz) == -1) 175c15c898eSBrian Feldman err(1, "truncating %s", out.name); 1764b88c807SRodney W. Grimes 1774b88c807SRodney W. Grimes /* 1784b88c807SRodney W. Grimes * If converting case at the same time as another conversion, build a 1794b88c807SRodney W. Grimes * table that does both at once. If just converting case, use the 1804b88c807SRodney W. Grimes * built-in tables. 1814b88c807SRodney W. Grimes */ 182426e9c1dSWarner Losh if (ddflags & (C_LCASE | C_UCASE)) { 18358687472SBrian Feldman if (ddflags & (C_ASCII | C_EBCDIC)) { 1844b88c807SRodney W. Grimes if (ddflags & C_LCASE) { 185c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 18658687472SBrian Feldman casetab[cnt] = tolower(ctab[cnt]); 1874b88c807SRodney W. Grimes } else { 188c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 18958687472SBrian Feldman casetab[cnt] = toupper(ctab[cnt]); 1904b88c807SRodney W. Grimes } 19158687472SBrian Feldman } else { 1924b88c807SRodney W. Grimes if (ddflags & C_LCASE) { 193c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 194c15c898eSBrian Feldman casetab[cnt] = tolower((int)cnt); 1954b88c807SRodney W. Grimes } else { 196c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 197c15c898eSBrian Feldman casetab[cnt] = toupper((int)cnt); 198dde07463SAndrey A. Chernov } 199dde07463SAndrey A. Chernov } 20058687472SBrian Feldman ctab = casetab; 201426e9c1dSWarner Losh } 20258687472SBrian Feldman 203ad66f7eeSPoul-Henning Kamp (void)gettimeofday(&tv, (struct timezone *)NULL); 204ad66f7eeSPoul-Henning Kamp st.start = tv.tv_sec + tv.tv_usec * 1e-6; 2054b88c807SRodney W. Grimes } 2064b88c807SRodney W. Grimes 2074b88c807SRodney W. Grimes static void 208f9bcb0beSWarner Losh getfdtype(IO *io) 2094b88c807SRodney W. Grimes { 2104b88c807SRodney W. Grimes struct stat sb; 211e08d7384SBrian Feldman int type; 2124b88c807SRodney W. Grimes 21358687472SBrian Feldman if (fstat(io->fd, &sb) == -1) 2144b88c807SRodney W. Grimes err(1, "%s", io->name); 215c15c898eSBrian Feldman if (S_ISREG(sb.st_mode)) 216c15c898eSBrian Feldman io->flags |= ISTRUNC; 217e08d7384SBrian Feldman if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) { 218ff8bb989SBrian Feldman if (ioctl(io->fd, FIODTYPE, &type) == -1) { 219bf3367d0SBrian Feldman err(1, "%s", io->name); 220ff8bb989SBrian Feldman } else { 221e08d7384SBrian Feldman if (type & D_TAPE) 222e08d7384SBrian Feldman io->flags |= ISTAPE; 223015a53cfSDavid E. O'Brien else if (type & (D_DISK | D_MEM)) { 224015a53cfSDavid E. O'Brien if (type & D_DISK) { 225015a53cfSDavid E. O'Brien const int one = 1; 226015a53cfSDavid E. O'Brien 227015a53cfSDavid E. O'Brien (void)ioctl(io->fd, DIOCWLABEL, &one); 228015a53cfSDavid E. O'Brien } 22932952d4bSBrian Feldman io->flags |= ISSEEK; 230015a53cfSDavid E. O'Brien } 231e08d7384SBrian Feldman if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0) 232e08d7384SBrian Feldman io->flags |= ISCHR; 233ff8bb989SBrian Feldman } 2345ff6541eSBrian Feldman return; 2355ff6541eSBrian Feldman } 2365ff6541eSBrian Feldman errno = 0; 2375ff6541eSBrian Feldman if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) 238e08d7384SBrian Feldman io->flags |= ISPIPE; 2395ff6541eSBrian Feldman else 2405ff6541eSBrian Feldman io->flags |= ISSEEK; 2414b88c807SRodney W. Grimes } 2424b88c807SRodney W. Grimes 2434b88c807SRodney W. Grimes static void 244f9bcb0beSWarner Losh dd_in(void) 2454b88c807SRodney W. Grimes { 246767bc8adSBrian Feldman ssize_t n; 2474b88c807SRodney W. Grimes 248af041bf3SJonathan Lemon for (;;) { 2495ff6541eSBrian Feldman switch (cpy_cnt) { 2505ff6541eSBrian Feldman case -1: /* count=0 was specified */ 2514b88c807SRodney W. Grimes return; 2525ff6541eSBrian Feldman case 0: 2535ff6541eSBrian Feldman break; 2545ff6541eSBrian Feldman default: 255c15c898eSBrian Feldman if (st.in_full + st.in_part >= (u_quad_t)cpy_cnt) 2565ff6541eSBrian Feldman return; 2575ff6541eSBrian Feldman break; 2585ff6541eSBrian Feldman } 2594b88c807SRodney W. Grimes 2604b88c807SRodney W. Grimes /* 26158687472SBrian Feldman * Zero the buffer first if sync; if doing block operations, 2624b88c807SRodney W. Grimes * use spaces. 2634b88c807SRodney W. Grimes */ 264767bc8adSBrian Feldman if (ddflags & C_SYNC) { 265af041bf3SJonathan Lemon if (ddflags & (C_BLOCK | C_UNBLOCK)) 2664b88c807SRodney W. Grimes memset(in.dbp, ' ', in.dbsz); 2674b88c807SRodney W. Grimes else 2684b88c807SRodney W. Grimes memset(in.dbp, 0, in.dbsz); 269767bc8adSBrian Feldman } 2704b88c807SRodney W. Grimes 2714b88c807SRodney W. Grimes n = read(in.fd, in.dbp, in.dbsz); 2724b88c807SRodney W. Grimes if (n == 0) { 2734b88c807SRodney W. Grimes in.dbrcnt = 0; 2744b88c807SRodney W. Grimes return; 2754b88c807SRodney W. Grimes } 2764b88c807SRodney W. Grimes 2774b88c807SRodney W. Grimes /* Read error. */ 278767bc8adSBrian Feldman if (n == -1) { 2794b88c807SRodney W. Grimes /* 2804b88c807SRodney W. Grimes * If noerror not specified, die. POSIX requires that 2814b88c807SRodney W. Grimes * the warning message be followed by an I/O display. 2824b88c807SRodney W. Grimes */ 283af041bf3SJonathan Lemon if (!(ddflags & C_NOERROR)) 2844b88c807SRodney W. Grimes err(1, "%s", in.name); 2854b88c807SRodney W. Grimes warn("%s", in.name); 2864b88c807SRodney W. Grimes summary(); 2874b88c807SRodney W. Grimes 2884b88c807SRodney W. Grimes /* 2897599187eSBrian Feldman * If it's a seekable file descriptor, seek past the 2904b88c807SRodney W. Grimes * error. If your OS doesn't do the right thing for 2914b88c807SRodney W. Grimes * raw disks this section should be modified to re-read 2924b88c807SRodney W. Grimes * in sector size chunks. 2934b88c807SRodney W. Grimes */ 2947599187eSBrian Feldman if (in.flags & ISSEEK && 2954b88c807SRodney W. Grimes lseek(in.fd, (off_t)in.dbsz, SEEK_CUR)) 2964b88c807SRodney W. Grimes warn("%s", in.name); 2974b88c807SRodney W. Grimes 2984b88c807SRodney W. Grimes /* If sync not specified, omit block and continue. */ 2994b88c807SRodney W. Grimes if (!(ddflags & C_SYNC)) 3004b88c807SRodney W. Grimes continue; 3014b88c807SRodney W. Grimes 3024b88c807SRodney W. Grimes /* Read errors count as full blocks. */ 3034b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = in.dbsz; 3044b88c807SRodney W. Grimes ++st.in_full; 3054b88c807SRodney W. Grimes 3064b88c807SRodney W. Grimes /* Handle full input blocks. */ 307c15c898eSBrian Feldman } else if ((size_t)n == in.dbsz) { 3084b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = n; 3094b88c807SRodney W. Grimes ++st.in_full; 3104b88c807SRodney W. Grimes 3114b88c807SRodney W. Grimes /* Handle partial input blocks. */ 3124b88c807SRodney W. Grimes } else { 3134b88c807SRodney W. Grimes /* If sync, use the entire block. */ 3144b88c807SRodney W. Grimes if (ddflags & C_SYNC) 3154b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = in.dbsz; 3164b88c807SRodney W. Grimes else 3174b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = n; 3184b88c807SRodney W. Grimes ++st.in_part; 3194b88c807SRodney W. Grimes } 3204b88c807SRodney W. Grimes 3214b88c807SRodney W. Grimes /* 3224b88c807SRodney W. Grimes * POSIX states that if bs is set and no other conversions 3234b88c807SRodney W. Grimes * than noerror, notrunc or sync are specified, the block 3244b88c807SRodney W. Grimes * is output without buffering as it is read. 3254b88c807SRodney W. Grimes */ 3264b88c807SRodney W. Grimes if (ddflags & C_BS) { 3274b88c807SRodney W. Grimes out.dbcnt = in.dbcnt; 3284b88c807SRodney W. Grimes dd_out(1); 3294b88c807SRodney W. Grimes in.dbcnt = 0; 3304b88c807SRodney W. Grimes continue; 3314b88c807SRodney W. Grimes } 3324b88c807SRodney W. Grimes 3334b88c807SRodney W. Grimes if (ddflags & C_SWAB) { 334a28ea077SJoerg Wunsch if ((n = in.dbrcnt) & 1) { 3354b88c807SRodney W. Grimes ++st.swab; 3364b88c807SRodney W. Grimes --n; 3374b88c807SRodney W. Grimes } 338c15c898eSBrian Feldman swab(in.dbp, in.dbp, (size_t)n); 3394b88c807SRodney W. Grimes } 3404b88c807SRodney W. Grimes 3414b88c807SRodney W. Grimes in.dbp += in.dbrcnt; 3424b88c807SRodney W. Grimes (*cfunc)(); 3434b88c807SRodney W. Grimes } 3444b88c807SRodney W. Grimes } 3454b88c807SRodney W. Grimes 3464b88c807SRodney W. Grimes /* 34758687472SBrian Feldman * Clean up any remaining I/O and flush output. If necessary, the output file 3484b88c807SRodney W. Grimes * is truncated. 3494b88c807SRodney W. Grimes */ 3504b88c807SRodney W. Grimes static void 351f9bcb0beSWarner Losh dd_close(void) 3524b88c807SRodney W. Grimes { 3534b88c807SRodney W. Grimes if (cfunc == def) 3544b88c807SRodney W. Grimes def_close(); 3554b88c807SRodney W. Grimes else if (cfunc == block) 3564b88c807SRodney W. Grimes block_close(); 3574b88c807SRodney W. Grimes else if (cfunc == unblock) 3584b88c807SRodney W. Grimes unblock_close(); 359af041bf3SJonathan Lemon if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) { 360af041bf3SJonathan Lemon if (ddflags & (C_BLOCK | C_UNBLOCK)) 361af041bf3SJonathan Lemon memset(out.dbp, ' ', out.dbsz - out.dbcnt); 362af041bf3SJonathan Lemon else 3634b88c807SRodney W. Grimes memset(out.dbp, 0, out.dbsz - out.dbcnt); 3644b88c807SRodney W. Grimes out.dbcnt = out.dbsz; 3654b88c807SRodney W. Grimes } 3664ddfeabdSJoerg Wunsch if (out.dbcnt || pending) 3674b88c807SRodney W. Grimes dd_out(1); 3684b88c807SRodney W. Grimes } 3694b88c807SRodney W. Grimes 3704b88c807SRodney W. Grimes void 371f9bcb0beSWarner Losh dd_out(int force) 3724b88c807SRodney W. Grimes { 3734b88c807SRodney W. Grimes u_char *outp; 37458687472SBrian Feldman size_t cnt, i, n; 37558687472SBrian Feldman ssize_t nw; 37658687472SBrian Feldman static int warned; 37758687472SBrian Feldman int sparse; 3784b88c807SRodney W. Grimes 3794b88c807SRodney W. Grimes /* 3804b88c807SRodney W. Grimes * Write one or more blocks out. The common case is writing a full 3814b88c807SRodney W. Grimes * output block in a single write; increment the full block stats. 3824b88c807SRodney W. Grimes * Otherwise, we're into partial block writes. If a partial write, 3834b88c807SRodney W. Grimes * and it's a character device, just warn. If a tape device, quit. 3844b88c807SRodney W. Grimes * 3854b88c807SRodney W. Grimes * The partial writes represent two cases. 1: Where the input block 3864b88c807SRodney W. Grimes * was less than expected so the output block was less than expected. 3874b88c807SRodney W. Grimes * 2: Where the input block was the right size but we were forced to 3884b88c807SRodney W. Grimes * write the block in multiple chunks. The original versions of dd(1) 3894b88c807SRodney W. Grimes * never wrote a block in more than a single write, so the latter case 3904b88c807SRodney W. Grimes * never happened. 3914b88c807SRodney W. Grimes * 3924b88c807SRodney W. Grimes * One special case is if we're forced to do the write -- in that case 3934b88c807SRodney W. Grimes * we play games with the buffer size, and it's usually a partial write. 3944b88c807SRodney W. Grimes */ 3954b88c807SRodney W. Grimes outp = out.db; 3964b88c807SRodney W. Grimes for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { 3974b88c807SRodney W. Grimes for (cnt = n;; cnt -= nw) { 3984ddfeabdSJoerg Wunsch sparse = 0; 3994ddfeabdSJoerg Wunsch if (ddflags & C_SPARSE) { 4004ddfeabdSJoerg Wunsch sparse = 1; /* Is buffer sparse? */ 4014ddfeabdSJoerg Wunsch for (i = 0; i < cnt; i++) 4024ddfeabdSJoerg Wunsch if (outp[i] != 0) { 4034ddfeabdSJoerg Wunsch sparse = 0; 4044ddfeabdSJoerg Wunsch break; 4054ddfeabdSJoerg Wunsch } 4064ddfeabdSJoerg Wunsch } 4074ddfeabdSJoerg Wunsch if (sparse && !force) { 4084ddfeabdSJoerg Wunsch pending += cnt; 4094ddfeabdSJoerg Wunsch nw = cnt; 4104ddfeabdSJoerg Wunsch } else { 4114ddfeabdSJoerg Wunsch if (pending != 0) { 4124ddfeabdSJoerg Wunsch if (force) 4134ddfeabdSJoerg Wunsch pending--; 41454946e00SBrian Feldman if (lseek(out.fd, pending, SEEK_CUR) == 41554946e00SBrian Feldman -1) 41654946e00SBrian Feldman err(2, "%s: seek error creating sparse file", 41754946e00SBrian Feldman out.name); 4184ddfeabdSJoerg Wunsch if (force) 4194ddfeabdSJoerg Wunsch write(out.fd, outp, 1); 4204ddfeabdSJoerg Wunsch pending = 0; 4214ddfeabdSJoerg Wunsch } 4224ddfeabdSJoerg Wunsch if (cnt) 4234b88c807SRodney W. Grimes nw = write(out.fd, outp, cnt); 4244ddfeabdSJoerg Wunsch else 4254ddfeabdSJoerg Wunsch return; 4264ddfeabdSJoerg Wunsch } 4274ddfeabdSJoerg Wunsch 4284b88c807SRodney W. Grimes if (nw <= 0) { 4294b88c807SRodney W. Grimes if (nw == 0) 4304b88c807SRodney W. Grimes errx(1, "%s: end of device", out.name); 4314b88c807SRodney W. Grimes if (errno != EINTR) 4324b88c807SRodney W. Grimes err(1, "%s", out.name); 4334b88c807SRodney W. Grimes nw = 0; 4344b88c807SRodney W. Grimes } 4354b88c807SRodney W. Grimes outp += nw; 4364b88c807SRodney W. Grimes st.bytes += nw; 437c15c898eSBrian Feldman if ((size_t)nw == n) { 4384b88c807SRodney W. Grimes if (n != out.dbsz) 4394b88c807SRodney W. Grimes ++st.out_part; 4404b88c807SRodney W. Grimes else 4414b88c807SRodney W. Grimes ++st.out_full; 4424b88c807SRodney W. Grimes break; 4434b88c807SRodney W. Grimes } 4444b88c807SRodney W. Grimes ++st.out_part; 445c15c898eSBrian Feldman if ((size_t)nw == cnt) 4464b88c807SRodney W. Grimes break; 4477599187eSBrian Feldman if (out.flags & ISTAPE) 4487599187eSBrian Feldman errx(1, "%s: short write on tape device", 4497599187eSBrian Feldman out.name); 4504b88c807SRodney W. Grimes if (out.flags & ISCHR && !warned) { 4514b88c807SRodney W. Grimes warned = 1; 4524b88c807SRodney W. Grimes warnx("%s: short write on character device", 4534b88c807SRodney W. Grimes out.name); 4544b88c807SRodney W. Grimes } 4554b88c807SRodney W. Grimes } 4564b88c807SRodney W. Grimes if ((out.dbcnt -= n) < out.dbsz) 4574b88c807SRodney W. Grimes break; 4584b88c807SRodney W. Grimes } 4594b88c807SRodney W. Grimes 4604b88c807SRodney W. Grimes /* Reassemble the output block. */ 4614b88c807SRodney W. Grimes if (out.dbcnt) 46258687472SBrian Feldman (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt); 4634b88c807SRodney W. Grimes out.dbp = out.db + out.dbcnt; 4644b88c807SRodney W. Grimes } 465