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