2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char const copyright[] =
37 "@(#) Copyright (c) 1991, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
51 #include <sys/disklabel.h>
52 #include <sys/filio.h>
70 static void dd_close(void);
71 static void dd_in(void);
72 static void getfdtype(IO *);
73 static void setup(void);
75 IO in, out; /* input/output state */
76 STAT st; /* statistics */
77 void (*cfunc)(void); /* conversion function */
78 uintmax_t cpy_cnt; /* # of blocks to copy */
79 static off_t pending = 0; /* pending seek if sparse */
80 u_int ddflags = 0; /* conversion options */
81 size_t cbsz; /* conversion block size */
82 uintmax_t files_cnt = 1; /* # of files to copy */
83 const u_char *ctab; /* conversion table */
84 char fill_char; /* Character to fill with if defined */
85 volatile sig_atomic_t need_summary;
88 main(int argc __unused, char *argv[])
90 (void)setlocale(LC_CTYPE, "");
94 (void)signal(SIGINFO, siginfo_handler);
95 (void)signal(SIGINT, terminate);
104 * Some devices such as cfi(4) may perform significant amounts
105 * of work when a write descriptor is closed. Close the out
106 * descriptor explicitly so that the summary handler (called
107 * from an atexit() hook) includes this work.
118 i = c ^ (c >> 1) ^ (c >> 2) ^ (c >> 3) ^
119 (c >> 4) ^ (c >> 5) ^ (c >> 6) ^ (c >> 7);
129 if (in.name == NULL) {
131 in.fd = STDIN_FILENO;
133 in.fd = open(in.name, O_RDONLY, 0);
135 err(1, "%s", in.name);
140 if (files_cnt > 1 && !(in.flags & ISTAPE))
141 errx(1, "files is not supported for non-tape devices");
143 if (out.name == NULL) {
144 /* No way to check for read access here. */
145 out.fd = STDOUT_FILENO;
149 (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
150 out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
152 * May not have read access, so try again with write only.
153 * Without read we may have a problem if output also does
157 out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
161 err(1, "%s", out.name);
167 * Allocate space for the input and output buffers. If not doing
168 * record oriented I/O, only need a single buffer.
170 if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
171 if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
172 err(1, "input buffer");
174 } else if ((in.db = malloc(MAX(in.dbsz, cbsz) + cbsz)) == NULL ||
175 (out.db = malloc(out.dbsz + cbsz)) == NULL)
176 err(1, "output buffer");
178 /* dbp is the first free position in each buffer. */
182 /* Position the input/output streams. */
189 * Truncate the output file. If it fails on a type of output file
190 * that it should _not_ fail on, error out.
192 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
194 if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
195 err(1, "truncating %s", out.name);
197 if (ddflags & (C_LCASE | C_UCASE | C_ASCII | C_EBCDIC | C_PARITY)) {
199 for (cnt = 0; cnt <= 0377; ++cnt)
200 casetab[cnt] = ctab[cnt];
202 for (cnt = 0; cnt <= 0377; ++cnt)
205 if ((ddflags & C_PARITY) && !(ddflags & C_ASCII)) {
207 * If the input is not EBCDIC, and we do parity
208 * processing, strip input parity.
210 for (cnt = 200; cnt <= 0377; ++cnt)
211 casetab[cnt] = casetab[cnt & 0x7f];
213 if (ddflags & C_LCASE) {
214 for (cnt = 0; cnt <= 0377; ++cnt)
215 casetab[cnt] = tolower(casetab[cnt]);
216 } else if (ddflags & C_UCASE) {
217 for (cnt = 0; cnt <= 0377; ++cnt)
218 casetab[cnt] = toupper(casetab[cnt]);
220 if ((ddflags & C_PARITY)) {
222 * This should strictly speaking be a no-op, but I
223 * wonder what funny LANG settings could get us.
225 for (cnt = 0; cnt <= 0377; ++cnt)
226 casetab[cnt] = casetab[cnt] & 0x7f;
228 if ((ddflags & C_PARSET)) {
229 for (cnt = 0; cnt <= 0377; ++cnt)
230 casetab[cnt] = casetab[cnt] | 0x80;
232 if ((ddflags & C_PAREVEN)) {
233 for (cnt = 0; cnt <= 0377; ++cnt)
234 if (parity(casetab[cnt]))
235 casetab[cnt] = casetab[cnt] | 0x80;
237 if ((ddflags & C_PARODD)) {
238 for (cnt = 0; cnt <= 0377; ++cnt)
239 if (!parity(casetab[cnt]))
240 casetab[cnt] = casetab[cnt] | 0x80;
246 (void)gettimeofday(&tv, NULL);
247 st.start = tv.tv_sec + tv.tv_usec * 1e-6;
256 if (fstat(io->fd, &sb) == -1)
257 err(1, "%s", io->name);
258 if (S_ISREG(sb.st_mode))
259 io->flags |= ISTRUNC;
260 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
261 if (ioctl(io->fd, FIODTYPE, &type) == -1) {
262 err(1, "%s", io->name);
266 else if (type & (D_DISK | D_MEM))
268 if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
274 if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
287 case -1: /* count=0 was specified */
292 if (st.in_full + st.in_part >= (uintmax_t)cpy_cnt)
298 * Zero the buffer first if sync; if doing block operations,
301 if (ddflags & C_SYNC) {
302 if (ddflags & C_FILL)
303 memset(in.dbp, fill_char, in.dbsz);
304 else if (ddflags & (C_BLOCK | C_UNBLOCK))
305 memset(in.dbp, ' ', in.dbsz);
307 memset(in.dbp, 0, in.dbsz);
310 n = read(in.fd, in.dbp, in.dbsz);
319 * If noerror not specified, die. POSIX requires that
320 * the warning message be followed by an I/O display.
322 if (!(ddflags & C_NOERROR))
323 err(1, "%s", in.name);
328 * If it's a seekable file descriptor, seek past the
329 * error. If your OS doesn't do the right thing for
330 * raw disks this section should be modified to re-read
331 * in sector size chunks.
333 if (in.flags & ISSEEK &&
334 lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
337 /* If sync not specified, omit block and continue. */
338 if (!(ddflags & C_SYNC))
341 /* Read errors count as full blocks. */
342 in.dbcnt += in.dbrcnt = in.dbsz;
345 /* Handle full input blocks. */
346 } else if ((size_t)n == in.dbsz) {
347 in.dbcnt += in.dbrcnt = n;
350 /* Handle partial input blocks. */
352 /* If sync, use the entire block. */
353 if (ddflags & C_SYNC)
354 in.dbcnt += in.dbrcnt = in.dbsz;
356 in.dbcnt += in.dbrcnt = n;
361 * POSIX states that if bs is set and no other conversions
362 * than noerror, notrunc or sync are specified, the block
363 * is output without buffering as it is read.
365 if ((ddflags & ~(C_NOERROR | C_NOTRUNC | C_SYNC)) == C_BS) {
366 out.dbcnt = in.dbcnt;
372 if (ddflags & C_SWAB) {
373 if ((n = in.dbrcnt) & 1) {
377 swab(in.dbp, in.dbp, (size_t)n);
389 * Clean up any remaining I/O and flush output. If necessary, the output file
397 else if (cfunc == block)
399 else if (cfunc == unblock)
401 if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
402 if (ddflags & C_FILL)
403 memset(out.dbp, fill_char, out.dbsz - out.dbcnt);
404 else if (ddflags & (C_BLOCK | C_UNBLOCK))
405 memset(out.dbp, ' ', out.dbsz - out.dbcnt);
407 memset(out.dbp, 0, out.dbsz - out.dbcnt);
408 out.dbcnt = out.dbsz;
410 if (out.dbcnt || pending)
414 * If the file ends with a hole, ftruncate it to extend its size
415 * up to the end of the hole (without having to write any data).
417 if (out.seek_offset > 0 && (out.flags & ISTRUNC)) {
418 if (ftruncate(out.fd, out.seek_offset) == -1)
419 err(1, "truncating %s", out.name);
433 * Write one or more blocks out. The common case is writing a full
434 * output block in a single write; increment the full block stats.
435 * Otherwise, we're into partial block writes. If a partial write,
436 * and it's a character device, just warn. If a tape device, quit.
438 * The partial writes represent two cases. 1: Where the input block
439 * was less than expected so the output block was less than expected.
440 * 2: Where the input block was the right size but we were forced to
441 * write the block in multiple chunks. The original versions of dd(1)
442 * never wrote a block in more than a single write, so the latter case
445 * One special case is if we're forced to do the write -- in that case
446 * we play games with the buffer size, and it's usually a partial write.
451 * If force, first try to write all pending data, else try to write
452 * just one block. Subsequently always write data one full block at
455 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
459 if (ddflags & C_SPARSE) {
460 sparse = 1; /* Is buffer sparse? */
461 for (i = 0; i < cnt; i++)
467 if (sparse && !force) {
473 * Seek past hole. Note that we need to record the
474 * reached offset, because we might have no more data
475 * to write, in which case we'll need to call
476 * ftruncate to extend the file size.
478 out.seek_offset = lseek(out.fd, pending, SEEK_CUR);
479 if (out.seek_offset == -1)
480 err(2, "%s: seek error creating sparse file",
485 nw = write(out.fd, outp, cnt);
494 errx(1, "%s: end of device", out.name);
496 err(1, "%s", out.name);
503 if ((size_t)nw == n && n == out.dbsz)
508 if ((size_t) nw != cnt) {
509 if (out.flags & ISTAPE)
510 errx(1, "%s: short write on tape device",
512 if (out.flags & ISCHR && !warned) {
514 warnx("%s: short write on character device",
522 if ((out.dbcnt -= n) < out.dbsz)
526 /* Reassemble the output block. */
528 (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
529 out.dbp = out.db + out.dbcnt;