dd.c revision 1.23 1 /* $NetBSD: dd.c,v 1.23 2001/11/25 10:50:06 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
26 *
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
37 * SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 #ifndef lint
42 __COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\n\
43 The Regents of the University of California. All rights reserved.\n");
44 #endif /* not lint */
45
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
49 #else
50 __RCSID("$NetBSD: dd.c,v 1.23 2001/11/25 10:50:06 lukem Exp $");
51 #endif
52 #endif /* not lint */
53
54 #include <sys/param.h>
55 #include <sys/stat.h>
56 #include <sys/ioctl.h>
57 #include <sys/mtio.h>
58 #include <sys/time.h>
59
60 #include <ctype.h>
61 #include <err.h>
62 #include <errno.h>
63 #include <fcntl.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69
70 #include "dd.h"
71 #include "extern.h"
72
73 static void dd_close(void);
74 static void dd_in(void);
75 static void getfdtype(IO *);
76 static void setup(void);
77
78 int main(int, char *[]);
79
80 IO in, out; /* input/output state */
81 STAT st; /* statistics */
82 void (*cfunc)(void); /* conversion function */
83 u_longlong_t cpy_cnt; /* # of blocks to copy */
84 u_int ddflags; /* conversion options */
85 u_longlong_t cbsz; /* conversion block size */
86 u_int files_cnt = 1; /* # of files to copy */
87 int progress = 0; /* display sign of life */
88 const u_char *ctab; /* conversion table */
89 sigset_t infoset; /* a set blocking SIGINFO */
90
91 int
92 main(int argc, char *argv[])
93 {
94 int ch;
95
96 while ((ch = getopt(argc, argv, "")) != -1) {
97 switch (ch) {
98 default:
99 errx(EXIT_FAILURE, "usage: dd [operand ...]");
100 /* NOTREACHED */
101 }
102 }
103 argc -= (optind - 1);
104 argv += (optind - 1);
105
106 jcl(argv);
107 setup();
108
109 (void)signal(SIGINFO, summaryx);
110 (void)signal(SIGINT, terminate);
111 (void)sigemptyset(&infoset);
112 (void)sigaddset(&infoset, SIGINFO);
113
114 (void)atexit(summary);
115
116 while (files_cnt--)
117 dd_in();
118
119 dd_close();
120 exit(0);
121 /* NOTREACHED */
122 }
123
124 static void
125 setup(void)
126 {
127 if (in.name == NULL) {
128 in.name = "stdin";
129 in.fd = STDIN_FILENO;
130 } else {
131 in.fd = open(in.name, O_RDONLY, 0);
132 if (in.fd < 0)
133 err(1, "%s", in.name);
134 }
135
136 getfdtype(&in);
137
138 if (files_cnt > 1 && !(in.flags & ISTAPE))
139 errx(1, "files is not supported for non-tape devices");
140
141 if (out.name == NULL) {
142 /* No way to check for read access here. */
143 out.fd = STDOUT_FILENO;
144 out.name = "stdout";
145 } else {
146 #define OFLAGS \
147 (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
148 out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
149 /*
150 * May not have read access, so try again with write only.
151 * Without read we may have a problem if output also does
152 * not support seeks.
153 */
154 if (out.fd < 0) {
155 out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
156 out.flags |= NOREAD;
157 }
158 if (out.fd < 0)
159 err(1, "%s", out.name);
160 }
161
162 getfdtype(&out);
163
164 /*
165 * Allocate space for the input and output buffers. If not doing
166 * record oriented I/O, only need a single buffer.
167 */
168 if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
169 if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
170 err(1, NULL);
171 out.db = in.db;
172 } else if ((in.db =
173 malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
174 (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL)
175 err(1, NULL);
176 in.dbp = in.db;
177 out.dbp = out.db;
178
179 /* Position the input/output streams. */
180 if (in.offset)
181 pos_in();
182 if (out.offset)
183 pos_out();
184
185 /*
186 * Truncate the output file; ignore errors because it fails on some
187 * kinds of output files, tapes, for example.
188 */
189 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
190 (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
191
192 /*
193 * If converting case at the same time as another conversion, build a
194 * table that does both at once. If just converting case, use the
195 * built-in tables.
196 */
197 if (ddflags & (C_LCASE|C_UCASE)) {
198 #ifdef NO_CONV
199 /* Should not get here, but just in case... */
200 errx(1, "case conv and -DNO_CONV");
201 #else /* NO_CONV */
202 u_int cnt;
203
204 if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
205 if (ddflags & C_LCASE) {
206 for (cnt = 0; cnt < 0377; ++cnt)
207 casetab[cnt] = tolower(ctab[cnt]);
208 } else {
209 for (cnt = 0; cnt < 0377; ++cnt)
210 casetab[cnt] = toupper(ctab[cnt]);
211 }
212 } else {
213 if (ddflags & C_LCASE) {
214 for (cnt = 0; cnt < 0377; ++cnt)
215 casetab[cnt] = tolower(cnt);
216 } else {
217 for (cnt = 0; cnt < 0377; ++cnt)
218 casetab[cnt] = toupper(cnt);
219 }
220 }
221
222 ctab = casetab;
223 #endif /* NO_CONV */
224 }
225
226 (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */
227 }
228
229 static void
230 getfdtype(IO *io)
231 {
232 struct mtget mt;
233 struct stat sb;
234
235 if (fstat(io->fd, &sb))
236 err(1, "%s", io->name);
237 if (S_ISCHR(sb.st_mode))
238 io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
239 else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
240 io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
241 }
242
243 static void
244 dd_in(void)
245 {
246 int flags;
247 u_longlong_t n;
248
249 for (flags = ddflags;;) {
250 if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
251 return;
252
253 /*
254 * Clear the buffer first if doing "sync" on input.
255 * If doing block operations use spaces. This will
256 * affect not only the C_NOERROR case, but also the
257 * last partial input block which should be padded
258 * with zero and not garbage.
259 */
260 if (flags & C_SYNC) {
261 if (flags & (C_BLOCK|C_UNBLOCK))
262 (void)memset(in.dbp, ' ', in.dbsz);
263 else
264 (void)memset(in.dbp, 0, in.dbsz);
265 }
266
267 n = read(in.fd, in.dbp, in.dbsz);
268 if (n == 0) {
269 in.dbrcnt = 0;
270 return;
271 }
272
273 /* Read error. */
274 if (n < 0) {
275 /*
276 * If noerror not specified, die. POSIX requires that
277 * the warning message be followed by an I/O display.
278 */
279 if (!(flags & C_NOERROR))
280 err(1, "%s", in.name);
281 warn("%s", in.name);
282 summary();
283
284 /*
285 * If it's not a tape drive or a pipe, 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.
289 */
290 if (!(in.flags & (ISPIPE|ISTAPE)) &&
291 lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
292 warn("%s", in.name);
293
294 /* If sync not specified, omit block and continue. */
295 if (!(ddflags & C_SYNC))
296 continue;
297
298 /* Read errors count as full blocks. */
299 in.dbcnt += in.dbrcnt = in.dbsz;
300 ++st.in_full;
301
302 /* Handle full input blocks. */
303 } else if (n == in.dbsz) {
304 in.dbcnt += in.dbrcnt = n;
305 ++st.in_full;
306
307 /* Handle partial input blocks. */
308 } else {
309 /* If sync, use the entire block. */
310 if (ddflags & C_SYNC)
311 in.dbcnt += in.dbrcnt = in.dbsz;
312 else
313 in.dbcnt += in.dbrcnt = n;
314 ++st.in_part;
315 }
316
317 /*
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.
321 */
322 if (ddflags & C_BS) {
323 out.dbcnt = in.dbcnt;
324 dd_out(1);
325 in.dbcnt = 0;
326 continue;
327 }
328
329 if (ddflags & C_SWAB) {
330 if ((n = in.dbrcnt) & 1) {
331 ++st.swab;
332 --n;
333 }
334 swab(in.dbp, in.dbp, n);
335 }
336
337 in.dbp += in.dbrcnt;
338 (*cfunc)();
339 }
340 }
341
342 /*
343 * Cleanup any remaining I/O and flush output. If necesssary, output file
344 * is truncated.
345 */
346 static void
347 dd_close(void)
348 {
349
350 if (cfunc == def)
351 def_close();
352 else if (cfunc == block)
353 block_close();
354 else if (cfunc == unblock)
355 unblock_close();
356 if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
357 (void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
358 out.dbcnt = out.dbsz;
359 }
360 if (out.dbcnt)
361 dd_out(1);
362 }
363
364 void
365 dd_out(int force)
366 {
367 static int warned;
368 u_longlong_t cnt, n, nw;
369 u_char *outp;
370
371 /*
372 * Write one or more blocks out. The common case is writing a full
373 * output block in a single write; increment the full block stats.
374 * Otherwise, we're into partial block writes. If a partial write,
375 * and it's a character device, just warn. If a tape device, quit.
376 *
377 * The partial writes represent two cases. 1: Where the input block
378 * was less than expected so the output block was less than expected.
379 * 2: Where the input block was the right size but we were forced to
380 * write the block in multiple chunks. The original versions of dd(1)
381 * never wrote a block in more than a single write, so the latter case
382 * never happened.
383 *
384 * One special case is if we're forced to do the write -- in that case
385 * we play games with the buffer size, and it's usually a partial write.
386 */
387 outp = out.db;
388 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
389 for (cnt = n;; cnt -= nw) {
390 nw = bwrite(out.fd, outp, cnt);
391 if (nw <= 0) {
392 if (nw == 0)
393 errx(1, "%s: end of device", out.name);
394 if (errno != EINTR)
395 err(1, "%s", out.name);
396 nw = 0;
397 }
398 outp += nw;
399 st.bytes += nw;
400 if (nw == n) {
401 if (n != out.dbsz)
402 ++st.out_part;
403 else
404 ++st.out_full;
405 break;
406 }
407 ++st.out_part;
408 if (nw == cnt)
409 break;
410 if (out.flags & ISCHR && !warned) {
411 warned = 1;
412 warnx("%s: short write on character device",
413 out.name);
414 }
415 if (out.flags & ISTAPE)
416 errx(1, "%s: short write on tape device", out.name);
417 }
418 if ((out.dbcnt -= n) < out.dbsz)
419 break;
420 }
421
422 /* Reassemble the output block. */
423 if (out.dbcnt)
424 (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
425 out.dbp = out.db + out.dbcnt;
426
427 if (progress)
428 (void)write(STDERR_FILENO, ".", 1);
429 }
430
431 /*
432 * A protected against SIGINFO write
433 */
434 ssize_t
435 bwrite(int fd, const void *buf, size_t len)
436 {
437 sigset_t oset;
438 ssize_t rv;
439 int oerrno;
440
441 (void)sigprocmask(SIG_BLOCK, &infoset, &oset);
442 rv = write(fd, buf, len);
443 oerrno = errno;
444 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
445 errno = oerrno;
446 return rv;
447 }
448