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 394b88c807SRodney W. Grimes static char 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 454b88c807SRodney W. Grimes static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94"; 464b88c807SRodney W. Grimes #endif /* not lint */ 474b88c807SRodney W. Grimes 484b88c807SRodney W. Grimes #include <sys/param.h> 494b88c807SRodney W. Grimes #include <sys/stat.h> 504b88c807SRodney W. Grimes #include <sys/ioctl.h> 514b88c807SRodney W. Grimes #include <sys/mtio.h> 524b88c807SRodney W. Grimes 534b88c807SRodney W. Grimes #include <ctype.h> 544b88c807SRodney W. Grimes #include <err.h> 554b88c807SRodney W. Grimes #include <errno.h> 564b88c807SRodney W. Grimes #include <fcntl.h> 574b88c807SRodney W. Grimes #include <signal.h> 584b88c807SRodney W. Grimes #include <stdio.h> 594b88c807SRodney W. Grimes #include <stdlib.h> 604b88c807SRodney W. Grimes #include <string.h> 614b88c807SRodney W. Grimes #include <unistd.h> 624b88c807SRodney W. Grimes 634b88c807SRodney W. Grimes #include "dd.h" 644b88c807SRodney W. Grimes #include "extern.h" 654b88c807SRodney W. Grimes 664b88c807SRodney W. Grimes static void dd_close __P((void)); 674b88c807SRodney W. Grimes static void dd_in __P((void)); 684b88c807SRodney W. Grimes static void getfdtype __P((IO *)); 694b88c807SRodney W. Grimes static void setup __P((void)); 704b88c807SRodney W. Grimes 714b88c807SRodney W. Grimes IO in, out; /* input/output state */ 724b88c807SRodney W. Grimes STAT st; /* statistics */ 734b88c807SRodney W. Grimes void (*cfunc) __P((void)); /* conversion function */ 744b88c807SRodney W. Grimes u_long cpy_cnt; /* # of blocks to copy */ 754b88c807SRodney W. Grimes u_int ddflags; /* conversion options */ 764b88c807SRodney W. Grimes u_int cbsz; /* conversion block size */ 774b88c807SRodney W. Grimes u_int files_cnt = 1; /* # of files to copy */ 784b88c807SRodney W. Grimes u_char *ctab; /* conversion table */ 794b88c807SRodney W. Grimes 804b88c807SRodney W. Grimes int 814b88c807SRodney W. Grimes main(argc, argv) 824b88c807SRodney W. Grimes int argc; 834b88c807SRodney W. Grimes char *argv[]; 844b88c807SRodney W. Grimes { 854b88c807SRodney W. Grimes jcl(argv); 864b88c807SRodney W. Grimes setup(); 874b88c807SRodney W. Grimes 884b88c807SRodney W. Grimes (void)signal(SIGINFO, summaryx); 894b88c807SRodney W. Grimes (void)signal(SIGINT, terminate); 904b88c807SRodney W. Grimes 914b88c807SRodney W. Grimes atexit(summary); 924b88c807SRodney W. Grimes 934b88c807SRodney W. Grimes while (files_cnt--) 944b88c807SRodney W. Grimes dd_in(); 954b88c807SRodney W. Grimes 964b88c807SRodney W. Grimes dd_close(); 974b88c807SRodney W. Grimes exit(0); 984b88c807SRodney W. Grimes } 994b88c807SRodney W. Grimes 1004b88c807SRodney W. Grimes static void 1014b88c807SRodney W. Grimes setup() 1024b88c807SRodney W. Grimes { 1034b88c807SRodney W. Grimes u_int cnt; 1044b88c807SRodney W. Grimes 1054b88c807SRodney W. Grimes if (in.name == NULL) { 1064b88c807SRodney W. Grimes in.name = "stdin"; 1074b88c807SRodney W. Grimes in.fd = STDIN_FILENO; 1084b88c807SRodney W. Grimes } else { 1094b88c807SRodney W. Grimes in.fd = open(in.name, O_RDONLY, 0); 1104b88c807SRodney W. Grimes if (in.fd < 0) 1114b88c807SRodney W. Grimes err(1, "%s", in.name); 1124b88c807SRodney W. Grimes } 1134b88c807SRodney W. Grimes 1144b88c807SRodney W. Grimes getfdtype(&in); 1154b88c807SRodney W. Grimes 1164b88c807SRodney W. Grimes if (files_cnt > 1 && !(in.flags & ISTAPE)) 1174b88c807SRodney W. Grimes errx(1, "files is not supported for non-tape devices"); 1184b88c807SRodney W. Grimes 1194b88c807SRodney W. Grimes if (out.name == NULL) { 1204b88c807SRodney W. Grimes /* No way to check for read access here. */ 1214b88c807SRodney W. Grimes out.fd = STDOUT_FILENO; 1224b88c807SRodney W. Grimes out.name = "stdout"; 1234b88c807SRodney W. Grimes } else { 1244b88c807SRodney W. Grimes #define OFLAGS \ 1254b88c807SRodney W. Grimes (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) 1264b88c807SRodney W. Grimes out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE); 1274b88c807SRodney W. Grimes /* 1284b88c807SRodney W. Grimes * May not have read access, so try again with write only. 1294b88c807SRodney W. Grimes * Without read we may have a problem if output also does 1304b88c807SRodney W. Grimes * not support seeks. 1314b88c807SRodney W. Grimes */ 1324b88c807SRodney W. Grimes if (out.fd < 0) { 1334b88c807SRodney W. Grimes out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); 1344b88c807SRodney W. Grimes out.flags |= NOREAD; 1354b88c807SRodney W. Grimes } 1364b88c807SRodney W. Grimes if (out.fd < 0) 1374b88c807SRodney W. Grimes err(1, "%s", out.name); 1384b88c807SRodney W. Grimes } 1394b88c807SRodney W. Grimes 1404b88c807SRodney W. Grimes getfdtype(&out); 1414b88c807SRodney W. Grimes 1424b88c807SRodney W. Grimes /* 1434b88c807SRodney W. Grimes * Allocate space for the input and output buffers. If not doing 1444b88c807SRodney W. Grimes * record oriented I/O, only need a single buffer. 1454b88c807SRodney W. Grimes */ 1464b88c807SRodney W. Grimes if (!(ddflags & (C_BLOCK|C_UNBLOCK))) { 1474b88c807SRodney W. Grimes if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) 1484b88c807SRodney W. Grimes err(1, NULL); 1494b88c807SRodney W. Grimes out.db = in.db; 1504b88c807SRodney W. Grimes } else if ((in.db = 1514b88c807SRodney W. Grimes malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL || 1524b88c807SRodney W. Grimes (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) 1534b88c807SRodney W. Grimes err(1, NULL); 1544b88c807SRodney W. Grimes in.dbp = in.db; 1554b88c807SRodney W. Grimes out.dbp = out.db; 1564b88c807SRodney W. Grimes 1574b88c807SRodney W. Grimes /* Position the input/output streams. */ 1584b88c807SRodney W. Grimes if (in.offset) 1594b88c807SRodney W. Grimes pos_in(); 1604b88c807SRodney W. Grimes if (out.offset) 1614b88c807SRodney W. Grimes pos_out(); 1624b88c807SRodney W. Grimes 1634b88c807SRodney W. Grimes /* 1644b88c807SRodney W. Grimes * Truncate the output file; ignore errors because it fails on some 1654b88c807SRodney W. Grimes * kinds of output files, tapes, for example. 1664b88c807SRodney W. Grimes */ 1674b88c807SRodney W. Grimes if (ddflags & (C_OF | C_SEEK | C_NOTRUNC) == (C_OF | C_SEEK)) 1684b88c807SRodney W. Grimes (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz); 1694b88c807SRodney W. Grimes 1704b88c807SRodney W. Grimes /* 1714b88c807SRodney W. Grimes * If converting case at the same time as another conversion, build a 1724b88c807SRodney W. Grimes * table that does both at once. If just converting case, use the 1734b88c807SRodney W. Grimes * built-in tables. 1744b88c807SRodney W. Grimes */ 1754b88c807SRodney W. Grimes if (ddflags & (C_LCASE|C_UCASE)) 1764b88c807SRodney W. Grimes if (ddflags & C_ASCII) 1774b88c807SRodney W. Grimes if (ddflags & C_LCASE) { 1784b88c807SRodney W. Grimes for (cnt = 0; cnt < 0377; ++cnt) 1794b88c807SRodney W. Grimes if (isupper(ctab[cnt])) 1804b88c807SRodney W. Grimes ctab[cnt] = tolower(ctab[cnt]); 1814b88c807SRodney W. Grimes } else { 1824b88c807SRodney W. Grimes for (cnt = 0; cnt < 0377; ++cnt) 1834b88c807SRodney W. Grimes if (islower(ctab[cnt])) 1844b88c807SRodney W. Grimes ctab[cnt] = toupper(ctab[cnt]); 1854b88c807SRodney W. Grimes } 1864b88c807SRodney W. Grimes else if (ddflags & C_EBCDIC) 1874b88c807SRodney W. Grimes if (ddflags & C_LCASE) { 1884b88c807SRodney W. Grimes for (cnt = 0; cnt < 0377; ++cnt) 1894b88c807SRodney W. Grimes if (isupper(cnt)) 1904b88c807SRodney W. Grimes ctab[cnt] = ctab[tolower(cnt)]; 1914b88c807SRodney W. Grimes } else { 1924b88c807SRodney W. Grimes for (cnt = 0; cnt < 0377; ++cnt) 1934b88c807SRodney W. Grimes if (islower(cnt)) 1944b88c807SRodney W. Grimes ctab[cnt] = ctab[toupper(cnt)]; 1954b88c807SRodney W. Grimes } 1964b88c807SRodney W. Grimes else 1974b88c807SRodney W. Grimes ctab = ddflags & C_LCASE ? u2l : l2u; 1984b88c807SRodney W. Grimes (void)time(&st.start); /* Statistics timestamp. */ 1994b88c807SRodney W. Grimes } 2004b88c807SRodney W. Grimes 2014b88c807SRodney W. Grimes static void 2024b88c807SRodney W. Grimes getfdtype(io) 2034b88c807SRodney W. Grimes IO *io; 2044b88c807SRodney W. Grimes { 2054b88c807SRodney W. Grimes struct mtget mt; 2064b88c807SRodney W. Grimes struct stat sb; 2074b88c807SRodney W. Grimes 2084b88c807SRodney W. Grimes if (fstat(io->fd, &sb)) 2094b88c807SRodney W. Grimes err(1, "%s", io->name); 2104b88c807SRodney W. Grimes if (S_ISCHR(sb.st_mode)) 2114b88c807SRodney W. Grimes io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE; 2124b88c807SRodney W. Grimes else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) 2134b88c807SRodney W. Grimes io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */ 2144b88c807SRodney W. Grimes } 2154b88c807SRodney W. Grimes 2164b88c807SRodney W. Grimes static void 2174b88c807SRodney W. Grimes dd_in() 2184b88c807SRodney W. Grimes { 2194b88c807SRodney W. Grimes int flags, n; 2204b88c807SRodney W. Grimes 2214b88c807SRodney W. Grimes for (flags = ddflags;;) { 2224b88c807SRodney W. Grimes if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt) 2234b88c807SRodney W. Grimes return; 2244b88c807SRodney W. Grimes 2254b88c807SRodney W. Grimes /* 2264b88c807SRodney W. Grimes * Zero the buffer first if trying to recover from errors so 2274b88c807SRodney W. Grimes * lose the minimum amount of data. If doing block operations 2284b88c807SRodney W. Grimes * use spaces. 2294b88c807SRodney W. Grimes */ 2304b88c807SRodney W. Grimes if ((flags & (C_NOERROR|C_SYNC)) == (C_NOERROR|C_SYNC)) 2314b88c807SRodney W. Grimes if (flags & (C_BLOCK|C_UNBLOCK)) 2324b88c807SRodney W. Grimes memset(in.dbp, ' ', in.dbsz); 2334b88c807SRodney W. Grimes else 2344b88c807SRodney W. Grimes memset(in.dbp, 0, in.dbsz); 2354b88c807SRodney W. Grimes 2364b88c807SRodney W. Grimes n = read(in.fd, in.dbp, in.dbsz); 2374b88c807SRodney W. Grimes if (n == 0) { 2384b88c807SRodney W. Grimes in.dbrcnt = 0; 2394b88c807SRodney W. Grimes return; 2404b88c807SRodney W. Grimes } 2414b88c807SRodney W. Grimes 2424b88c807SRodney W. Grimes /* Read error. */ 2434b88c807SRodney W. Grimes if (n < 0) { 2444b88c807SRodney W. Grimes /* 2454b88c807SRodney W. Grimes * If noerror not specified, die. POSIX requires that 2464b88c807SRodney W. Grimes * the warning message be followed by an I/O display. 2474b88c807SRodney W. Grimes */ 2484b88c807SRodney W. Grimes if (!(flags & C_NOERROR)) 2494b88c807SRodney W. Grimes err(1, "%s", in.name); 2504b88c807SRodney W. Grimes warn("%s", in.name); 2514b88c807SRodney W. Grimes summary(); 2524b88c807SRodney W. Grimes 2534b88c807SRodney W. Grimes /* 2544b88c807SRodney W. Grimes * If it's not a tape drive or a pipe, seek past the 2554b88c807SRodney W. Grimes * error. If your OS doesn't do the right thing for 2564b88c807SRodney W. Grimes * raw disks this section should be modified to re-read 2574b88c807SRodney W. Grimes * in sector size chunks. 2584b88c807SRodney W. Grimes */ 2594b88c807SRodney W. Grimes if (!(in.flags & (ISPIPE|ISTAPE)) && 2604b88c807SRodney W. Grimes lseek(in.fd, (off_t)in.dbsz, SEEK_CUR)) 2614b88c807SRodney W. Grimes warn("%s", in.name); 2624b88c807SRodney W. Grimes 2634b88c807SRodney W. Grimes /* If sync not specified, omit block and continue. */ 2644b88c807SRodney W. Grimes if (!(ddflags & C_SYNC)) 2654b88c807SRodney W. Grimes continue; 2664b88c807SRodney W. Grimes 2674b88c807SRodney W. Grimes /* Read errors count as full blocks. */ 2684b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = in.dbsz; 2694b88c807SRodney W. Grimes ++st.in_full; 2704b88c807SRodney W. Grimes 2714b88c807SRodney W. Grimes /* Handle full input blocks. */ 2724b88c807SRodney W. Grimes } else if (n == in.dbsz) { 2734b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = n; 2744b88c807SRodney W. Grimes ++st.in_full; 2754b88c807SRodney W. Grimes 2764b88c807SRodney W. Grimes /* Handle partial input blocks. */ 2774b88c807SRodney W. Grimes } else { 2784b88c807SRodney W. Grimes /* If sync, use the entire block. */ 2794b88c807SRodney W. Grimes if (ddflags & C_SYNC) 2804b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = in.dbsz; 2814b88c807SRodney W. Grimes else 2824b88c807SRodney W. Grimes in.dbcnt += in.dbrcnt = n; 2834b88c807SRodney W. Grimes ++st.in_part; 2844b88c807SRodney W. Grimes } 2854b88c807SRodney W. Grimes 2864b88c807SRodney W. Grimes /* 2874b88c807SRodney W. Grimes * POSIX states that if bs is set and no other conversions 2884b88c807SRodney W. Grimes * than noerror, notrunc or sync are specified, the block 2894b88c807SRodney W. Grimes * is output without buffering as it is read. 2904b88c807SRodney W. Grimes */ 2914b88c807SRodney W. Grimes if (ddflags & C_BS) { 2924b88c807SRodney W. Grimes out.dbcnt = in.dbcnt; 2934b88c807SRodney W. Grimes dd_out(1); 2944b88c807SRodney W. Grimes in.dbcnt = 0; 2954b88c807SRodney W. Grimes continue; 2964b88c807SRodney W. Grimes } 2974b88c807SRodney W. Grimes 2984b88c807SRodney W. Grimes if (ddflags & C_SWAB) { 2994b88c807SRodney W. Grimes if ((n = in.dbcnt) & 1) { 3004b88c807SRodney W. Grimes ++st.swab; 3014b88c807SRodney W. Grimes --n; 3024b88c807SRodney W. Grimes } 3034b88c807SRodney W. Grimes swab(in.dbp, in.dbp, n); 3044b88c807SRodney W. Grimes } 3054b88c807SRodney W. Grimes 3064b88c807SRodney W. Grimes in.dbp += in.dbrcnt; 3074b88c807SRodney W. Grimes (*cfunc)(); 3084b88c807SRodney W. Grimes } 3094b88c807SRodney W. Grimes } 3104b88c807SRodney W. Grimes 3114b88c807SRodney W. Grimes /* 3124b88c807SRodney W. Grimes * Cleanup any remaining I/O and flush output. If necesssary, output file 3134b88c807SRodney W. Grimes * is truncated. 3144b88c807SRodney W. Grimes */ 3154b88c807SRodney W. Grimes static void 3164b88c807SRodney W. Grimes dd_close() 3174b88c807SRodney W. Grimes { 3184b88c807SRodney W. Grimes if (cfunc == def) 3194b88c807SRodney W. Grimes def_close(); 3204b88c807SRodney W. Grimes else if (cfunc == block) 3214b88c807SRodney W. Grimes block_close(); 3224b88c807SRodney W. Grimes else if (cfunc == unblock) 3234b88c807SRodney W. Grimes unblock_close(); 3244b88c807SRodney W. Grimes if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) { 3254b88c807SRodney W. Grimes memset(out.dbp, 0, out.dbsz - out.dbcnt); 3264b88c807SRodney W. Grimes out.dbcnt = out.dbsz; 3274b88c807SRodney W. Grimes } 3284b88c807SRodney W. Grimes if (out.dbcnt) 3294b88c807SRodney W. Grimes dd_out(1); 3304b88c807SRodney W. Grimes } 3314b88c807SRodney W. Grimes 3324b88c807SRodney W. Grimes void 3334b88c807SRodney W. Grimes dd_out(force) 3344b88c807SRodney W. Grimes int force; 3354b88c807SRodney W. Grimes { 3364b88c807SRodney W. Grimes static int warned; 3374b88c807SRodney W. Grimes int cnt, n, nw; 3384b88c807SRodney W. Grimes u_char *outp; 3394b88c807SRodney W. Grimes 3404b88c807SRodney W. Grimes /* 3414b88c807SRodney W. Grimes * Write one or more blocks out. The common case is writing a full 3424b88c807SRodney W. Grimes * output block in a single write; increment the full block stats. 3434b88c807SRodney W. Grimes * Otherwise, we're into partial block writes. If a partial write, 3444b88c807SRodney W. Grimes * and it's a character device, just warn. If a tape device, quit. 3454b88c807SRodney W. Grimes * 3464b88c807SRodney W. Grimes * The partial writes represent two cases. 1: Where the input block 3474b88c807SRodney W. Grimes * was less than expected so the output block was less than expected. 3484b88c807SRodney W. Grimes * 2: Where the input block was the right size but we were forced to 3494b88c807SRodney W. Grimes * write the block in multiple chunks. The original versions of dd(1) 3504b88c807SRodney W. Grimes * never wrote a block in more than a single write, so the latter case 3514b88c807SRodney W. Grimes * never happened. 3524b88c807SRodney W. Grimes * 3534b88c807SRodney W. Grimes * One special case is if we're forced to do the write -- in that case 3544b88c807SRodney W. Grimes * we play games with the buffer size, and it's usually a partial write. 3554b88c807SRodney W. Grimes */ 3564b88c807SRodney W. Grimes outp = out.db; 3574b88c807SRodney W. Grimes for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { 3584b88c807SRodney W. Grimes for (cnt = n;; cnt -= nw) { 3594b88c807SRodney W. Grimes nw = write(out.fd, outp, cnt); 3604b88c807SRodney W. Grimes if (nw <= 0) { 3614b88c807SRodney W. Grimes if (nw == 0) 3624b88c807SRodney W. Grimes errx(1, "%s: end of device", out.name); 3634b88c807SRodney W. Grimes if (errno != EINTR) 3644b88c807SRodney W. Grimes err(1, "%s", out.name); 3654b88c807SRodney W. Grimes nw = 0; 3664b88c807SRodney W. Grimes } 3674b88c807SRodney W. Grimes outp += nw; 3684b88c807SRodney W. Grimes st.bytes += nw; 3694b88c807SRodney W. Grimes if (nw == n) { 3704b88c807SRodney W. Grimes if (n != out.dbsz) 3714b88c807SRodney W. Grimes ++st.out_part; 3724b88c807SRodney W. Grimes else 3734b88c807SRodney W. Grimes ++st.out_full; 3744b88c807SRodney W. Grimes break; 3754b88c807SRodney W. Grimes } 3764b88c807SRodney W. Grimes ++st.out_part; 3774b88c807SRodney W. Grimes if (nw == cnt) 3784b88c807SRodney W. Grimes break; 3794b88c807SRodney W. Grimes if (out.flags & ISCHR && !warned) { 3804b88c807SRodney W. Grimes warned = 1; 3814b88c807SRodney W. Grimes warnx("%s: short write on character device", 3824b88c807SRodney W. Grimes out.name); 3834b88c807SRodney W. Grimes } 3844b88c807SRodney W. Grimes if (out.flags & ISTAPE) 3854b88c807SRodney W. Grimes errx(1, "%s: short write on tape device", out.name); 3864b88c807SRodney W. Grimes } 3874b88c807SRodney W. Grimes if ((out.dbcnt -= n) < out.dbsz) 3884b88c807SRodney W. Grimes break; 3894b88c807SRodney W. Grimes } 3904b88c807SRodney W. Grimes 3914b88c807SRodney W. Grimes /* Reassemble the output block. */ 3924b88c807SRodney W. Grimes if (out.dbcnt) 3934b88c807SRodney W. Grimes memmove(out.db, out.dbp - out.dbcnt, out.dbcnt); 3944b88c807SRodney W. Grimes out.dbp = out.db + out.dbcnt; 3954b88c807SRodney W. Grimes } 396