]>
Commit | Line | Data |
---|---|---|
44a7a5ab A |
1 | /*- |
2 | * Copyright (c) 1991, 1993, 1994 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
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. | |
8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
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. | |
24 | * | |
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 | |
35 | * SUCH DAMAGE. | |
36 | */ | |
37 | ||
44a7a5ab | 38 | #ifndef lint |
440bd198 A |
39 | static char const copyright[] = |
40 | "@(#) Copyright (c) 1991, 1993, 1994\n\ | |
41 | The Regents of the University of California. All rights reserved.\n"; | |
44a7a5ab A |
42 | #endif /* not lint */ |
43 | ||
44 | #ifndef lint | |
45 | #if 0 | |
46 | static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94"; | |
44a7a5ab | 47 | #endif |
440bd198 A |
48 | static const char rcsid[] = |
49 | "$FreeBSD: src/bin/dd/dd.c,v 1.36 2002/03/07 14:00:33 markm Exp $"; | |
44a7a5ab A |
50 | #endif /* not lint */ |
51 | ||
52 | #include <sys/param.h> | |
53 | #include <sys/stat.h> | |
440bd198 A |
54 | #include <sys/conf.h> |
55 | #include <sys/filio.h> | |
56 | #include <sys/time.h> | |
57 | ||
58 | #ifdef __APPLE__ | |
44a7a5ab | 59 | #include <sys/ioctl.h> |
440bd198 A |
60 | #else |
61 | #include <sys/disklabel.h> | |
62 | #endif | |
44a7a5ab A |
63 | |
64 | #include <ctype.h> | |
65 | #include <err.h> | |
66 | #include <errno.h> | |
67 | #include <fcntl.h> | |
440bd198 | 68 | #include <locale.h> |
44a7a5ab A |
69 | #include <stdio.h> |
70 | #include <stdlib.h> | |
71 | #include <string.h> | |
44a7a5ab A |
72 | #include <unistd.h> |
73 | ||
74 | #include "dd.h" | |
75 | #include "extern.h" | |
76 | ||
440bd198 A |
77 | static void dd_close(void); |
78 | static void dd_in(void); | |
79 | static void getfdtype(IO *); | |
80 | static void setup(void); | |
44a7a5ab A |
81 | |
82 | IO in, out; /* input/output state */ | |
83 | STAT st; /* statistics */ | |
440bd198 A |
84 | void (*cfunc)(void); /* conversion function */ |
85 | quad_t cpy_cnt; /* # of blocks to copy */ | |
86 | off_t pending = 0; /* pending seek if sparse */ | |
44a7a5ab | 87 | u_int ddflags; /* conversion options */ |
440bd198 A |
88 | size_t cbsz; /* conversion block size */ |
89 | quad_t files_cnt = 1; /* # of files to copy */ | |
90 | const u_char *ctab; /* conversion table */ | |
44a7a5ab A |
91 | |
92 | int | |
440bd198 | 93 | main(int argc, char *argv[]) |
44a7a5ab | 94 | { |
440bd198 | 95 | (void)setlocale(LC_CTYPE, ""); |
44a7a5ab A |
96 | jcl(argv); |
97 | setup(); | |
98 | ||
99 | (void)signal(SIGINFO, summaryx); | |
100 | (void)signal(SIGINT, terminate); | |
101 | ||
440bd198 | 102 | atexit(summary); |
44a7a5ab A |
103 | |
104 | while (files_cnt--) | |
105 | dd_in(); | |
106 | ||
107 | dd_close(); | |
108 | exit(0); | |
44a7a5ab A |
109 | } |
110 | ||
111 | static void | |
440bd198 | 112 | setup(void) |
44a7a5ab A |
113 | { |
114 | u_int cnt; | |
440bd198 | 115 | struct timeval tv; |
44a7a5ab A |
116 | |
117 | if (in.name == NULL) { | |
118 | in.name = "stdin"; | |
119 | in.fd = STDIN_FILENO; | |
120 | } else { | |
121 | in.fd = open(in.name, O_RDONLY, 0); | |
440bd198 | 122 | if (in.fd == -1) |
44a7a5ab A |
123 | err(1, "%s", in.name); |
124 | } | |
125 | ||
126 | getfdtype(&in); | |
127 | ||
128 | if (files_cnt > 1 && !(in.flags & ISTAPE)) | |
129 | errx(1, "files is not supported for non-tape devices"); | |
130 | ||
131 | if (out.name == NULL) { | |
132 | /* No way to check for read access here. */ | |
133 | out.fd = STDOUT_FILENO; | |
134 | out.name = "stdout"; | |
135 | } else { | |
136 | #define OFLAGS \ | |
137 | (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) | |
138 | out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE); | |
139 | /* | |
140 | * May not have read access, so try again with write only. | |
141 | * Without read we may have a problem if output also does | |
142 | * not support seeks. | |
143 | */ | |
440bd198 | 144 | if (out.fd == -1) { |
44a7a5ab A |
145 | out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); |
146 | out.flags |= NOREAD; | |
147 | } | |
440bd198 | 148 | if (out.fd == -1) |
44a7a5ab A |
149 | err(1, "%s", out.name); |
150 | } | |
151 | ||
152 | getfdtype(&out); | |
153 | ||
154 | /* | |
155 | * Allocate space for the input and output buffers. If not doing | |
156 | * record oriented I/O, only need a single buffer. | |
157 | */ | |
440bd198 | 158 | if (!(ddflags & (C_BLOCK | C_UNBLOCK))) { |
44a7a5ab | 159 | if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) |
440bd198 | 160 | err(1, "input buffer"); |
44a7a5ab | 161 | out.db = in.db; |
440bd198 A |
162 | } else if ((in.db = malloc(MAX(in.dbsz, cbsz) + cbsz)) == NULL || |
163 | (out.db = malloc(out.dbsz + cbsz)) == NULL) | |
164 | err(1, "output buffer"); | |
44a7a5ab A |
165 | in.dbp = in.db; |
166 | out.dbp = out.db; | |
167 | ||
168 | /* Position the input/output streams. */ | |
169 | if (in.offset) | |
170 | pos_in(); | |
171 | if (out.offset) | |
172 | pos_out(); | |
173 | ||
174 | /* | |
440bd198 A |
175 | * Truncate the output file. If it fails on a type of output file |
176 | * that it should _not_ fail on, error out. | |
44a7a5ab | 177 | */ |
440bd198 A |
178 | if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK) && |
179 | out.flags & ISTRUNC) | |
180 | if (ftruncate(out.fd, out.offset * out.dbsz) == -1) | |
181 | err(1, "truncating %s", out.name); | |
44a7a5ab A |
182 | |
183 | /* | |
184 | * If converting case at the same time as another conversion, build a | |
185 | * table that does both at once. If just converting case, use the | |
186 | * built-in tables. | |
187 | */ | |
440bd198 A |
188 | if (ddflags & (C_LCASE | C_UCASE)) { |
189 | if (ddflags & (C_ASCII | C_EBCDIC)) { | |
44a7a5ab | 190 | if (ddflags & C_LCASE) { |
440bd198 | 191 | for (cnt = 0; cnt <= 0377; ++cnt) |
44a7a5ab A |
192 | casetab[cnt] = tolower(ctab[cnt]); |
193 | } else { | |
440bd198 | 194 | for (cnt = 0; cnt <= 0377; ++cnt) |
44a7a5ab A |
195 | casetab[cnt] = toupper(ctab[cnt]); |
196 | } | |
197 | } else { | |
198 | if (ddflags & C_LCASE) { | |
440bd198 A |
199 | for (cnt = 0; cnt <= 0377; ++cnt) |
200 | casetab[cnt] = tolower((int)cnt); | |
44a7a5ab | 201 | } else { |
440bd198 A |
202 | for (cnt = 0; cnt <= 0377; ++cnt) |
203 | casetab[cnt] = toupper((int)cnt); | |
44a7a5ab A |
204 | } |
205 | } | |
44a7a5ab | 206 | ctab = casetab; |
44a7a5ab A |
207 | } |
208 | ||
440bd198 A |
209 | (void)gettimeofday(&tv, (struct timezone *)NULL); |
210 | st.start = tv.tv_sec + tv.tv_usec * 1e-6; | |
44a7a5ab A |
211 | } |
212 | ||
213 | static void | |
440bd198 | 214 | getfdtype(IO *io) |
44a7a5ab | 215 | { |
44a7a5ab | 216 | struct stat sb; |
440bd198 | 217 | int type; |
44a7a5ab | 218 | |
440bd198 | 219 | if (fstat(io->fd, &sb) == -1) |
44a7a5ab | 220 | err(1, "%s", io->name); |
440bd198 A |
221 | if (S_ISREG(sb.st_mode)) |
222 | io->flags |= ISTRUNC; | |
223 | if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) { | |
224 | if (ioctl(io->fd, FIODTYPE, &type) == -1) { | |
225 | err(1, "%s", io->name); | |
226 | } else { | |
c59d3020 A |
227 | #ifdef __APPLE__ /* MacOSX uses enumeration for type not a bitmask */ |
228 | if (type == D_TAPE) | |
229 | io->flags |= ISTAPE; | |
230 | else if (type == D_DISK || type == D_TTY) { | |
231 | #else /* !__APPLE__ */ | |
440bd198 A |
232 | if (type & D_TAPE) |
233 | io->flags |= ISTAPE; | |
440bd198 A |
234 | else if (type & (D_DISK | D_MEM)) { |
235 | if (type & D_DISK) { | |
236 | const int one = 1; | |
237 | ||
238 | (void)ioctl(io->fd, DIOCWLABEL, &one); | |
239 | } | |
c59d3020 | 240 | #endif /* __APPLE__ */ |
440bd198 A |
241 | io->flags |= ISSEEK; |
242 | } | |
c59d3020 A |
243 | #ifdef __APPLE__ |
244 | if (S_ISCHR(sb.st_mode) && (type != D_TAPE)) | |
245 | #else /* !__APPLE__ */ | |
440bd198 | 246 | if (S_ISCHR(sb.st_mode) && (type & D_TAPE) == 0) |
c59d3020 | 247 | #endif /* __APPLE__ */ |
440bd198 A |
248 | io->flags |= ISCHR; |
249 | } | |
250 | return; | |
251 | } | |
252 | errno = 0; | |
253 | if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) | |
254 | io->flags |= ISPIPE; | |
255 | else | |
256 | io->flags |= ISSEEK; | |
44a7a5ab A |
257 | } |
258 | ||
259 | static void | |
440bd198 | 260 | dd_in(void) |
44a7a5ab | 261 | { |
440bd198 | 262 | ssize_t n; |
44a7a5ab | 263 | |
440bd198 A |
264 | for (;;) { |
265 | switch (cpy_cnt) { | |
266 | case -1: /* count=0 was specified */ | |
44a7a5ab | 267 | return; |
440bd198 A |
268 | case 0: |
269 | break; | |
270 | default: | |
271 | if (st.in_full + st.in_part >= (u_quad_t)cpy_cnt) | |
272 | return; | |
273 | break; | |
274 | } | |
44a7a5ab A |
275 | |
276 | /* | |
440bd198 A |
277 | * Zero the buffer first if sync; if doing block operations, |
278 | * use spaces. | |
44a7a5ab | 279 | */ |
440bd198 A |
280 | if (ddflags & C_SYNC) { |
281 | if (ddflags & (C_BLOCK | C_UNBLOCK)) | |
282 | memset(in.dbp, ' ', in.dbsz); | |
44a7a5ab | 283 | else |
440bd198 | 284 | memset(in.dbp, 0, in.dbsz); |
44a7a5ab A |
285 | } |
286 | ||
287 | n = read(in.fd, in.dbp, in.dbsz); | |
288 | if (n == 0) { | |
289 | in.dbrcnt = 0; | |
290 | return; | |
291 | } | |
292 | ||
293 | /* Read error. */ | |
440bd198 | 294 | if (n == -1) { |
44a7a5ab A |
295 | /* |
296 | * If noerror not specified, die. POSIX requires that | |
297 | * the warning message be followed by an I/O display. | |
298 | */ | |
440bd198 | 299 | if (!(ddflags & C_NOERROR)) |
44a7a5ab A |
300 | err(1, "%s", in.name); |
301 | warn("%s", in.name); | |
302 | summary(); | |
303 | ||
304 | /* | |
440bd198 | 305 | * If it's a seekable file descriptor, seek past the |
44a7a5ab A |
306 | * error. If your OS doesn't do the right thing for |
307 | * raw disks this section should be modified to re-read | |
308 | * in sector size chunks. | |
309 | */ | |
440bd198 | 310 | if (in.flags & ISSEEK && |
44a7a5ab A |
311 | lseek(in.fd, (off_t)in.dbsz, SEEK_CUR)) |
312 | warn("%s", in.name); | |
313 | ||
314 | /* If sync not specified, omit block and continue. */ | |
315 | if (!(ddflags & C_SYNC)) | |
316 | continue; | |
317 | ||
318 | /* Read errors count as full blocks. */ | |
319 | in.dbcnt += in.dbrcnt = in.dbsz; | |
320 | ++st.in_full; | |
321 | ||
322 | /* Handle full input blocks. */ | |
440bd198 | 323 | } else if ((size_t)n == in.dbsz) { |
44a7a5ab A |
324 | in.dbcnt += in.dbrcnt = n; |
325 | ++st.in_full; | |
326 | ||
327 | /* Handle partial input blocks. */ | |
328 | } else { | |
329 | /* If sync, use the entire block. */ | |
330 | if (ddflags & C_SYNC) | |
331 | in.dbcnt += in.dbrcnt = in.dbsz; | |
332 | else | |
333 | in.dbcnt += in.dbrcnt = n; | |
334 | ++st.in_part; | |
335 | } | |
336 | ||
337 | /* | |
338 | * POSIX states that if bs is set and no other conversions | |
339 | * than noerror, notrunc or sync are specified, the block | |
340 | * is output without buffering as it is read. | |
341 | */ | |
342 | if (ddflags & C_BS) { | |
343 | out.dbcnt = in.dbcnt; | |
344 | dd_out(1); | |
345 | in.dbcnt = 0; | |
346 | continue; | |
347 | } | |
348 | ||
349 | if (ddflags & C_SWAB) { | |
440bd198 | 350 | if ((n = in.dbrcnt) & 1) { |
44a7a5ab A |
351 | ++st.swab; |
352 | --n; | |
353 | } | |
440bd198 | 354 | swab(in.dbp, in.dbp, (size_t)n); |
44a7a5ab A |
355 | } |
356 | ||
357 | in.dbp += in.dbrcnt; | |
358 | (*cfunc)(); | |
359 | } | |
360 | } | |
361 | ||
362 | /* | |
440bd198 | 363 | * Clean up any remaining I/O and flush output. If necessary, the output file |
44a7a5ab A |
364 | * is truncated. |
365 | */ | |
366 | static void | |
440bd198 | 367 | dd_close(void) |
44a7a5ab A |
368 | { |
369 | if (cfunc == def) | |
370 | def_close(); | |
371 | else if (cfunc == block) | |
372 | block_close(); | |
373 | else if (cfunc == unblock) | |
374 | unblock_close(); | |
440bd198 A |
375 | if (ddflags & C_OSYNC && out.dbcnt && out.dbcnt < out.dbsz) { |
376 | if (ddflags & (C_BLOCK | C_UNBLOCK)) | |
377 | memset(out.dbp, ' ', out.dbsz - out.dbcnt); | |
378 | else | |
379 | memset(out.dbp, 0, out.dbsz - out.dbcnt); | |
44a7a5ab A |
380 | out.dbcnt = out.dbsz; |
381 | } | |
440bd198 | 382 | if (out.dbcnt || pending) |
44a7a5ab A |
383 | dd_out(1); |
384 | } | |
385 | ||
386 | void | |
440bd198 | 387 | dd_out(int force) |
44a7a5ab | 388 | { |
44a7a5ab | 389 | u_char *outp; |
440bd198 A |
390 | size_t cnt, i, n; |
391 | ssize_t nw; | |
392 | static int warned; | |
393 | int sparse; | |
44a7a5ab A |
394 | |
395 | /* | |
396 | * Write one or more blocks out. The common case is writing a full | |
397 | * output block in a single write; increment the full block stats. | |
398 | * Otherwise, we're into partial block writes. If a partial write, | |
399 | * and it's a character device, just warn. If a tape device, quit. | |
400 | * | |
401 | * The partial writes represent two cases. 1: Where the input block | |
402 | * was less than expected so the output block was less than expected. | |
403 | * 2: Where the input block was the right size but we were forced to | |
404 | * write the block in multiple chunks. The original versions of dd(1) | |
405 | * never wrote a block in more than a single write, so the latter case | |
406 | * never happened. | |
407 | * | |
408 | * One special case is if we're forced to do the write -- in that case | |
409 | * we play games with the buffer size, and it's usually a partial write. | |
410 | */ | |
411 | outp = out.db; | |
412 | for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) { | |
413 | for (cnt = n;; cnt -= nw) { | |
440bd198 A |
414 | sparse = 0; |
415 | if (ddflags & C_SPARSE) { | |
416 | sparse = 1; /* Is buffer sparse? */ | |
417 | for (i = 0; i < cnt; i++) | |
418 | if (outp[i] != 0) { | |
419 | sparse = 0; | |
420 | break; | |
421 | } | |
422 | } | |
423 | if (sparse && !force) { | |
424 | pending += cnt; | |
425 | nw = cnt; | |
426 | } else { | |
427 | if (pending != 0) { | |
428 | if (force) | |
429 | pending--; | |
430 | if (lseek(out.fd, pending, SEEK_CUR) == | |
431 | -1) | |
432 | err(2, "%s: seek error creating sparse file", | |
433 | out.name); | |
434 | if (force) | |
435 | write(out.fd, outp, 1); | |
436 | pending = 0; | |
437 | } | |
438 | if (cnt) | |
439 | nw = write(out.fd, outp, cnt); | |
440 | else | |
441 | return; | |
442 | } | |
443 | ||
44a7a5ab A |
444 | if (nw <= 0) { |
445 | if (nw == 0) | |
446 | errx(1, "%s: end of device", out.name); | |
447 | if (errno != EINTR) | |
448 | err(1, "%s", out.name); | |
449 | nw = 0; | |
450 | } | |
451 | outp += nw; | |
452 | st.bytes += nw; | |
440bd198 | 453 | if ((size_t)nw == n) { |
44a7a5ab A |
454 | if (n != out.dbsz) |
455 | ++st.out_part; | |
456 | else | |
457 | ++st.out_full; | |
458 | break; | |
459 | } | |
460 | ++st.out_part; | |
440bd198 | 461 | if ((size_t)nw == cnt) |
44a7a5ab | 462 | break; |
440bd198 A |
463 | if (out.flags & ISTAPE) |
464 | errx(1, "%s: short write on tape device", | |
465 | out.name); | |
44a7a5ab A |
466 | if (out.flags & ISCHR && !warned) { |
467 | warned = 1; | |
468 | warnx("%s: short write on character device", | |
469 | out.name); | |
470 | } | |
44a7a5ab A |
471 | } |
472 | if ((out.dbcnt -= n) < out.dbsz) | |
473 | break; | |
474 | } | |
475 | ||
476 | /* Reassemble the output block. */ | |
477 | if (out.dbcnt) | |
478 | (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt); | |
479 | out.dbp = out.db + out.dbcnt; | |
480 | } |