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