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