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. 3689730b29SDavid Greenman * 374ddfeabdSJoerg Wunsch * $Id: dd.c,v 1.11 1997/08/19 19:46:18 jlemon Exp $ 384b88c807SRodney W. Grimes */ 394b88c807SRodney W. Grimes 404b88c807SRodney W. Grimes #ifndef lint 4178b09ffeSSteve Price static char const copyright[] = 424b88c807SRodney W. Grimes "@(#) Copyright (c) 1991, 1993, 1994\n\ 434b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 444b88c807SRodney W. Grimes #endif /* not lint */ 454b88c807SRodney W. Grimes 464b88c807SRodney W. Grimes #ifndef lint 4778b09ffeSSteve Price static char const sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94"; 484b88c807SRodney W. Grimes #endif /* not lint */ 494b88c807SRodney W. Grimes 504b88c807SRodney W. Grimes #include <sys/param.h> 514b88c807SRodney W. Grimes #include <sys/stat.h> 524b88c807SRodney W. Grimes #include <sys/ioctl.h> 534b88c807SRodney W. Grimes #include <sys/mtio.h> 544b88c807SRodney W. Grimes 554b88c807SRodney W. Grimes #include <ctype.h> 564b88c807SRodney W. Grimes #include <err.h> 574b88c807SRodney W. Grimes #include <errno.h> 584b88c807SRodney W. Grimes #include <fcntl.h> 59ad66f7eeSPoul-Henning Kamp #include <locale.h> 604b88c807SRodney W. Grimes #include <signal.h> 614b88c807SRodney W. Grimes #include <stdio.h> 624b88c807SRodney W. Grimes #include <stdlib.h> 634b88c807SRodney W. Grimes #include <string.h> 644b88c807SRodney W. Grimes #include <unistd.h> 654b88c807SRodney W. Grimes 664b88c807SRodney W. Grimes #include "dd.h" 674b88c807SRodney W. Grimes #include "extern.h" 684b88c807SRodney W. Grimes 694b88c807SRodney W. Grimes static void dd_close __P((void)); 704b88c807SRodney W. Grimes static void dd_in __P((void)); 714b88c807SRodney W. Grimes static void getfdtype __P((IO *)); 724b88c807SRodney W. Grimes static void setup __P((void)); 734b88c807SRodney W. Grimes 744b88c807SRodney W. Grimes IO in, out; /* input/output state */ 754b88c807SRodney W. Grimes STAT st; /* statistics */ 764b88c807SRodney W. Grimes void (*cfunc) __P((void)); /* conversion function */ 774b88c807SRodney W. Grimes u_long cpy_cnt; /* # of blocks to copy */ 784ddfeabdSJoerg Wunsch u_long pending = 0; /* pending seek if sparse */ 794b88c807SRodney W. Grimes u_int ddflags; /* conversion options */ 804b88c807SRodney W. Grimes u_int cbsz; /* conversion block size */ 814b88c807SRodney W. Grimes u_int files_cnt = 1; /* # of files to copy */ 824b88c807SRodney W. Grimes u_char *ctab; /* conversion table */ 834b88c807SRodney W. Grimes 844b88c807SRodney W. Grimes int 854b88c807SRodney W. Grimes main(argc, argv) 864b88c807SRodney W. Grimes int argc; 874b88c807SRodney W. Grimes 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 1064b88c807SRodney W. Grimes setup() 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 { 1154b88c807SRodney W. Grimes in.fd = open(in.name, O_RDONLY, 0); 1164b88c807SRodney W. Grimes if (in.fd < 0) 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 */ 1384b88c807SRodney W. Grimes if (out.fd < 0) { 1394b88c807SRodney W. Grimes out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); 1404b88c807SRodney W. Grimes out.flags |= NOREAD; 1414b88c807SRodney W. Grimes } 1424b88c807SRodney W. Grimes if (out.fd < 0) 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) 1544b88c807SRodney W. Grimes err(1, NULL); 1554b88c807SRodney W. Grimes out.db = in.db; 1564b88c807SRodney W. Grimes } else if ((in.db = 1574b88c807SRodney W. Grimes malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL || 1584b88c807SRodney W. Grimes (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) 1594b88c807SRodney W. Grimes err(1, NULL); 1604b88c807SRodney W. Grimes in.dbp = in.db; 1614b88c807SRodney W. Grimes out.dbp = out.db; 1624b88c807SRodney W. Grimes 1634b88c807SRodney W. Grimes /* Position the input/output streams. */ 1644b88c807SRodney W. Grimes if (in.offset) 1654b88c807SRodney W. Grimes pos_in(); 1664b88c807SRodney W. Grimes if (out.offset) 1674b88c807SRodney W. Grimes pos_out(); 1684b88c807SRodney W. Grimes 1694b88c807SRodney W. Grimes /* 1704b88c807SRodney W. Grimes * Truncate the output file; ignore errors because it fails on some 1714b88c807SRodney W. Grimes * kinds of output files, tapes, for example. 1724b88c807SRodney W. Grimes */ 17378b09ffeSSteve Price if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK)) 1744b88c807SRodney W. Grimes (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz); 1754b88c807SRodney W. Grimes 1764b88c807SRodney W. Grimes /* 1774b88c807SRodney W. Grimes * If converting case at the same time as another conversion, build a 1784b88c807SRodney W. Grimes * table that does both at once. If just converting case, use the 1794b88c807SRodney W. Grimes * built-in tables. 1804b88c807SRodney W. Grimes */ 1814b88c807SRodney W. Grimes if (ddflags & (C_LCASE|C_UCASE)) 1824b88c807SRodney W. Grimes if (ddflags & C_ASCII) 1834b88c807SRodney W. Grimes if (ddflags & C_LCASE) { 184c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 1854b88c807SRodney W. Grimes if (isupper(ctab[cnt])) 1864b88c807SRodney W. Grimes ctab[cnt] = tolower(ctab[cnt]); 1874b88c807SRodney W. Grimes } else { 188c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 1894b88c807SRodney W. Grimes if (islower(ctab[cnt])) 1904b88c807SRodney W. Grimes ctab[cnt] = toupper(ctab[cnt]); 1914b88c807SRodney W. Grimes } 1924b88c807SRodney W. Grimes else if (ddflags & C_EBCDIC) 1934b88c807SRodney W. Grimes if (ddflags & C_LCASE) { 194c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 1954b88c807SRodney W. Grimes if (isupper(cnt)) 1964b88c807SRodney W. Grimes ctab[cnt] = ctab[tolower(cnt)]; 1974b88c807SRodney W. Grimes } else { 198c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 1994b88c807SRodney W. Grimes if (islower(cnt)) 2004b88c807SRodney W. Grimes ctab[cnt] = ctab[toupper(cnt)]; 2014b88c807SRodney W. Grimes } 202dde07463SAndrey A. Chernov else { 2034b88c807SRodney W. Grimes ctab = ddflags & C_LCASE ? u2l : l2u; 204dde07463SAndrey A. Chernov if (ddflags & C_LCASE) { 205c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 206dde07463SAndrey A. Chernov if (isupper(cnt)) 207dde07463SAndrey A. Chernov ctab[cnt] = tolower(cnt); 208c5191e58SAndrey A. Chernov else 209c5191e58SAndrey A. Chernov ctab[cnt] = cnt; 210dde07463SAndrey A. Chernov } else { 211c5191e58SAndrey A. Chernov for (cnt = 0; cnt <= 0377; ++cnt) 212dde07463SAndrey A. Chernov if (islower(cnt)) 213dde07463SAndrey A. Chernov ctab[cnt] = toupper(cnt); 214c5191e58SAndrey A. Chernov else 215c5191e58SAndrey A. Chernov ctab[cnt] = cnt; 216dde07463SAndrey A. Chernov } 217dde07463SAndrey A. Chernov } 218ad66f7eeSPoul-Henning Kamp (void)gettimeofday(&tv, (struct timezone *)NULL); 219ad66f7eeSPoul-Henning Kamp st.start = tv.tv_sec + tv.tv_usec * 1e-6; 2204b88c807SRodney W. Grimes } 2214b88c807SRodney W. Grimes 2224b88c807SRodney W. Grimes static void 2234b88c807SRodney W. Grimes getfdtype(io) 2244b88c807SRodney W. Grimes IO *io; 2254b88c807SRodney W. Grimes { 2264b88c807SRodney W. Grimes struct mtget mt; 2274b88c807SRodney W. Grimes struct stat sb; 2284b88c807SRodney W. Grimes 2294b88c807SRodney W. Grimes if (fstat(io->fd, &sb)) 2304b88c807SRodney W. Grimes err(1, "%s", io->name); 2314b88c807SRodney W. Grimes if (S_ISCHR(sb.st_mode)) 2324b88c807SRodney W. Grimes io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE; 2334b88c807SRodney W. Grimes else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) 2344b88c807SRodney W. Grimes io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */ 2354b88c807SRodney W. Grimes } 2364b88c807SRodney W. Grimes 2374b88c807SRodney W. Grimes static void 2384b88c807SRodney W. Grimes dd_in() 2394b88c807SRodney W. Grimes { 240af041bf3SJonathan Lemon int n; 2414b88c807SRodney W. Grimes 242af041bf3SJonathan Lemon for (;;) { 2434b88c807SRodney W. Grimes if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt) 2444b88c807SRodney W. Grimes return; 2454b88c807SRodney W. Grimes 2464b88c807SRodney W. Grimes /* 247af041bf3SJonathan Lemon * Zero the buffer first if sync; If doing block operations 2484b88c807SRodney W. Grimes * use spaces. 2494b88c807SRodney W. Grimes */ 250af041bf3SJonathan Lemon if (ddflags & C_SYNC) 251af041bf3SJonathan Lemon if (ddflags & (C_BLOCK|C_UNBLOCK)) 2524b88c807SRodney W. Grimes memset(in.dbp, ' ', in.dbsz); 2534b88c807SRodney W. Grimes else 2544b88c807SRodney W. Grimes memset(in.dbp, 0, in.dbsz); 2554b88c807SRodney W. Grimes 2564b88c807SRodney W. Grimes n = read(in.fd, in.dbp, in.dbsz); 2574b88c807SRodney W. Grimes if (n == 0) { 2584b88c807SRodney W. Grimes in.dbrcnt = 0; 2594b88c807SRodney W. Grimes return; 2604b88c807SRodney W. Grimes } 2614b88c807SRodney W. Grimes 2624b88c807SRodney W. Grimes /* Read error. */ 2634b88c807SRodney W. Grimes if (n < 0) { 2644b88c807SRodney W. Grimes /* 2654b88c807SRodney W. Grimes * If noerror not specified, die. POSIX requires that 2664b88c807SRodney W. Grimes * the warning message be followed by an I/O display. 2674b88c807SRodney W. Grimes */ 268af041bf3SJonathan Lemon if (!(ddflags & C_NOERROR)) 2694b88c807SRodney W. Grimes err(1, "%s", in.name); 2704b88c807SRodney W. Grimes warn("%s", in.name); 2714b88c807SRodney W. Grimes summary(); 2724b88c807SRodney W. Grimes 2734b88c807SRodney W. Grimes /* 2744b88c807SRodney W. Grimes * If it's not a tape drive or a pipe, seek past the 2754b88c807SRodney W. Grimes * error. If your OS doesn't do the right thing for 2764b88c807SRodney W. Grimes * raw disks this section should be modified to re-read 2774b88c807SRodney W. Grimes * in sector size chunks. 2784b88c807SRodney W. Grimes */ 2794b88c807SRodney W. Grimes if (!(in.flags & (ISPIPE|ISTAPE)) && 2804b88c807SRodney W. Grimes lseek(in.fd, (off_t)in.dbsz, SEEK_CUR)) 2814b88c807SRodney W. Grimes warn("%s", in.name); 2824b88c807SRodney W. Grimes 2834b88c807SRodney W. Grimes /* If sync not specified, omit block and continue. */ 2844b88c807SRodney W. Grimes if (!(ddflags & C_SYNC)) 2854b88c807SRodney W. Grimes continue; 2864b88c807SRodney W. Grimes 2874b88c807SRodney W. Grimes /* Read errors count as full blocks. */ 2884b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = in.dbsz; 2894b88c807SRodney W. Grimes ++st.in_full; 2904b88c807SRodney W. Grimes 2914b88c807SRodney W. Grimes /* Handle full input blocks. */ 2924b88c807SRodney W. Grimes } else if (n == in.dbsz) { 2934b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = n; 2944b88c807SRodney W. Grimes ++st.in_full; 2954b88c807SRodney W. Grimes 2964b88c807SRodney W. Grimes /* Handle partial input blocks. */ 2974b88c807SRodney W. Grimes } else { 2984b88c807SRodney W. Grimes /* If sync, use the entire block. */ 2994b88c807SRodney W. Grimes if (ddflags & C_SYNC) 3004b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = in.dbsz; 3014b88c807SRodney W. Grimes else 3024b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = n; 3034b88c807SRodney W. Grimes ++st.in_part; 3044b88c807SRodney W. Grimes } 3054b88c807SRodney W. Grimes 3064b88c807SRodney W. Grimes /* 3074b88c807SRodney W. Grimes * POSIX states that if bs is set and no other conversions 3084b88c807SRodney W. Grimes * than noerror, notrunc or sync are specified, the block 3094b88c807SRodney W. Grimes * is output without buffering as it is read. 3104b88c807SRodney W. Grimes */ 3114b88c807SRodney W. Grimes if (ddflags & C_BS) { 3124b88c807SRodney W. Grimes out.dbcnt = in.dbcnt; 3134b88c807SRodney W. Grimes dd_out(1); 3144b88c807SRodney W. Grimes in.dbcnt = 0; 3154b88c807SRodney W. Grimes continue; 3164b88c807SRodney W. Grimes } 3174b88c807SRodney W. Grimes 3184b88c807SRodney W. Grimes if (ddflags & C_SWAB) { 3194b88c807SRodney W. Grimes if ((n = in.dbcnt) & 1) { 3204b88c807SRodney W. Grimes ++st.swab; 3214b88c807SRodney W. Grimes --n; 3224b88c807SRodney W. Grimes } 3234b88c807SRodney W. Grimes swab(in.dbp, in.dbp, n); 3244b88c807SRodney W. Grimes } 3254b88c807SRodney W. Grimes 3264b88c807SRodney W. Grimes in.dbp += in.dbrcnt; 3274b88c807SRodney W. Grimes (*cfunc)(); 3284b88c807SRodney W. Grimes } 3294b88c807SRodney W. Grimes } 3304b88c807SRodney W. Grimes 3314b88c807SRodney W. Grimes /* 3324b88c807SRodney W. Grimes * Cleanup any remaining I/O and flush output. If necesssary, output file 3334b88c807SRodney W. Grimes * is truncated. 3344b88c807SRodney W. Grimes */ 3354b88c807SRodney W. Grimes static void 3364b88c807SRodney W. Grimes dd_close() 3374b88c807SRodney W. Grimes { 3384b88c807SRodney W. Grimes if (cfunc == def) 3394b88c807SRodney W. Grimes def_close(); 3404b88c807SRodney W. Grimes else if (cfunc == block) 3414b88c807SRodney W. Grimes block_close(); 3424b88c807SRodney W. Grimes else if (cfunc == unblock) 3434b88c807SRodney W. Grimes unblock_close(); 344af041bf3SJonathan Lemon if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) { 345af041bf3SJonathan Lemon if (ddflags & (C_BLOCK|C_UNBLOCK)) 346af041bf3SJonathan Lemon memset(out.dbp, ' ', out.dbsz - out.dbcnt); 347af041bf3SJonathan Lemon else 3484b88c807SRodney W. Grimes memset(out.dbp, 0, out.dbsz - out.dbcnt); 3494b88c807SRodney W. Grimes out.dbcnt = out.dbsz; 3504b88c807SRodney W. Grimes } 3514ddfeabdSJoerg Wunsch if (out.dbcnt || pending) 3524b88c807SRodney W. Grimes dd_out(1); 3534b88c807SRodney W. Grimes } 3544b88c807SRodney W. Grimes 3554b88c807SRodney W. Grimes void 3564b88c807SRodney W. Grimes dd_out(force) 3574b88c807SRodney W. Grimes int force; 3584b88c807SRodney W. Grimes { 3594b88c807SRodney W. Grimes static int warned; 3604ddfeabdSJoerg Wunsch int cnt, n, nw, i, sparse; 3614b88c807SRodney W. Grimes u_char *outp; 3624b88c807SRodney W. Grimes 3634b88c807SRodney W. Grimes /* 3644b88c807SRodney W. Grimes * Write one or more blocks out. The common case is writing a full 3654b88c807SRodney W. Grimes * output block in a single write; increment the full block stats. 3664b88c807SRodney W. Grimes * Otherwise, we're into partial block writes. If a partial write, 3674b88c807SRodney W. Grimes * and it's a character device, just warn. If a tape device, quit. 3684b88c807SRodney W. Grimes * 3694b88c807SRodney W. Grimes * The partial writes represent two cases. 1: Where the input block 3704b88c807SRodney W. Grimes * was less than expected so the output block was less than expected. 3714b88c807SRodney W. Grimes * 2: Where the input block was the right size but we were forced to 3724b88c807SRodney W. Grimes * write the block in multiple chunks. The original versions of dd(1) 3734b88c807SRodney W. Grimes * never wrote a block in more than a single write, so the latter case 3744b88c807SRodney W. Grimes * never happened. 3754b88c807SRodney W. Grimes * 3764b88c807SRodney W. Grimes * One special case is if we're forced to do the write -- in that case 3774b88c807SRodney W. Grimes * we play games with the buffer size, and it's usually a partial write. 3784b88c807SRodney W. Grimes */ 3794b88c807SRodney W. Grimes outp = out.db; 3804b88c807SRodney W. Grimes for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { 3814b88c807SRodney W. Grimes for (cnt = n;; cnt -= nw) { 3824ddfeabdSJoerg Wunsch sparse = 0; 3834ddfeabdSJoerg Wunsch if (ddflags & C_SPARSE) { 3844ddfeabdSJoerg Wunsch sparse = 1; /* Is buffer sparse? */ 3854ddfeabdSJoerg Wunsch for (i = 0; i < cnt; i++) 3864ddfeabdSJoerg Wunsch if (outp[i] != 0) { 3874ddfeabdSJoerg Wunsch sparse = 0; 3884ddfeabdSJoerg Wunsch break; 3894ddfeabdSJoerg Wunsch } 3904ddfeabdSJoerg Wunsch } 3914ddfeabdSJoerg Wunsch if (sparse && !force) { 3924ddfeabdSJoerg Wunsch pending += cnt; 3934ddfeabdSJoerg Wunsch nw = cnt; 3944ddfeabdSJoerg Wunsch } else { 3954ddfeabdSJoerg Wunsch if (pending != 0) { 3964ddfeabdSJoerg Wunsch if (force) 3974ddfeabdSJoerg Wunsch pending--; 3984ddfeabdSJoerg Wunsch if (lseek (out.fd, pending, SEEK_CUR) == -1) 3994ddfeabdSJoerg Wunsch err(2, "%s: seek error creating sparse file", 4004ddfeabdSJoerg Wunsch out.name); 4014ddfeabdSJoerg Wunsch if (force) 4024ddfeabdSJoerg Wunsch write(out.fd, outp, 1); 4034ddfeabdSJoerg Wunsch pending = 0; 4044ddfeabdSJoerg Wunsch } 4054ddfeabdSJoerg Wunsch if (cnt) 4064b88c807SRodney W. Grimes nw = write(out.fd, outp, cnt); 4074ddfeabdSJoerg Wunsch else 4084ddfeabdSJoerg Wunsch return; 4094ddfeabdSJoerg Wunsch } 4104ddfeabdSJoerg Wunsch 4114b88c807SRodney W. Grimes if (nw <= 0) { 4124b88c807SRodney W. Grimes if (nw == 0) 4134b88c807SRodney W. Grimes errx(1, "%s: end of device", out.name); 4144b88c807SRodney W. Grimes if (errno != EINTR) 4154b88c807SRodney W. Grimes err(1, "%s", out.name); 4164b88c807SRodney W. Grimes nw = 0; 4174b88c807SRodney W. Grimes } 4184b88c807SRodney W. Grimes outp += nw; 4194b88c807SRodney W. Grimes st.bytes += nw; 4204b88c807SRodney W. Grimes if (nw == n) { 4214b88c807SRodney W. Grimes if (n != out.dbsz) 4224b88c807SRodney W. Grimes ++st.out_part; 4234b88c807SRodney W. Grimes else 4244b88c807SRodney W. Grimes ++st.out_full; 4254b88c807SRodney W. Grimes break; 4264b88c807SRodney W. Grimes } 4274b88c807SRodney W. Grimes ++st.out_part; 4284b88c807SRodney W. Grimes if (nw == cnt) 4294b88c807SRodney W. Grimes break; 4304b88c807SRodney W. Grimes if (out.flags & ISCHR && !warned) { 4314b88c807SRodney W. Grimes warned = 1; 4324b88c807SRodney W. Grimes warnx("%s: short write on character device", 4334b88c807SRodney W. Grimes out.name); 4344b88c807SRodney W. Grimes } 4354b88c807SRodney W. Grimes if (out.flags & ISTAPE) 4364b88c807SRodney W. Grimes errx(1, "%s: short write on tape device", out.name); 4374b88c807SRodney W. Grimes } 4384b88c807SRodney W. Grimes if ((out.dbcnt -= n) < out.dbsz) 4394b88c807SRodney W. Grimes break; 4404b88c807SRodney W. Grimes } 4414b88c807SRodney W. Grimes 4424b88c807SRodney W. Grimes /* Reassemble the output block. */ 4434b88c807SRodney W. Grimes if (out.dbcnt) 4444b88c807SRodney W. Grimes memmove(out.db, out.dbp - out.dbcnt, out.dbcnt); 4454b88c807SRodney W. Grimes out.dbp = out.db + out.dbcnt; 4464b88c807SRodney W. Grimes } 447