]>
git.saurik.com Git - apple/file_cmds.git/blob - dd/dd.c
1 /* $NetBSD: dd.c,v 1.12 1998/08/19 01:32:44 thorpej Exp $ */
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
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
42 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
43 The Regents of the University of California. All rights reserved.\n");
48 static char sccsid
[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
50 __RCSID("$NetBSD: dd.c,v 1.12 1998/08/19 01:32:44 thorpej Exp $");
54 #include <sys/param.h>
56 #include <sys/ioctl.h>
73 static void dd_close
__P((void));
74 static void dd_in
__P((void));
75 static void getfdtype
__P((IO
*));
76 static void setup
__P((void));
78 int main
__P((int, char *[]));
80 IO in
, out
; /* input/output state */
81 STAT st
; /* statistics */
82 void (*cfunc
) __P((void)); /* conversion function */
83 u_long cpy_cnt
; /* # of blocks to copy */
84 u_int ddflags
; /* conversion options */
85 u_int cbsz
; /* conversion block size */
86 u_int files_cnt
= 1; /* # of files to copy */
87 const u_char
*ctab
; /* conversion table */
97 (void)signal(SIGINFO
, summaryx
);
98 (void)signal(SIGINT
, terminate
);
100 (void)atexit(summary
);
115 if (in
.name
== NULL
) {
117 in
.fd
= STDIN_FILENO
;
119 in
.fd
= open(in
.name
, O_RDONLY
, 0);
121 err(1, "%s", in
.name
);
126 if (files_cnt
> 1 && !(in
.flags
& ISTAPE
))
127 errx(1, "files is not supported for non-tape devices");
129 if (out
.name
== NULL
) {
130 /* No way to check for read access here. */
131 out
.fd
= STDOUT_FILENO
;
135 (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
136 out
.fd
= open(out
.name
, O_RDWR
| OFLAGS
, DEFFILEMODE
);
138 * May not have read access, so try again with write only.
139 * Without read we may have a problem if output also does
143 out
.fd
= open(out
.name
, O_WRONLY
| OFLAGS
, DEFFILEMODE
);
147 err(1, "%s", out
.name
);
153 * Allocate space for the input and output buffers. If not doing
154 * record oriented I/O, only need a single buffer.
156 if (!(ddflags
& (C_BLOCK
|C_UNBLOCK
))) {
157 if ((in
.db
= malloc(out
.dbsz
+ in
.dbsz
- 1)) == NULL
)
161 malloc((u_int
)(MAX(in
.dbsz
, cbsz
) + cbsz
))) == NULL
||
162 (out
.db
= malloc((u_int
)(out
.dbsz
+ cbsz
))) == NULL
)
167 /* Position the input/output streams. */
174 * Truncate the output file; ignore errors because it fails on some
175 * kinds of output files, tapes, for example.
177 if ((ddflags
& (C_OF
| C_SEEK
| C_NOTRUNC
)) == (C_OF
| C_SEEK
))
178 (void)ftruncate(out
.fd
, (off_t
)out
.offset
* out
.dbsz
);
181 * If converting case at the same time as another conversion, build a
182 * table that does both at once. If just converting case, use the
185 if (ddflags
& (C_LCASE
|C_UCASE
)) {
187 /* Should not get here, but just in case... */
188 errx(1, "case conv and -DNO_CONV");
190 if (ddflags
& C_ASCII
|| ddflags
& C_EBCDIC
) {
191 if (ddflags
& C_LCASE
) {
192 for (cnt
= 0; cnt
< 0377; ++cnt
)
193 casetab
[cnt
] = tolower(ctab
[cnt
]);
195 for (cnt
= 0; cnt
< 0377; ++cnt
)
196 casetab
[cnt
] = toupper(ctab
[cnt
]);
199 if (ddflags
& C_LCASE
) {
200 for (cnt
= 0; cnt
< 0377; ++cnt
)
201 casetab
[cnt
] = tolower(cnt
);
203 for (cnt
= 0; cnt
< 0377; ++cnt
)
204 casetab
[cnt
] = toupper(cnt
);
212 (void)time(&st
.start
); /* Statistics timestamp. */
222 if (fstat(io
->fd
, &sb
))
223 err(1, "%s", io
->name
);
224 if (S_ISCHR(sb
.st_mode
))
225 io
->flags
|= ioctl(io
->fd
, MTIOCGET
, &mt
) ? ISCHR
: ISTAPE
;
226 else if (lseek(io
->fd
, (off_t
)0, SEEK_CUR
) == -1 && errno
== ESPIPE
)
227 io
->flags
|= ISPIPE
; /* XXX fixed in 4.4BSD */
235 for (flags
= ddflags
;;) {
236 if (cpy_cnt
&& (st
.in_full
+ st
.in_part
) >= cpy_cnt
)
240 * Clear the buffer first if doing "sync" on input.
241 * If doing block operations use spaces. This will
242 * affect not only the C_NOERROR case, but also the
243 * last partial input block which should be padded
244 * with zero and not garbage.
246 if (flags
& C_SYNC
) {
247 if (flags
& (C_BLOCK
|C_UNBLOCK
))
248 (void)memset(in
.dbp
, ' ', in
.dbsz
);
250 (void)memset(in
.dbp
, 0, in
.dbsz
);
253 n
= read(in
.fd
, in
.dbp
, in
.dbsz
);
262 * If noerror not specified, die. POSIX requires that
263 * the warning message be followed by an I/O display.
265 if (!(flags
& C_NOERROR
))
266 err(1, "%s", in
.name
);
271 * If it's not a tape drive or a pipe, seek past the
272 * error. If your OS doesn't do the right thing for
273 * raw disks this section should be modified to re-read
274 * in sector size chunks.
276 if (!(in
.flags
& (ISPIPE
|ISTAPE
)) &&
277 lseek(in
.fd
, (off_t
)in
.dbsz
, SEEK_CUR
))
280 /* If sync not specified, omit block and continue. */
281 if (!(ddflags
& C_SYNC
))
284 /* Read errors count as full blocks. */
285 in
.dbcnt
+= in
.dbrcnt
= in
.dbsz
;
288 /* Handle full input blocks. */
289 } else if (n
== in
.dbsz
) {
290 in
.dbcnt
+= in
.dbrcnt
= n
;
293 /* Handle partial input blocks. */
295 /* If sync, use the entire block. */
296 if (ddflags
& C_SYNC
)
297 in
.dbcnt
+= in
.dbrcnt
= in
.dbsz
;
299 in
.dbcnt
+= in
.dbrcnt
= n
;
304 * POSIX states that if bs is set and no other conversions
305 * than noerror, notrunc or sync are specified, the block
306 * is output without buffering as it is read.
308 if (ddflags
& C_BS
) {
309 out
.dbcnt
= in
.dbcnt
;
315 if (ddflags
& C_SWAB
) {
316 if ((n
= in
.dbcnt
) & 1) {
320 swab(in
.dbp
, in
.dbp
, n
);
329 * Cleanup any remaining I/O and flush output. If necesssary, output file
337 else if (cfunc
== block
)
339 else if (cfunc
== unblock
)
341 if (ddflags
& C_OSYNC
&& out
.dbcnt
< out
.dbsz
) {
342 (void)memset(out
.dbp
, 0, out
.dbsz
- out
.dbcnt
);
343 out
.dbcnt
= out
.dbsz
;
358 * Write one or more blocks out. The common case is writing a full
359 * output block in a single write; increment the full block stats.
360 * Otherwise, we're into partial block writes. If a partial write,
361 * and it's a character device, just warn. If a tape device, quit.
363 * The partial writes represent two cases. 1: Where the input block
364 * was less than expected so the output block was less than expected.
365 * 2: Where the input block was the right size but we were forced to
366 * write the block in multiple chunks. The original versions of dd(1)
367 * never wrote a block in more than a single write, so the latter case
370 * One special case is if we're forced to do the write -- in that case
371 * we play games with the buffer size, and it's usually a partial write.
374 for (n
= force
? out
.dbcnt
: out
.dbsz
;; n
= out
.dbsz
) {
375 for (cnt
= n
;; cnt
-= nw
) {
376 nw
= write(out
.fd
, outp
, cnt
);
379 errx(1, "%s: end of device", out
.name
);
381 err(1, "%s", out
.name
);
396 if (out
.flags
& ISCHR
&& !warned
) {
398 warnx("%s: short write on character device",
401 if (out
.flags
& ISTAPE
)
402 errx(1, "%s: short write on tape device", out
.name
);
404 if ((out
.dbcnt
-= n
) < out
.dbsz
)
408 /* Reassemble the output block. */
410 (void)memmove(out
.db
, out
.dbp
- out
.dbcnt
, out
.dbcnt
);
411 out
.dbp
= out
.db
+ out
.dbcnt
;