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