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