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