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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#) Copyright (c) 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
38 * @(#)dd.c 8.5 (Berkeley) 4/2/94
39 * $FreeBSD: src/bin/dd/dd.c,v 1.27.2.3 2001/08/01 01:37:35 obrien Exp $
40 * $DragonFly: src/bin/dd/dd.c,v 1.2 2003/06/17 04:22:49 dillon Exp $
43 #include <sys/param.h>
46 #include <sys/disklabel.h>
47 #include <sys/filio.h>
63 static void dd_close __P((void));
64 static void dd_in __P((void));
65 int main __P((int, char *[]));
66 static void getfdtype __P((IO *));
67 static void setup __P((void));
69 IO in, out; /* input/output state */
70 STAT st; /* statistics */
71 void (*cfunc) __P((void)); /* conversion function */
72 quad_t cpy_cnt; /* # of blocks to copy */
73 off_t pending = 0; /* pending seek if sparse */
74 u_int ddflags; /* conversion options */
75 size_t cbsz; /* conversion block size */
76 quad_t files_cnt = 1; /* # of files to copy */
77 const u_char *ctab; /* conversion table */
84 (void)setlocale(LC_CTYPE, "");
88 (void)signal(SIGINFO, summaryx);
89 (void)signal(SIGINT, terminate);
106 if (in.name == NULL) {
108 in.fd = STDIN_FILENO;
110 in.fd = open(in.name, O_RDONLY, 0);
112 err(1, "%s", in.name);
117 if (files_cnt > 1 && !(in.flags & ISTAPE))
118 errx(1, "files is not supported for non-tape devices");
120 if (out.name == NULL) {
121 /* No way to check for read access here. */
122 out.fd = STDOUT_FILENO;
126 (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
127 out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
129 * May not have read access, so try again with write only.
130 * Without read we may have a problem if output also does
134 out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
138 err(1, "%s", out.name);
144 * Allocate space for the input and output buffers. If not doing
145 * record oriented I/O, only need a single buffer.
147 if (!(ddflags & (C_BLOCK | C_UNBLOCK))) {
148 if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
149 err(1, "input buffer");
151 } else if ((in.db = malloc(MAX(in.dbsz, cbsz) + cbsz)) == NULL ||
152 (out.db = malloc(out.dbsz + cbsz)) == NULL)
153 err(1, "output buffer");
157 /* Position the input/output streams. */
164 * Truncate the output file. If it fails on a type of output file
165 * that it should _not_ fail on, error out.
167 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) &&
169 if (ftruncate(out.fd, out.offset * out.dbsz) == -1)
170 err(1, "truncating %s", out.name);
173 * If converting case at the same time as another conversion, build a
174 * table that does both at once. If just converting case, use the
177 if (ddflags & (C_LCASE | C_UCASE)) {
178 if (ddflags & (C_ASCII | C_EBCDIC)) {
179 if (ddflags & C_LCASE) {
180 for (cnt = 0; cnt <= 0377; ++cnt)
181 casetab[cnt] = tolower(ctab[cnt]);
183 for (cnt = 0; cnt <= 0377; ++cnt)
184 casetab[cnt] = toupper(ctab[cnt]);
187 if (ddflags & C_LCASE) {
188 for (cnt = 0; cnt <= 0377; ++cnt)
189 casetab[cnt] = tolower((int)cnt);
191 for (cnt = 0; cnt <= 0377; ++cnt)
192 casetab[cnt] = toupper((int)cnt);
198 (void)gettimeofday(&tv, (struct timezone *)NULL);
199 st.start = tv.tv_sec + tv.tv_usec * 1e-6;
209 if (fstat(io->fd, &sb) == -1)
210 err(1, "%s", io->name);
211 if (S_ISREG(sb.st_mode))
212 io->flags |= ISTRUNC;
213 if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
214 if (ioctl(io->fd, FIODTYPE, &type) == -1) {
215 err(1, "%s", io->name);
219 else if (type & (D_DISK | D_MEM)) {
223 (void)ioctl(io->fd, DIOCWLABEL, &one);
227 if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0)
233 if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
246 case -1: /* count=0 was specified */
251 if (st.in_full + st.in_part >= (u_quad_t)cpy_cnt)
257 * Zero the buffer first if sync; if doing block operations,
260 if (ddflags & C_SYNC) {
261 if (ddflags & (C_BLOCK | C_UNBLOCK))
262 memset(in.dbp, ' ', in.dbsz);
264 memset(in.dbp, 0, in.dbsz);
267 n = read(in.fd, in.dbp, in.dbsz);
276 * If noerror not specified, die. POSIX requires that
277 * the warning message be followed by an I/O display.
279 if (!(ddflags & C_NOERROR))
280 err(1, "%s", in.name);
285 * If it's a seekable file descriptor, seek past the
286 * error. If your OS doesn't do the right thing for
287 * raw disks this section should be modified to re-read
288 * in sector size chunks.
290 if (in.flags & ISSEEK &&
291 lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
294 /* If sync not specified, omit block and continue. */
295 if (!(ddflags & C_SYNC))
298 /* Read errors count as full blocks. */
299 in.dbcnt += in.dbrcnt = in.dbsz;
302 /* Handle full input blocks. */
303 } else if ((size_t)n == in.dbsz) {
304 in.dbcnt += in.dbrcnt = n;
307 /* Handle partial input blocks. */
309 /* If sync, use the entire block. */
310 if (ddflags & C_SYNC)
311 in.dbcnt += in.dbrcnt = in.dbsz;
313 in.dbcnt += in.dbrcnt = n;
318 * POSIX states that if bs is set and no other conversions
319 * than noerror, notrunc or sync are specified, the block
320 * is output without buffering as it is read.
322 if (ddflags & C_BS) {
323 out.dbcnt = in.dbcnt;
329 if (ddflags & C_SWAB) {
330 if ((n = in.dbrcnt) & 1) {
334 swab(in.dbp, in.dbp, (size_t)n);
343 * Clean up any remaining I/O and flush output. If necessary, the output file
351 else if (cfunc == block)
353 else if (cfunc == unblock)
355 if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) {
356 if (ddflags & (C_BLOCK | C_UNBLOCK))
357 memset(out.dbp, ' ', out.dbsz - out.dbcnt);
359 memset(out.dbp, 0, out.dbsz - out.dbcnt);
360 out.dbcnt = out.dbsz;
362 if (out.dbcnt || pending)
377 * Write one or more blocks out. The common case is writing a full
378 * output block in a single write; increment the full block stats.
379 * Otherwise, we're into partial block writes. If a partial write,
380 * and it's a character device, just warn. If a tape device, quit.
382 * The partial writes represent two cases. 1: Where the input block
383 * was less than expected so the output block was less than expected.
384 * 2: Where the input block was the right size but we were forced to
385 * write the block in multiple chunks. The original versions of dd(1)
386 * never wrote a block in more than a single write, so the latter case
389 * One special case is if we're forced to do the write -- in that case
390 * we play games with the buffer size, and it's usually a partial write.
393 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
394 for (cnt = n;; cnt -= nw) {
396 if (ddflags & C_SPARSE) {
397 sparse = 1; /* Is buffer sparse? */
398 for (i = 0; i < cnt; i++)
404 if (sparse && !force) {
411 if (lseek(out.fd, pending, SEEK_CUR) ==
413 err(2, "%s: seek error creating sparse file",
416 write(out.fd, outp, 1);
420 nw = write(out.fd, outp, cnt);
427 errx(1, "%s: end of device", out.name);
429 err(1, "%s", out.name);
434 if ((size_t)nw == n) {
442 if ((size_t)nw == cnt)
444 if (out.flags & ISTAPE)
445 errx(1, "%s: short write on tape device",
447 if (out.flags & ISCHR && !warned) {
449 warnx("%s: short write on character device",
453 if ((out.dbcnt -= n) < out.dbsz)
457 /* Reassemble the output block. */
459 (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
460 out.dbp = out.db + out.dbcnt;