1*38a2b7e9Smrg /* $NetBSD: dd.c,v 1.53 2019/10/04 08:57:37 mrg Exp $ */
249f0ad86Scgd
328bef396Sglass /*-
4667b5ea1Smycroft * Copyright (c) 1991, 1993, 1994
5667b5ea1Smycroft * The Regents of the University of California. All rights reserved.
628bef396Sglass *
728bef396Sglass * This code is derived from software contributed to Berkeley by
828bef396Sglass * Keith Muller of the University of California, San Diego and Lance
928bef396Sglass * Visser of Convex Computer Corporation.
1028bef396Sglass *
1128bef396Sglass * Redistribution and use in source and binary forms, with or without
1228bef396Sglass * modification, are permitted provided that the following conditions
1328bef396Sglass * are met:
1428bef396Sglass * 1. Redistributions of source code must retain the above copyright
1528bef396Sglass * notice, this list of conditions and the following disclaimer.
1628bef396Sglass * 2. Redistributions in binary form must reproduce the above copyright
1728bef396Sglass * notice, this list of conditions and the following disclaimer in the
1828bef396Sglass * documentation and/or other materials provided with the distribution.
19b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors
2028bef396Sglass * may be used to endorse or promote products derived from this software
2128bef396Sglass * without specific prior written permission.
2228bef396Sglass *
2328bef396Sglass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2428bef396Sglass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2528bef396Sglass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2628bef396Sglass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2728bef396Sglass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2828bef396Sglass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2928bef396Sglass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3028bef396Sglass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3128bef396Sglass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3228bef396Sglass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3328bef396Sglass * SUCH DAMAGE.
3428bef396Sglass */
3528bef396Sglass
364a3a58b6Schristos #include <sys/cdefs.h>
3728bef396Sglass #ifndef lint
382fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
392fe2731dSlukem The Regents of the University of California. All rights reserved.");
4028bef396Sglass #endif /* not lint */
4128bef396Sglass
4228bef396Sglass #ifndef lint
4349f0ad86Scgd #if 0
4449f0ad86Scgd static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
4549f0ad86Scgd #else
46*38a2b7e9Smrg __RCSID("$NetBSD: dd.c,v 1.53 2019/10/04 08:57:37 mrg Exp $");
4749f0ad86Scgd #endif
4828bef396Sglass #endif /* not lint */
4928bef396Sglass
5028bef396Sglass #include <sys/param.h>
5128bef396Sglass #include <sys/stat.h>
5228bef396Sglass #include <sys/ioctl.h>
5328bef396Sglass #include <sys/mtio.h>
54aecbd4c4Sross #include <sys/time.h>
5528bef396Sglass
5628bef396Sglass #include <ctype.h>
57667b5ea1Smycroft #include <err.h>
5828bef396Sglass #include <errno.h>
5928bef396Sglass #include <fcntl.h>
609655f5c2Schristos #include <locale.h>
6128bef396Sglass #include <signal.h>
6228bef396Sglass #include <stdio.h>
6328bef396Sglass #include <stdlib.h>
6428bef396Sglass #include <string.h>
6528bef396Sglass #include <unistd.h>
6628bef396Sglass
6728bef396Sglass #include "dd.h"
6828bef396Sglass #include "extern.h"
6928bef396Sglass
708605a1aaSlukem static void dd_close(void);
718605a1aaSlukem static void dd_in(void);
728605a1aaSlukem static void getfdtype(IO *);
736b03da8bSpooka static void redup_clean_fd(IO *);
748605a1aaSlukem static void setup(void);
754d638bd5Smlelstv static void *buffer_alloc(size_t);
7628bef396Sglass
7728bef396Sglass IO in, out; /* input/output state */
7828bef396Sglass STAT st; /* statistics */
798605a1aaSlukem void (*cfunc)(void); /* conversion function */
80301fb81aSlukem uint64_t cpy_cnt; /* # of blocks to copy */
816b493373Sdbj static off_t pending = 0; /* pending seek if sparse */
8228bef396Sglass u_int ddflags; /* conversion options */
83887615afSmanu #ifdef NO_IOFLAG
84887615afSmanu #define iflag O_RDONLY
85887615afSmanu #define oflag O_CREAT
86887615afSmanu #else
87887615afSmanu u_int iflag = O_RDONLY; /* open(2) flags for input file */
88887615afSmanu u_int oflag = O_CREAT; /* open(2) flags for output file */
89887615afSmanu #endif /* NO_IOFLAG */
90301fb81aSlukem uint64_t cbsz; /* conversion block size */
9128bef396Sglass u_int files_cnt = 1; /* # of files to copy */
92b7d86f5eSapb uint64_t progress = 0; /* display sign of life */
93dba6c619Sjtc const u_char *ctab; /* conversion table */
947c30c00aSchristos sigset_t infoset; /* a set blocking SIGINFO */
958648eca2Sjym const char *msgfmt = "posix"; /* default summary() message format */
9628bef396Sglass
97275ff3e9Spooka /*
98275ff3e9Spooka * Ops for stdin/stdout and crunch'd dd. These are always host ops.
99275ff3e9Spooka */
100275ff3e9Spooka static const struct ddfops ddfops_stdfd = {
1016b03da8bSpooka .op_open = open,
1026b03da8bSpooka .op_close = close,
1036b03da8bSpooka .op_fcntl = fcntl,
1046b03da8bSpooka .op_ioctl = ioctl,
1056b03da8bSpooka .op_fstat = fstat,
1066b03da8bSpooka .op_fsync = fsync,
1076b03da8bSpooka .op_ftruncate = ftruncate,
1086b03da8bSpooka .op_lseek = lseek,
1096b03da8bSpooka .op_read = read,
1106b03da8bSpooka .op_write = write,
1116b03da8bSpooka };
112275ff3e9Spooka extern const struct ddfops ddfops_prog;
1136b03da8bSpooka
11428bef396Sglass int
main(int argc,char * argv[])1158605a1aaSlukem main(int argc, char *argv[])
11628bef396Sglass {
1175072fc9eSkleink int ch;
1185072fc9eSkleink
1199655f5c2Schristos setprogname(argv[0]);
1209655f5c2Schristos (void)setlocale(LC_ALL, "");
1219655f5c2Schristos
1225072fc9eSkleink while ((ch = getopt(argc, argv, "")) != -1) {
1235072fc9eSkleink switch (ch) {
1245072fc9eSkleink default:
1255072fc9eSkleink errx(EXIT_FAILURE, "usage: dd [operand ...]");
1265072fc9eSkleink /* NOTREACHED */
1275072fc9eSkleink }
1285072fc9eSkleink }
1295072fc9eSkleink argc -= (optind - 1);
1305072fc9eSkleink argv += (optind - 1);
1315072fc9eSkleink
13228bef396Sglass jcl(argv);
133275ff3e9Spooka #ifndef CRUNCHOPS
134275ff3e9Spooka if (ddfops_prog.op_init && ddfops_prog.op_init() == -1)
135275ff3e9Spooka err(1, "prog init");
136275ff3e9Spooka #endif
13728bef396Sglass setup();
13828bef396Sglass
139667b5ea1Smycroft (void)signal(SIGINFO, summaryx);
14028bef396Sglass (void)signal(SIGINT, terminate);
1417c30c00aSchristos (void)sigemptyset(&infoset);
1427c30c00aSchristos (void)sigaddset(&infoset, SIGINFO);
14328bef396Sglass
1441381f684Smycroft (void)atexit(summary);
145667b5ea1Smycroft
146667b5ea1Smycroft while (files_cnt--)
14728bef396Sglass dd_in();
14828bef396Sglass
14928bef396Sglass dd_close();
15028bef396Sglass exit(0);
1519dc385beSmycroft /* NOTREACHED */
15228bef396Sglass }
15328bef396Sglass
1544d638bd5Smlelstv static void *
buffer_alloc(size_t sz)1554d638bd5Smlelstv buffer_alloc(size_t sz)
1564d638bd5Smlelstv {
1574d638bd5Smlelstv size_t align = getpagesize();
1584d638bd5Smlelstv void *res;
1594d638bd5Smlelstv
1604d638bd5Smlelstv if (sz < align)
1614d638bd5Smlelstv res = malloc(sz);
1624d638bd5Smlelstv else if (posix_memalign(&res, align, sz) != 0)
1634d638bd5Smlelstv res = NULL;
1644d638bd5Smlelstv
1654d638bd5Smlelstv return res;
1664d638bd5Smlelstv }
1674d638bd5Smlelstv
16828bef396Sglass static void
setup(void)1698605a1aaSlukem setup(void)
17028bef396Sglass {
171275ff3e9Spooka #ifdef CRUNCHOPS
172275ff3e9Spooka const struct ddfops *prog_ops = &ddfops_stdfd;
173275ff3e9Spooka #else
174275ff3e9Spooka const struct ddfops *prog_ops = &ddfops_prog;
175275ff3e9Spooka #endif
176cd5b6006Senami
17728bef396Sglass if (in.name == NULL) {
17828bef396Sglass in.name = "stdin";
17928bef396Sglass in.fd = STDIN_FILENO;
180275ff3e9Spooka in.ops = &ddfops_stdfd;
18128bef396Sglass } else {
182275ff3e9Spooka in.ops = prog_ops;
183887615afSmanu in.fd = ddop_open(in, in.name, iflag, 0);
18428bef396Sglass if (in.fd < 0)
1856a75fbb6Sjschauma err(EXIT_FAILURE, "%s", in.name);
186458ed234Sjschauma /* NOTREACHED */
1870b16f710Sdsainty
1880b16f710Sdsainty /* Ensure in.fd is outside the stdio descriptor range */
1896b03da8bSpooka redup_clean_fd(&in);
19028bef396Sglass }
19128bef396Sglass
192667b5ea1Smycroft getfdtype(&in);
19328bef396Sglass
194458ed234Sjschauma if (files_cnt > 1 && !(in.flags & ISTAPE)) {
195458ed234Sjschauma errx(EXIT_FAILURE, "files is not supported for non-tape devices");
196458ed234Sjschauma /* NOTREACHED */
197458ed234Sjschauma }
19828bef396Sglass
19928bef396Sglass if (out.name == NULL) {
20028bef396Sglass /* No way to check for read access here. */
20128bef396Sglass out.fd = STDOUT_FILENO;
20228bef396Sglass out.name = "stdout";
203275ff3e9Spooka out.ops = &ddfops_stdfd;
20428bef396Sglass } else {
205275ff3e9Spooka out.ops = prog_ops;
206887615afSmanu
207887615afSmanu #ifndef NO_IOFLAG
208887615afSmanu if ((oflag & O_TRUNC) && (ddflags & C_SEEK)) {
209887615afSmanu errx(EXIT_FAILURE, "oflag=trunc is incompatible "
210887615afSmanu "with seek or oseek operands, giving up.");
211887615afSmanu /* NOTREACHED */
212887615afSmanu }
213887615afSmanu if ((oflag & O_TRUNC) && (ddflags & C_NOTRUNC)) {
214887615afSmanu errx(EXIT_FAILURE, "oflag=trunc is incompatible "
215887615afSmanu "with conv=notrunc operand, giving up.");
216887615afSmanu /* NOTREACHED */
217887615afSmanu }
218887615afSmanu #endif /* NO_IOFLAG */
21928bef396Sglass #define OFLAGS \
220887615afSmanu (oflag | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
2216b03da8bSpooka out.fd = ddop_open(out, out.name, O_RDWR | OFLAGS, DEFFILEMODE);
22228bef396Sglass /*
22328bef396Sglass * May not have read access, so try again with write only.
22428bef396Sglass * Without read we may have a problem if output also does
22528bef396Sglass * not support seeks.
22628bef396Sglass */
22728bef396Sglass if (out.fd < 0) {
2286b03da8bSpooka out.fd = ddop_open(out, out.name, O_WRONLY | OFLAGS,
2296b03da8bSpooka DEFFILEMODE);
23028bef396Sglass out.flags |= NOREAD;
23128bef396Sglass }
232458ed234Sjschauma if (out.fd < 0) {
2336a75fbb6Sjschauma err(EXIT_FAILURE, "%s", out.name);
234458ed234Sjschauma /* NOTREACHED */
235458ed234Sjschauma }
2360b16f710Sdsainty
2370b16f710Sdsainty /* Ensure out.fd is outside the stdio descriptor range */
2386b03da8bSpooka redup_clean_fd(&out);
23928bef396Sglass }
24028bef396Sglass
241667b5ea1Smycroft getfdtype(&out);
24228bef396Sglass
24328bef396Sglass /*
24428bef396Sglass * Allocate space for the input and output buffers. If not doing
24528bef396Sglass * record oriented I/O, only need a single buffer.
24628bef396Sglass */
24728bef396Sglass if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
248f71b980dSmatt size_t dbsz = out.dbsz;
249f71b980dSmatt if (!(ddflags & C_BS))
250f71b980dSmatt dbsz += in.dbsz - 1;
2514d638bd5Smlelstv if ((in.db = buffer_alloc(dbsz)) == NULL) {
252458ed234Sjschauma err(EXIT_FAILURE, NULL);
253458ed234Sjschauma /* NOTREACHED */
254458ed234Sjschauma }
25528bef396Sglass out.db = in.db;
25628bef396Sglass } else if ((in.db =
2574d638bd5Smlelstv buffer_alloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
2584d638bd5Smlelstv (out.db = buffer_alloc((u_int)(out.dbsz + cbsz))) == NULL) {
259458ed234Sjschauma err(EXIT_FAILURE, NULL);
260458ed234Sjschauma /* NOTREACHED */
261458ed234Sjschauma }
26228bef396Sglass in.dbp = in.db;
26328bef396Sglass out.dbp = out.db;
26428bef396Sglass
26528bef396Sglass /* Position the input/output streams. */
26628bef396Sglass if (in.offset)
26728bef396Sglass pos_in();
26828bef396Sglass if (out.offset)
26928bef396Sglass pos_out();
27028bef396Sglass
27128bef396Sglass /*
27228bef396Sglass * Truncate the output file; ignore errors because it fails on some
27328bef396Sglass * kinds of output files, tapes, for example.
27428bef396Sglass */
2754a3a58b6Schristos if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
2766b03da8bSpooka (void)ddop_ftruncate(out, out.fd, (off_t)out.offset * out.dbsz);
27728bef396Sglass
27828bef396Sglass /*
27928bef396Sglass * If converting case at the same time as another conversion, build a
28028bef396Sglass * table that does both at once. If just converting case, use the
28128bef396Sglass * built-in tables.
28228bef396Sglass */
283f8791fb0Sgwr if (ddflags & (C_LCASE|C_UCASE)) {
284f8791fb0Sgwr #ifdef NO_CONV
285f8791fb0Sgwr /* Should not get here, but just in case... */
286458ed234Sjschauma errx(EXIT_FAILURE, "case conv and -DNO_CONV");
287458ed234Sjschauma /* NOTREACHED */
288f8791fb0Sgwr #else /* NO_CONV */
2896848de8fSlukem u_int cnt;
2906848de8fSlukem
291dba6c619Sjtc if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
29228bef396Sglass if (ddflags & C_LCASE) {
29357fc49ddSrillig for (cnt = 0; cnt < 256; ++cnt)
294dba6c619Sjtc casetab[cnt] = tolower(ctab[cnt]);
29528bef396Sglass } else {
29657fc49ddSrillig for (cnt = 0; cnt < 256; ++cnt)
297dba6c619Sjtc casetab[cnt] = toupper(ctab[cnt]);
29828bef396Sglass }
299dba6c619Sjtc } else {
30028bef396Sglass if (ddflags & C_LCASE) {
30157fc49ddSrillig for (cnt = 0; cnt < 256; ++cnt)
302dba6c619Sjtc casetab[cnt] = tolower(cnt);
30328bef396Sglass } else {
30457fc49ddSrillig for (cnt = 0; cnt < 256; ++cnt)
305dba6c619Sjtc casetab[cnt] = toupper(cnt);
30628bef396Sglass }
307dba6c619Sjtc }
308dba6c619Sjtc
309dba6c619Sjtc ctab = casetab;
310f8791fb0Sgwr #endif /* NO_CONV */
311f8791fb0Sgwr }
312f8791fb0Sgwr
313aecbd4c4Sross (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */
31428bef396Sglass }
31528bef396Sglass
31628bef396Sglass static void
getfdtype(IO * io)3178605a1aaSlukem getfdtype(IO *io)
318667b5ea1Smycroft {
319667b5ea1Smycroft struct mtget mt;
320667b5ea1Smycroft struct stat sb;
321667b5ea1Smycroft
3226b03da8bSpooka if (io->ops->op_fstat(io->fd, &sb)) {
3236a75fbb6Sjschauma err(EXIT_FAILURE, "%s", io->name);
324458ed234Sjschauma /* NOTREACHED */
325458ed234Sjschauma }
326667b5ea1Smycroft if (S_ISCHR(sb.st_mode))
3276b03da8bSpooka io->flags |= io->ops->op_ioctl(io->fd, MTIOCGET, &mt)
3286b03da8bSpooka ? ISCHR : ISTAPE;
3296b03da8bSpooka else if (io->ops->op_lseek(io->fd, (off_t)0, SEEK_CUR) == -1
3306b03da8bSpooka && errno == ESPIPE)
331667b5ea1Smycroft io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
332667b5ea1Smycroft }
333667b5ea1Smycroft
3340b16f710Sdsainty /*
3350b16f710Sdsainty * Move the parameter file descriptor to a descriptor that is outside the
3360b16f710Sdsainty * stdio descriptor range, if necessary. This is required to avoid
3370b16f710Sdsainty * accidentally outputting completion or error messages into the
3380b16f710Sdsainty * output file that were intended for the tty.
3390b16f710Sdsainty */
3406b03da8bSpooka static void
redup_clean_fd(IO * io)3416b03da8bSpooka redup_clean_fd(IO *io)
3420b16f710Sdsainty {
3436b03da8bSpooka int fd = io->fd;
3440b16f710Sdsainty int newfd;
3450b16f710Sdsainty
3460b16f710Sdsainty if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
3470b16f710Sdsainty fd != STDERR_FILENO)
3480b16f710Sdsainty /* File descriptor is ok, return immediately. */
3496b03da8bSpooka return;
3500b16f710Sdsainty
35139973c52Sdsainty /*
35239973c52Sdsainty * 3 is the first descriptor greater than STD*_FILENO. Any
35339973c52Sdsainty * free descriptor valued 3 or above is acceptable...
35439973c52Sdsainty */
3556b03da8bSpooka newfd = io->ops->op_fcntl(fd, F_DUPFD, 3);
3560b16f710Sdsainty if (newfd < 0) {
35739973c52Sdsainty err(EXIT_FAILURE, "dupfd IO");
3580b16f710Sdsainty /* NOTREACHED */
3590b16f710Sdsainty }
3600b16f710Sdsainty
3616b03da8bSpooka io->ops->op_close(fd);
3626b03da8bSpooka io->fd = newfd;
3630b16f710Sdsainty }
3640b16f710Sdsainty
365667b5ea1Smycroft static void
dd_in(void)3668605a1aaSlukem dd_in(void)
36728bef396Sglass {
368b6ce735cSlukem int flags;
369466f2452Slukem int64_t n;
37028bef396Sglass
37128bef396Sglass for (flags = ddflags;;) {
37228bef396Sglass if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
37328bef396Sglass return;
37428bef396Sglass
37528bef396Sglass /*
376cc865e46Sgwr * Clear the buffer first if doing "sync" on input.
377cc865e46Sgwr * If doing block operations use spaces. This will
378cc865e46Sgwr * affect not only the C_NOERROR case, but also the
379cc865e46Sgwr * last partial input block which should be padded
380cc865e46Sgwr * with zero and not garbage.
38128bef396Sglass */
382341bd18bSthorpej if (flags & C_SYNC) {
38328bef396Sglass if (flags & (C_BLOCK|C_UNBLOCK))
3841381f684Smycroft (void)memset(in.dbp, ' ', in.dbsz);
38528bef396Sglass else
3861381f684Smycroft (void)memset(in.dbp, 0, in.dbsz);
387341bd18bSthorpej }
38828bef396Sglass
3896b03da8bSpooka n = ddop_read(in, in.fd, in.dbp, in.dbsz);
39028bef396Sglass if (n == 0) {
39128bef396Sglass in.dbrcnt = 0;
39228bef396Sglass return;
39328bef396Sglass }
39428bef396Sglass
39528bef396Sglass /* Read error. */
39628bef396Sglass if (n < 0) {
397458ed234Sjschauma
39828bef396Sglass /*
39928bef396Sglass * If noerror not specified, die. POSIX requires that
40028bef396Sglass * the warning message be followed by an I/O display.
40128bef396Sglass */
402458ed234Sjschauma if (!(flags & C_NOERROR)) {
4036a75fbb6Sjschauma err(EXIT_FAILURE, "%s", in.name);
404458ed234Sjschauma /* NOTREACHED */
405458ed234Sjschauma }
4066a75fbb6Sjschauma warn("%s", in.name);
407667b5ea1Smycroft summary();
40828bef396Sglass
40928bef396Sglass /*
41028bef396Sglass * If it's not a tape drive or a pipe, seek past the
41128bef396Sglass * error. If your OS doesn't do the right thing for
41228bef396Sglass * raw disks this section should be modified to re-read
41328bef396Sglass * in sector size chunks.
41428bef396Sglass */
41528bef396Sglass if (!(in.flags & (ISPIPE|ISTAPE)) &&
4166b03da8bSpooka ddop_lseek(in, in.fd, (off_t)in.dbsz, SEEK_CUR))
4176a75fbb6Sjschauma warn("%s", in.name);
41839ff49f1Sjschauma
41928bef396Sglass /* If sync not specified, omit block and continue. */
42028bef396Sglass if (!(ddflags & C_SYNC))
42128bef396Sglass continue;
42228bef396Sglass
42328bef396Sglass /* Read errors count as full blocks. */
42428bef396Sglass in.dbcnt += in.dbrcnt = in.dbsz;
42528bef396Sglass ++st.in_full;
42628bef396Sglass
42728bef396Sglass /* Handle full input blocks. */
4289050ab5cSlukem } else if ((uint64_t)n == in.dbsz) {
42928bef396Sglass in.dbcnt += in.dbrcnt = n;
43028bef396Sglass ++st.in_full;
43128bef396Sglass
43228bef396Sglass /* Handle partial input blocks. */
43328bef396Sglass } else {
43428bef396Sglass /* If sync, use the entire block. */
43528bef396Sglass if (ddflags & C_SYNC)
43628bef396Sglass in.dbcnt += in.dbrcnt = in.dbsz;
43728bef396Sglass else
43828bef396Sglass in.dbcnt += in.dbrcnt = n;
43928bef396Sglass ++st.in_part;
44028bef396Sglass }
44128bef396Sglass
44228bef396Sglass /*
44328bef396Sglass * POSIX states that if bs is set and no other conversions
44428bef396Sglass * than noerror, notrunc or sync are specified, the block
44528bef396Sglass * is output without buffering as it is read.
44628bef396Sglass */
44728bef396Sglass if (ddflags & C_BS) {
44828bef396Sglass out.dbcnt = in.dbcnt;
44928bef396Sglass dd_out(1);
45028bef396Sglass in.dbcnt = 0;
45128bef396Sglass continue;
45228bef396Sglass }
45328bef396Sglass
45428bef396Sglass if (ddflags & C_SWAB) {
45554d55aa1Smatt if ((n = in.dbrcnt) & 1) {
45628bef396Sglass ++st.swab;
45728bef396Sglass --n;
45828bef396Sglass }
459*38a2b7e9Smrg dd_swab(in.dbp, in.dbp, n);
46028bef396Sglass }
46128bef396Sglass
46228bef396Sglass in.dbp += in.dbrcnt;
46328bef396Sglass (*cfunc)();
46428bef396Sglass }
46528bef396Sglass }
46628bef396Sglass
46728bef396Sglass /*
4688ce1f4ffSmsaitoh * Cleanup any remaining I/O and flush output. If necessary, output file
46928bef396Sglass * is truncated.
47028bef396Sglass */
47128bef396Sglass static void
dd_close(void)4728605a1aaSlukem dd_close(void)
47328bef396Sglass {
4748605a1aaSlukem
47528bef396Sglass if (cfunc == def)
47628bef396Sglass def_close();
47728bef396Sglass else if (cfunc == block)
47828bef396Sglass block_close();
47928bef396Sglass else if (cfunc == unblock)
48028bef396Sglass unblock_close();
481667b5ea1Smycroft if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
4821381f684Smycroft (void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
483667b5ea1Smycroft out.dbcnt = out.dbsz;
484667b5ea1Smycroft }
4856b493373Sdbj /* If there are pending sparse blocks, make sure
4866b493373Sdbj * to write out the final block un-sparse
4876b493373Sdbj */
4886b493373Sdbj if ((out.dbcnt == 0) && pending) {
4896b493373Sdbj memset(out.db, 0, out.dbsz);
4906b493373Sdbj out.dbcnt = out.dbsz;
491f01a4aa2Sdbj out.dbp = out.db + out.dbcnt;
4926b493373Sdbj pending -= out.dbsz;
4936b493373Sdbj }
49428bef396Sglass if (out.dbcnt)
49528bef396Sglass dd_out(1);
496caa54090Senami
497caa54090Senami /*
498b1337647Sriz * Reporting nfs write error may be deferred until next
499caa54090Senami * write(2) or close(2) system call. So, we need to do an
500caa54090Senami * extra check. If an output is stdout, the file structure
501b1337647Sriz * may be shared with other processes and close(2) just
502caa54090Senami * decreases the reference count.
503caa54090Senami */
5046b03da8bSpooka if (out.fd == STDOUT_FILENO && ddop_fsync(out, out.fd) == -1
5056b03da8bSpooka && errno != EINVAL) {
506458ed234Sjschauma err(EXIT_FAILURE, "fsync stdout");
507458ed234Sjschauma /* NOTREACHED */
508458ed234Sjschauma }
5096b03da8bSpooka if (ddop_close(out, out.fd) == -1) {
510458ed234Sjschauma err(EXIT_FAILURE, "close");
511458ed234Sjschauma /* NOTREACHED */
512458ed234Sjschauma }
51328bef396Sglass }
51428bef396Sglass
51528bef396Sglass void
dd_out(int force)5168605a1aaSlukem dd_out(int force)
51728bef396Sglass {
51828bef396Sglass static int warned;
519466f2452Slukem int64_t cnt, n, nw;
520667b5ea1Smycroft u_char *outp;
52128bef396Sglass
52228bef396Sglass /*
52328bef396Sglass * Write one or more blocks out. The common case is writing a full
52428bef396Sglass * output block in a single write; increment the full block stats.
52528bef396Sglass * Otherwise, we're into partial block writes. If a partial write,
52628bef396Sglass * and it's a character device, just warn. If a tape device, quit.
52728bef396Sglass *
52828bef396Sglass * The partial writes represent two cases. 1: Where the input block
52928bef396Sglass * was less than expected so the output block was less than expected.
53028bef396Sglass * 2: Where the input block was the right size but we were forced to
53128bef396Sglass * write the block in multiple chunks. The original versions of dd(1)
53228bef396Sglass * never wrote a block in more than a single write, so the latter case
53328bef396Sglass * never happened.
53428bef396Sglass *
53528bef396Sglass * One special case is if we're forced to do the write -- in that case
53628bef396Sglass * we play games with the buffer size, and it's usually a partial write.
53728bef396Sglass */
53828bef396Sglass outp = out.db;
53928bef396Sglass for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
54028bef396Sglass for (cnt = n;; cnt -= nw) {
541458ed234Sjschauma
5426b493373Sdbj if (!force && ddflags & C_SPARSE) {
5436b493373Sdbj int sparse, i;
5446b493373Sdbj sparse = 1; /* Is buffer sparse? */
5456b493373Sdbj for (i = 0; i < cnt; i++)
5466b493373Sdbj if (outp[i] != 0) {
5476b493373Sdbj sparse = 0;
5486b493373Sdbj break;
5496b493373Sdbj }
5506b493373Sdbj if (sparse) {
5516b493373Sdbj pending += cnt;
5526b493373Sdbj outp += cnt;
5536b493373Sdbj nw = 0;
5546b493373Sdbj break;
5556b493373Sdbj }
5566b493373Sdbj }
5576b493373Sdbj if (pending != 0) {
5586b03da8bSpooka if (ddop_lseek(out,
5596b03da8bSpooka out.fd, pending, SEEK_CUR) == -1)
5606b493373Sdbj err(EXIT_FAILURE, "%s: seek error creating sparse file",
5616b493373Sdbj out.name);
5626b493373Sdbj }
5636b03da8bSpooka nw = bwrite(&out, outp, cnt);
564667b5ea1Smycroft if (nw <= 0) {
565667b5ea1Smycroft if (nw == 0)
566458ed234Sjschauma errx(EXIT_FAILURE,
5676a75fbb6Sjschauma "%s: end of device", out.name);
568458ed234Sjschauma /* NOTREACHED */
569667b5ea1Smycroft if (errno != EINTR)
5706a75fbb6Sjschauma err(EXIT_FAILURE, "%s", out.name);
571458ed234Sjschauma /* NOTREACHED */
572667b5ea1Smycroft nw = 0;
573667b5ea1Smycroft }
5746b493373Sdbj if (pending) {
5756b493373Sdbj st.bytes += pending;
5766b493373Sdbj st.sparse += pending/out.dbsz;
5776b493373Sdbj st.out_full += pending/out.dbsz;
5786b493373Sdbj pending = 0;
5796b493373Sdbj }
58028bef396Sglass outp += nw;
58128bef396Sglass st.bytes += nw;
58228bef396Sglass if (nw == n) {
5839050ab5cSlukem if ((uint64_t)n != out.dbsz)
58428bef396Sglass ++st.out_part;
58528bef396Sglass else
58628bef396Sglass ++st.out_full;
58728bef396Sglass break;
58828bef396Sglass }
58928bef396Sglass ++st.out_part;
5906a75fbb6Sjschauma if (nw == cnt)
59128bef396Sglass break;
59228bef396Sglass if (out.flags & ISCHR && !warned) {
59328bef396Sglass warned = 1;
5946a75fbb6Sjschauma warnx("%s: short write on character device", out.name);
59528bef396Sglass }
59628bef396Sglass if (out.flags & ISTAPE)
597458ed234Sjschauma errx(EXIT_FAILURE,
5986a75fbb6Sjschauma "%s: short write on tape device", out.name);
599458ed234Sjschauma /* NOTREACHED */
600458ed234Sjschauma
60128bef396Sglass }
60228bef396Sglass if ((out.dbcnt -= n) < out.dbsz)
60328bef396Sglass break;
60428bef396Sglass }
60528bef396Sglass
60628bef396Sglass /* Reassemble the output block. */
60728bef396Sglass if (out.dbcnt)
6081381f684Smycroft (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
60928bef396Sglass out.dbp = out.db + out.dbcnt;
6104d486ab6Shubertf
611b7d86f5eSapb if (progress && (st.out_full + st.out_part) % progress == 0)
6124d486ab6Shubertf (void)write(STDERR_FILENO, ".", 1);
61328bef396Sglass }
61480dd95bbSchristos
61580dd95bbSchristos /*
61680dd95bbSchristos * A protected against SIGINFO write
61780dd95bbSchristos */
61880dd95bbSchristos ssize_t
bwrite(IO * io,const void * buf,size_t len)6196b03da8bSpooka bwrite(IO *io, const void *buf, size_t len)
62080dd95bbSchristos {
62180dd95bbSchristos sigset_t oset;
62280dd95bbSchristos ssize_t rv;
62380dd95bbSchristos int oerrno;
62480dd95bbSchristos
6257c30c00aSchristos (void)sigprocmask(SIG_BLOCK, &infoset, &oset);
6266b03da8bSpooka rv = io->ops->op_write(io->fd, buf, len);
62780dd95bbSchristos oerrno = errno;
62880dd95bbSchristos (void)sigprocmask(SIG_SETMASK, &oset, NULL);
62980dd95bbSchristos errno = oerrno;
630cd5b6006Senami return (rv);
63180dd95bbSchristos }
632