Remove building with NOCRYPTO option
[minix.git] / bin / dd / dd.c
blobc4fb1e87b9fbf123d92eb6a08f97262577e9bb43
1 /* $NetBSD: dd.c,v 1.51 2016/09/05 01:00:07 sevan Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
39 The Regents of the University of California. All rights reserved.");
40 #endif /* not lint */
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
45 #else
46 __RCSID("$NetBSD: dd.c,v 1.51 2016/09/05 01:00:07 sevan Exp $");
47 #endif
48 #endif /* not lint */
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53 #include <sys/mtio.h>
54 #include <sys/time.h>
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <locale.h>
61 #include <signal.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
67 #include "dd.h"
68 #include "extern.h"
70 static void dd_close(void);
71 static void dd_in(void);
72 static void getfdtype(IO *);
73 static void redup_clean_fd(IO *);
74 static void setup(void);
76 IO in, out; /* input/output state */
77 STAT st; /* statistics */
78 void (*cfunc)(void); /* conversion function */
79 uint64_t cpy_cnt; /* # of blocks to copy */
80 static off_t pending = 0; /* pending seek if sparse */
81 u_int ddflags; /* conversion options */
82 #ifdef NO_IOFLAG
83 #define iflag O_RDONLY
84 #define oflag O_CREAT
85 #else
86 u_int iflag = O_RDONLY; /* open(2) flags for input file */
87 u_int oflag = O_CREAT; /* open(2) flags for output file */
88 #endif /* NO_IOFLAG */
89 uint64_t cbsz; /* conversion block size */
90 u_int files_cnt = 1; /* # of files to copy */
91 uint64_t progress = 0; /* display sign of life */
92 const u_char *ctab; /* conversion table */
93 sigset_t infoset; /* a set blocking SIGINFO */
94 const char *msgfmt = "posix"; /* default summary() message format */
97 * Ops for stdin/stdout and crunch'd dd. These are always host ops.
99 static const struct ddfops ddfops_stdfd = {
100 .op_open = open,
101 .op_close = close,
102 .op_fcntl = fcntl,
103 .op_ioctl = ioctl,
104 .op_fstat = fstat,
105 .op_fsync = fsync,
106 .op_ftruncate = ftruncate,
107 .op_lseek = lseek,
108 .op_read = read,
109 .op_write = write,
111 extern const struct ddfops ddfops_prog;
114 main(int argc, char *argv[])
116 int ch;
118 setprogname(argv[0]);
119 (void)setlocale(LC_ALL, "");
121 while ((ch = getopt(argc, argv, "")) != -1) {
122 switch (ch) {
123 default:
124 errx(EXIT_FAILURE, "usage: dd [operand ...]");
125 /* NOTREACHED */
128 argc -= (optind - 1);
129 argv += (optind - 1);
131 jcl(argv);
132 #ifndef CRUNCHOPS
133 if (ddfops_prog.op_init && ddfops_prog.op_init() == -1)
134 err(1, "prog init");
135 #endif
136 setup();
138 (void)signal(SIGINFO, summaryx);
139 (void)signal(SIGINT, terminate);
140 (void)sigemptyset(&infoset);
141 (void)sigaddset(&infoset, SIGINFO);
143 (void)atexit(summary);
145 while (files_cnt--)
146 dd_in();
148 dd_close();
149 exit(0);
150 /* NOTREACHED */
153 static void
154 setup(void)
156 #ifdef CRUNCHOPS
157 const struct ddfops *prog_ops = &ddfops_stdfd;
158 #else
159 const struct ddfops *prog_ops = &ddfops_prog;
160 #endif
162 if (in.name == NULL) {
163 in.name = "stdin";
164 in.fd = STDIN_FILENO;
165 in.ops = &ddfops_stdfd;
166 } else {
167 in.ops = prog_ops;
168 in.fd = ddop_open(in, in.name, iflag, 0);
169 if (in.fd < 0)
170 err(EXIT_FAILURE, "%s", in.name);
171 /* NOTREACHED */
173 /* Ensure in.fd is outside the stdio descriptor range */
174 redup_clean_fd(&in);
177 getfdtype(&in);
179 if (files_cnt > 1 && !(in.flags & ISTAPE)) {
180 errx(EXIT_FAILURE, "files is not supported for non-tape devices");
181 /* NOTREACHED */
184 if (out.name == NULL) {
185 /* No way to check for read access here. */
186 out.fd = STDOUT_FILENO;
187 out.name = "stdout";
188 out.ops = &ddfops_stdfd;
189 } else {
190 out.ops = prog_ops;
192 #ifndef NO_IOFLAG
193 if ((oflag & O_TRUNC) && (ddflags & C_SEEK)) {
194 errx(EXIT_FAILURE, "oflag=trunc is incompatible "
195 "with seek or oseek operands, giving up.");
196 /* NOTREACHED */
198 if ((oflag & O_TRUNC) && (ddflags & C_NOTRUNC)) {
199 errx(EXIT_FAILURE, "oflag=trunc is incompatible "
200 "with conv=notrunc operand, giving up.");
201 /* NOTREACHED */
203 #endif /* NO_IOFLAG */
204 #define OFLAGS \
205 (oflag | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
206 out.fd = ddop_open(out, out.name, O_RDWR | OFLAGS, DEFFILEMODE);
208 * May not have read access, so try again with write only.
209 * Without read we may have a problem if output also does
210 * not support seeks.
212 if (out.fd < 0) {
213 out.fd = ddop_open(out, out.name, O_WRONLY | OFLAGS,
214 DEFFILEMODE);
215 out.flags |= NOREAD;
217 if (out.fd < 0) {
218 err(EXIT_FAILURE, "%s", out.name);
219 /* NOTREACHED */
222 /* Ensure out.fd is outside the stdio descriptor range */
223 redup_clean_fd(&out);
226 getfdtype(&out);
229 * Allocate space for the input and output buffers. If not doing
230 * record oriented I/O, only need a single buffer.
232 if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
233 size_t dbsz = out.dbsz;
234 if (!(ddflags & C_BS))
235 dbsz += in.dbsz - 1;
236 if ((in.db = malloc(dbsz)) == NULL) {
237 err(EXIT_FAILURE, NULL);
238 /* NOTREACHED */
240 out.db = in.db;
241 } else if ((in.db =
242 malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
243 (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
244 err(EXIT_FAILURE, NULL);
245 /* NOTREACHED */
247 in.dbp = in.db;
248 out.dbp = out.db;
250 /* Position the input/output streams. */
251 if (in.offset)
252 pos_in();
253 if (out.offset)
254 pos_out();
257 * Truncate the output file; ignore errors because it fails on some
258 * kinds of output files, tapes, for example.
260 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
261 (void)ddop_ftruncate(out, out.fd, (off_t)out.offset * out.dbsz);
264 * If converting case at the same time as another conversion, build a
265 * table that does both at once. If just converting case, use the
266 * built-in tables.
268 if (ddflags & (C_LCASE|C_UCASE)) {
269 #ifdef NO_CONV
270 /* Should not get here, but just in case... */
271 errx(EXIT_FAILURE, "case conv and -DNO_CONV");
272 /* NOTREACHED */
273 #else /* NO_CONV */
274 u_int cnt;
276 if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
277 if (ddflags & C_LCASE) {
278 for (cnt = 0; cnt < 256; ++cnt)
279 casetab[cnt] = tolower(ctab[cnt]);
280 } else {
281 for (cnt = 0; cnt < 256; ++cnt)
282 casetab[cnt] = toupper(ctab[cnt]);
284 } else {
285 if (ddflags & C_LCASE) {
286 for (cnt = 0; cnt < 256; ++cnt)
287 casetab[cnt] = tolower(cnt);
288 } else {
289 for (cnt = 0; cnt < 256; ++cnt)
290 casetab[cnt] = toupper(cnt);
294 ctab = casetab;
295 #endif /* NO_CONV */
298 (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */
301 static void
302 getfdtype(IO *io)
304 struct mtget mt;
305 struct stat sb;
307 if (io->ops->op_fstat(io->fd, &sb)) {
308 err(EXIT_FAILURE, "%s", io->name);
309 /* NOTREACHED */
311 if (S_ISCHR(sb.st_mode))
312 io->flags |= io->ops->op_ioctl(io->fd, MTIOCGET, &mt)
313 ? ISCHR : ISTAPE;
314 else if (io->ops->op_lseek(io->fd, (off_t)0, SEEK_CUR) == -1
315 && errno == ESPIPE)
316 io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
320 * Move the parameter file descriptor to a descriptor that is outside the
321 * stdio descriptor range, if necessary. This is required to avoid
322 * accidentally outputting completion or error messages into the
323 * output file that were intended for the tty.
325 static void
326 redup_clean_fd(IO *io)
328 int fd = io->fd;
329 int newfd;
331 if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
332 fd != STDERR_FILENO)
333 /* File descriptor is ok, return immediately. */
334 return;
337 * 3 is the first descriptor greater than STD*_FILENO. Any
338 * free descriptor valued 3 or above is acceptable...
340 newfd = io->ops->op_fcntl(fd, F_DUPFD, 3);
341 if (newfd < 0) {
342 err(EXIT_FAILURE, "dupfd IO");
343 /* NOTREACHED */
346 io->ops->op_close(fd);
347 io->fd = newfd;
350 static void
351 dd_in(void)
353 int flags;
354 int64_t n;
356 for (flags = ddflags;;) {
357 if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
358 return;
361 * Clear the buffer first if doing "sync" on input.
362 * If doing block operations use spaces. This will
363 * affect not only the C_NOERROR case, but also the
364 * last partial input block which should be padded
365 * with zero and not garbage.
367 if (flags & C_SYNC) {
368 if (flags & (C_BLOCK|C_UNBLOCK))
369 (void)memset(in.dbp, ' ', in.dbsz);
370 else
371 (void)memset(in.dbp, 0, in.dbsz);
374 n = ddop_read(in, in.fd, in.dbp, in.dbsz);
375 if (n == 0) {
376 in.dbrcnt = 0;
377 return;
380 /* Read error. */
381 if (n < 0) {
384 * If noerror not specified, die. POSIX requires that
385 * the warning message be followed by an I/O display.
387 if (!(flags & C_NOERROR)) {
388 err(EXIT_FAILURE, "%s", in.name);
389 /* NOTREACHED */
391 warn("%s", in.name);
392 summary();
395 * If it's not a tape drive or a pipe, seek past the
396 * error. If your OS doesn't do the right thing for
397 * raw disks this section should be modified to re-read
398 * in sector size chunks.
400 if (!(in.flags & (ISPIPE|ISTAPE)) &&
401 ddop_lseek(in, in.fd, (off_t)in.dbsz, SEEK_CUR))
402 warn("%s", in.name);
404 /* If sync not specified, omit block and continue. */
405 if (!(ddflags & C_SYNC))
406 continue;
408 /* Read errors count as full blocks. */
409 in.dbcnt += in.dbrcnt = in.dbsz;
410 ++st.in_full;
412 /* Handle full input blocks. */
413 } else if ((uint64_t)n == in.dbsz) {
414 in.dbcnt += in.dbrcnt = n;
415 ++st.in_full;
417 /* Handle partial input blocks. */
418 } else {
419 /* If sync, use the entire block. */
420 if (ddflags & C_SYNC)
421 in.dbcnt += in.dbrcnt = in.dbsz;
422 else
423 in.dbcnt += in.dbrcnt = n;
424 ++st.in_part;
428 * POSIX states that if bs is set and no other conversions
429 * than noerror, notrunc or sync are specified, the block
430 * is output without buffering as it is read.
432 if (ddflags & C_BS) {
433 out.dbcnt = in.dbcnt;
434 dd_out(1);
435 in.dbcnt = 0;
436 continue;
439 if (ddflags & C_SWAB) {
440 if ((n = in.dbrcnt) & 1) {
441 ++st.swab;
442 --n;
444 swab(in.dbp, in.dbp, n);
447 in.dbp += in.dbrcnt;
448 (*cfunc)();
453 * Cleanup any remaining I/O and flush output. If necessary, output file
454 * is truncated.
456 static void
457 dd_close(void)
460 if (cfunc == def)
461 def_close();
462 else if (cfunc == block)
463 block_close();
464 else if (cfunc == unblock)
465 unblock_close();
466 if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
467 (void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
468 out.dbcnt = out.dbsz;
470 /* If there are pending sparse blocks, make sure
471 * to write out the final block un-sparse
473 if ((out.dbcnt == 0) && pending) {
474 memset(out.db, 0, out.dbsz);
475 out.dbcnt = out.dbsz;
476 out.dbp = out.db + out.dbcnt;
477 pending -= out.dbsz;
479 if (out.dbcnt)
480 dd_out(1);
483 * Reporting nfs write error may be deferred until next
484 * write(2) or close(2) system call. So, we need to do an
485 * extra check. If an output is stdout, the file structure
486 * may be shared with other processes and close(2) just
487 * decreases the reference count.
489 if (out.fd == STDOUT_FILENO && ddop_fsync(out, out.fd) == -1
490 && errno != EINVAL) {
491 err(EXIT_FAILURE, "fsync stdout");
492 /* NOTREACHED */
494 if (ddop_close(out, out.fd) == -1) {
495 err(EXIT_FAILURE, "close");
496 /* NOTREACHED */
500 void
501 dd_out(int force)
503 static int warned;
504 int64_t cnt, n, nw;
505 u_char *outp;
508 * Write one or more blocks out. The common case is writing a full
509 * output block in a single write; increment the full block stats.
510 * Otherwise, we're into partial block writes. If a partial write,
511 * and it's a character device, just warn. If a tape device, quit.
513 * The partial writes represent two cases. 1: Where the input block
514 * was less than expected so the output block was less than expected.
515 * 2: Where the input block was the right size but we were forced to
516 * write the block in multiple chunks. The original versions of dd(1)
517 * never wrote a block in more than a single write, so the latter case
518 * never happened.
520 * One special case is if we're forced to do the write -- in that case
521 * we play games with the buffer size, and it's usually a partial write.
523 outp = out.db;
524 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
525 for (cnt = n;; cnt -= nw) {
527 if (!force && ddflags & C_SPARSE) {
528 int sparse, i;
529 sparse = 1; /* Is buffer sparse? */
530 for (i = 0; i < cnt; i++)
531 if (outp[i] != 0) {
532 sparse = 0;
533 break;
535 if (sparse) {
536 pending += cnt;
537 outp += cnt;
538 nw = 0;
539 break;
542 if (pending != 0) {
543 if (ddop_lseek(out,
544 out.fd, pending, SEEK_CUR) == -1)
545 err(EXIT_FAILURE, "%s: seek error creating sparse file",
546 out.name);
548 nw = bwrite(&out, outp, cnt);
549 if (nw <= 0) {
550 if (nw == 0)
551 errx(EXIT_FAILURE,
552 "%s: end of device", out.name);
553 /* NOTREACHED */
554 if (errno != EINTR)
555 err(EXIT_FAILURE, "%s", out.name);
556 /* NOTREACHED */
557 nw = 0;
559 if (pending) {
560 st.bytes += pending;
561 st.sparse += pending/out.dbsz;
562 st.out_full += pending/out.dbsz;
563 pending = 0;
565 outp += nw;
566 st.bytes += nw;
567 if (nw == n) {
568 if ((uint64_t)n != out.dbsz)
569 ++st.out_part;
570 else
571 ++st.out_full;
572 break;
574 ++st.out_part;
575 if (nw == cnt)
576 break;
577 if (out.flags & ISCHR && !warned) {
578 warned = 1;
579 warnx("%s: short write on character device", out.name);
581 if (out.flags & ISTAPE)
582 errx(EXIT_FAILURE,
583 "%s: short write on tape device", out.name);
584 /* NOTREACHED */
587 if ((out.dbcnt -= n) < out.dbsz)
588 break;
591 /* Reassemble the output block. */
592 if (out.dbcnt)
593 (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
594 out.dbp = out.db + out.dbcnt;
596 if (progress && (st.out_full + st.out_part) % progress == 0)
597 (void)write(STDERR_FILENO, ".", 1);
601 * A protected against SIGINFO write
603 ssize_t
604 bwrite(IO *io, const void *buf, size_t len)
606 sigset_t oset;
607 ssize_t rv;
608 int oerrno;
610 (void)sigprocmask(SIG_BLOCK, &infoset, &oset);
611 rv = io->ops->op_write(io->fd, buf, len);
612 oerrno = errno;
613 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
614 errno = oerrno;
615 return (rv);