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