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