dd.c revision 1.1 1 1.1 glass /*-
2 1.1 glass * Copyright (c) 1991 The Regents of the University of California.
3 1.1 glass * All rights reserved.
4 1.1 glass *
5 1.1 glass * This code is derived from software contributed to Berkeley by
6 1.1 glass * Keith Muller of the University of California, San Diego and Lance
7 1.1 glass * Visser of Convex Computer Corporation.
8 1.1 glass *
9 1.1 glass * Redistribution and use in source and binary forms, with or without
10 1.1 glass * modification, are permitted provided that the following conditions
11 1.1 glass * are met:
12 1.1 glass * 1. Redistributions of source code must retain the above copyright
13 1.1 glass * notice, this list of conditions and the following disclaimer.
14 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 glass * notice, this list of conditions and the following disclaimer in the
16 1.1 glass * documentation and/or other materials provided with the distribution.
17 1.1 glass * 3. All advertising materials mentioning features or use of this software
18 1.1 glass * must display the following acknowledgement:
19 1.1 glass * This product includes software developed by the University of
20 1.1 glass * California, Berkeley and its contributors.
21 1.1 glass * 4. Neither the name of the University nor the names of its contributors
22 1.1 glass * may be used to endorse or promote products derived from this software
23 1.1 glass * without specific prior written permission.
24 1.1 glass *
25 1.1 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 glass * SUCH DAMAGE.
36 1.1 glass */
37 1.1 glass
38 1.1 glass #ifndef lint
39 1.1 glass char copyright[] =
40 1.1 glass "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
41 1.1 glass All rights reserved.\n";
42 1.1 glass #endif /* not lint */
43 1.1 glass
44 1.1 glass #ifndef lint
45 1.1 glass static char sccsid[] = "@(#)dd.c 5.16 (Berkeley) 4/28/93";
46 1.1 glass #endif /* not lint */
47 1.1 glass
48 1.1 glass #include <sys/param.h>
49 1.1 glass #include <sys/stat.h>
50 1.1 glass #include <sys/ioctl.h>
51 1.1 glass #include <sys/mtio.h>
52 1.1 glass
53 1.1 glass #include <ctype.h>
54 1.1 glass #include <errno.h>
55 1.1 glass #include <fcntl.h>
56 1.1 glass #include <signal.h>
57 1.1 glass #include <stdio.h>
58 1.1 glass #include <stdlib.h>
59 1.1 glass #include <string.h>
60 1.1 glass #include <unistd.h>
61 1.1 glass
62 1.1 glass #include "dd.h"
63 1.1 glass #include "extern.h"
64 1.1 glass
65 1.1 glass static void dd_close __P((void));
66 1.1 glass static void dd_in __P((void));
67 1.1 glass static void setup __P((void));
68 1.1 glass
69 1.1 glass IO in, out; /* input/output state */
70 1.1 glass STAT st; /* statistics */
71 1.1 glass void (*cfunc) __P((void)); /* conversion function */
72 1.1 glass u_long cpy_cnt; /* # of blocks to copy */
73 1.1 glass u_int ddflags; /* conversion options */
74 1.1 glass u_int cbsz; /* conversion block size */
75 1.1 glass u_int files_cnt = 1; /* # of files to copy */
76 1.1 glass int errstats; /* show statistics on error */
77 1.1 glass u_char *ctab; /* conversion table */
78 1.1 glass
79 1.1 glass int
80 1.1 glass main(argc, argv)
81 1.1 glass int argc;
82 1.1 glass char *argv[];
83 1.1 glass {
84 1.1 glass jcl(argv);
85 1.1 glass setup();
86 1.1 glass
87 1.1 glass (void)signal(SIGINFO, summary);
88 1.1 glass (void)signal(SIGINT, terminate);
89 1.1 glass
90 1.1 glass for (errstats = 1; files_cnt--;)
91 1.1 glass dd_in();
92 1.1 glass
93 1.1 glass dd_close();
94 1.1 glass summary(0);
95 1.1 glass exit(0);
96 1.1 glass }
97 1.1 glass
98 1.1 glass static void
99 1.1 glass setup()
100 1.1 glass {
101 1.1 glass register u_int cnt;
102 1.1 glass struct stat sb;
103 1.1 glass struct mtget mt;
104 1.1 glass
105 1.1 glass if (in.name == NULL) {
106 1.1 glass in.name = "stdin";
107 1.1 glass in.fd = STDIN_FILENO;
108 1.1 glass } else {
109 1.1 glass in.fd = open(in.name, O_RDONLY, 0);
110 1.1 glass if (in.fd < 0)
111 1.1 glass err("%s: %s", in.name, strerror(errno));
112 1.1 glass }
113 1.1 glass
114 1.1 glass if (fstat(in.fd, &sb))
115 1.1 glass err("%s: %s", in.name, strerror(errno));
116 1.1 glass if (S_ISCHR(sb.st_mode))
117 1.1 glass in.flags |= ioctl(in.fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
118 1.1 glass else if (lseek(in.fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
119 1.1 glass in.flags |= ISPIPE; /* XXX fixed in 4.4BSD */
120 1.1 glass
121 1.1 glass if (files_cnt > 1 && !(in.flags & ISTAPE))
122 1.1 glass err("files is not supported for non-tape devices");
123 1.1 glass
124 1.1 glass if (out.name == NULL) {
125 1.1 glass /* No way to check for read access here. */
126 1.1 glass out.fd = STDOUT_FILENO;
127 1.1 glass out.name = "stdout";
128 1.1 glass } else {
129 1.1 glass #define OFLAGS \
130 1.1 glass (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
131 1.1 glass out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
132 1.1 glass /*
133 1.1 glass * May not have read access, so try again with write only.
134 1.1 glass * Without read we may have a problem if output also does
135 1.1 glass * not support seeks.
136 1.1 glass */
137 1.1 glass if (out.fd < 0) {
138 1.1 glass out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
139 1.1 glass out.flags |= NOREAD;
140 1.1 glass }
141 1.1 glass if (out.fd < 0)
142 1.1 glass err("%s: %s", out.name, strerror(errno));
143 1.1 glass }
144 1.1 glass
145 1.1 glass if (fstat(out.fd, &sb))
146 1.1 glass err("%s: %s", out.name, strerror(errno));
147 1.1 glass if (S_ISCHR(sb.st_mode))
148 1.1 glass out.flags |= ioctl(out.fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
149 1.1 glass else if (lseek(out.fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
150 1.1 glass out.flags |= ISPIPE; /* XXX fixed in 4.4BSD */
151 1.1 glass
152 1.1 glass /*
153 1.1 glass * Allocate space for the input and output buffers. If not doing
154 1.1 glass * record oriented I/O, only need a single buffer.
155 1.1 glass */
156 1.1 glass if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
157 1.1 glass if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
158 1.1 glass err("%s", strerror(errno));
159 1.1 glass out.db = in.db;
160 1.1 glass } else if ((in.db =
161 1.1 glass malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
162 1.1 glass (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL)
163 1.1 glass err("%s", strerror(errno));
164 1.1 glass in.dbp = in.db;
165 1.1 glass out.dbp = out.db;
166 1.1 glass
167 1.1 glass /* Position the input/output streams. */
168 1.1 glass if (in.offset)
169 1.1 glass pos_in();
170 1.1 glass if (out.offset)
171 1.1 glass pos_out();
172 1.1 glass
173 1.1 glass /*
174 1.1 glass * Truncate the output file; ignore errors because it fails on some
175 1.1 glass * kinds of output files, tapes, for example.
176 1.1 glass */
177 1.1 glass if (ddflags & (C_OF | C_SEEK | C_NOTRUNC) == (C_OF | C_SEEK))
178 1.1 glass (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
179 1.1 glass
180 1.1 glass /*
181 1.1 glass * If converting case at the same time as another conversion, build a
182 1.1 glass * table that does both at once. If just converting case, use the
183 1.1 glass * built-in tables.
184 1.1 glass */
185 1.1 glass if (ddflags & (C_LCASE|C_UCASE))
186 1.1 glass if (ddflags & C_ASCII)
187 1.1 glass if (ddflags & C_LCASE) {
188 1.1 glass for (cnt = 0; cnt < 0377; ++cnt)
189 1.1 glass if (isupper(ctab[cnt]))
190 1.1 glass ctab[cnt] = tolower(ctab[cnt]);
191 1.1 glass } else {
192 1.1 glass for (cnt = 0; cnt < 0377; ++cnt)
193 1.1 glass if (islower(ctab[cnt]))
194 1.1 glass ctab[cnt] = toupper(ctab[cnt]);
195 1.1 glass }
196 1.1 glass else if (ddflags & C_EBCDIC)
197 1.1 glass if (ddflags & C_LCASE) {
198 1.1 glass for (cnt = 0; cnt < 0377; ++cnt)
199 1.1 glass if (isupper(cnt))
200 1.1 glass ctab[cnt] = ctab[tolower(cnt)];
201 1.1 glass } else {
202 1.1 glass for (cnt = 0; cnt < 0377; ++cnt)
203 1.1 glass if (islower(cnt))
204 1.1 glass ctab[cnt] = ctab[toupper(cnt)];
205 1.1 glass }
206 1.1 glass else
207 1.1 glass ctab = ddflags & C_LCASE ? u2l : l2u;
208 1.1 glass (void)time(&st.start); /* Statistics timestamp. */
209 1.1 glass }
210 1.1 glass
211 1.1 glass static void
212 1.1 glass dd_in()
213 1.1 glass {
214 1.1 glass register int flags, n;
215 1.1 glass
216 1.1 glass for (flags = ddflags;;) {
217 1.1 glass if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
218 1.1 glass return;
219 1.1 glass
220 1.1 glass /*
221 1.1 glass * Zero the buffer first if trying to recover from errors so
222 1.1 glass * lose the minimum amount of data. If doing block operations
223 1.1 glass * use spaces.
224 1.1 glass */
225 1.1 glass if (flags & (C_NOERROR|C_SYNC))
226 1.1 glass if (flags & (C_BLOCK|C_UNBLOCK))
227 1.1 glass memset(in.dbp, ' ', in.dbsz);
228 1.1 glass else
229 1.1 glass memset(in.dbp, 0, in.dbsz);
230 1.1 glass
231 1.1 glass n = read(in.fd, in.dbp, in.dbsz);
232 1.1 glass if (n == 0) {
233 1.1 glass in.dbrcnt = 0;
234 1.1 glass return;
235 1.1 glass }
236 1.1 glass
237 1.1 glass /* Read error. */
238 1.1 glass if (n < 0) {
239 1.1 glass /*
240 1.1 glass * If noerror not specified, die. POSIX requires that
241 1.1 glass * the warning message be followed by an I/O display.
242 1.1 glass */
243 1.1 glass if (!(flags & C_NOERROR))
244 1.1 glass err("%s: %s", in.name, strerror(errno));
245 1.1 glass warn("%s: %s", in.name, strerror(errno));
246 1.1 glass summary(0);
247 1.1 glass
248 1.1 glass /*
249 1.1 glass * If it's not a tape drive or a pipe, seek past the
250 1.1 glass * error. If your OS doesn't do the right thing for
251 1.1 glass * raw disks this section should be modified to re-read
252 1.1 glass * in sector size chunks.
253 1.1 glass */
254 1.1 glass if (!(in.flags & (ISPIPE|ISTAPE)) &&
255 1.1 glass lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
256 1.1 glass warn("%s: %s", in.name, strerror(errno));
257 1.1 glass
258 1.1 glass /* If sync not specified, omit block and continue. */
259 1.1 glass if (!(ddflags & C_SYNC))
260 1.1 glass continue;
261 1.1 glass
262 1.1 glass /* Read errors count as full blocks. */
263 1.1 glass in.dbcnt += in.dbrcnt = in.dbsz;
264 1.1 glass ++st.in_full;
265 1.1 glass
266 1.1 glass /* Handle full input blocks. */
267 1.1 glass } else if (n == in.dbsz) {
268 1.1 glass in.dbcnt += in.dbrcnt = n;
269 1.1 glass ++st.in_full;
270 1.1 glass
271 1.1 glass /* Handle partial input blocks. */
272 1.1 glass } else {
273 1.1 glass /* If sync, use the entire block. */
274 1.1 glass if (ddflags & C_SYNC)
275 1.1 glass in.dbcnt += in.dbrcnt = in.dbsz;
276 1.1 glass else
277 1.1 glass in.dbcnt += in.dbrcnt = n;
278 1.1 glass ++st.in_part;
279 1.1 glass }
280 1.1 glass
281 1.1 glass /*
282 1.1 glass * POSIX states that if bs is set and no other conversions
283 1.1 glass * than noerror, notrunc or sync are specified, the block
284 1.1 glass * is output without buffering as it is read.
285 1.1 glass */
286 1.1 glass if (ddflags & C_BS) {
287 1.1 glass out.dbcnt = in.dbcnt;
288 1.1 glass dd_out(1);
289 1.1 glass in.dbcnt = 0;
290 1.1 glass continue;
291 1.1 glass }
292 1.1 glass
293 1.1 glass if (ddflags & C_SWAB) {
294 1.1 glass if ((n = in.dbcnt) & 1) {
295 1.1 glass ++st.swab;
296 1.1 glass --n;
297 1.1 glass }
298 1.1 glass swab(in.dbp, in.dbp, n);
299 1.1 glass }
300 1.1 glass
301 1.1 glass in.dbp += in.dbrcnt;
302 1.1 glass (*cfunc)();
303 1.1 glass }
304 1.1 glass }
305 1.1 glass
306 1.1 glass /*
307 1.1 glass * Cleanup any remaining I/O and flush output. If necesssary, output file
308 1.1 glass * is truncated.
309 1.1 glass */
310 1.1 glass static void
311 1.1 glass dd_close()
312 1.1 glass {
313 1.1 glass if (cfunc == def)
314 1.1 glass def_close();
315 1.1 glass else if (cfunc == block)
316 1.1 glass block_close();
317 1.1 glass else if (cfunc == unblock)
318 1.1 glass unblock_close();
319 1.1 glass if (out.dbcnt)
320 1.1 glass dd_out(1);
321 1.1 glass }
322 1.1 glass
323 1.1 glass void
324 1.1 glass dd_out(force)
325 1.1 glass int force;
326 1.1 glass {
327 1.1 glass static int warned;
328 1.1 glass register int cnt, n, nw;
329 1.1 glass register u_char *outp;
330 1.1 glass
331 1.1 glass /*
332 1.1 glass * Write one or more blocks out. The common case is writing a full
333 1.1 glass * output block in a single write; increment the full block stats.
334 1.1 glass * Otherwise, we're into partial block writes. If a partial write,
335 1.1 glass * and it's a character device, just warn. If a tape device, quit.
336 1.1 glass *
337 1.1 glass * The partial writes represent two cases. 1: Where the input block
338 1.1 glass * was less than expected so the output block was less than expected.
339 1.1 glass * 2: Where the input block was the right size but we were forced to
340 1.1 glass * write the block in multiple chunks. The original versions of dd(1)
341 1.1 glass * never wrote a block in more than a single write, so the latter case
342 1.1 glass * never happened.
343 1.1 glass *
344 1.1 glass * One special case is if we're forced to do the write -- in that case
345 1.1 glass * we play games with the buffer size, and it's usually a partial write.
346 1.1 glass */
347 1.1 glass outp = out.db;
348 1.1 glass for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
349 1.1 glass for (cnt = n;; cnt -= nw) {
350 1.1 glass nw = write(out.fd, outp, cnt);
351 1.1 glass if (nw < 0)
352 1.1 glass err("%s: %s", out.name, strerror(errno));
353 1.1 glass outp += nw;
354 1.1 glass st.bytes += nw;
355 1.1 glass if (nw == n) {
356 1.1 glass if (n != out.dbsz)
357 1.1 glass ++st.out_part;
358 1.1 glass else
359 1.1 glass ++st.out_full;
360 1.1 glass break;
361 1.1 glass }
362 1.1 glass ++st.out_part;
363 1.1 glass if (nw == cnt)
364 1.1 glass break;
365 1.1 glass if (out.flags & ISCHR && !warned) {
366 1.1 glass warned = 1;
367 1.1 glass warn("%s: short write on character device",
368 1.1 glass out.name);
369 1.1 glass }
370 1.1 glass if (out.flags & ISTAPE)
371 1.1 glass err("%s: short write on tape device", out.name);
372 1.1 glass }
373 1.1 glass if ((out.dbcnt -= n) < out.dbsz)
374 1.1 glass break;
375 1.1 glass }
376 1.1 glass
377 1.1 glass /* Reassemble the output block. */
378 1.1 glass if (out.dbcnt)
379 1.1 glass memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
380 1.1 glass out.dbp = out.db + out.dbcnt;
381 1.1 glass }
382