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