xref: /minix3/bin/dd/position.c (revision c19d619d42588dfcfbd5c23c7290d975d0294e65)
1*c19d619dSJacob Adams /*	$NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $	*/
2*c19d619dSJacob Adams 
3*c19d619dSJacob Adams /*-
4*c19d619dSJacob Adams  * Copyright (c) 1991, 1993, 1994
5*c19d619dSJacob Adams  *	The Regents of the University of California.  All rights reserved.
6*c19d619dSJacob Adams  *
7*c19d619dSJacob Adams  * This code is derived from software contributed to Berkeley by
8*c19d619dSJacob Adams  * Keith Muller of the University of California, San Diego and Lance
9*c19d619dSJacob Adams  * Visser of Convex Computer Corporation.
10*c19d619dSJacob Adams  *
11*c19d619dSJacob Adams  * Redistribution and use in source and binary forms, with or without
12*c19d619dSJacob Adams  * modification, are permitted provided that the following conditions
13*c19d619dSJacob Adams  * are met:
14*c19d619dSJacob Adams  * 1. Redistributions of source code must retain the above copyright
15*c19d619dSJacob Adams  *    notice, this list of conditions and the following disclaimer.
16*c19d619dSJacob Adams  * 2. Redistributions in binary form must reproduce the above copyright
17*c19d619dSJacob Adams  *    notice, this list of conditions and the following disclaimer in the
18*c19d619dSJacob Adams  *    documentation and/or other materials provided with the distribution.
19*c19d619dSJacob Adams  * 3. Neither the name of the University nor the names of its contributors
20*c19d619dSJacob Adams  *    may be used to endorse or promote products derived from this software
21*c19d619dSJacob Adams  *    without specific prior written permission.
22*c19d619dSJacob Adams  *
23*c19d619dSJacob Adams  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*c19d619dSJacob Adams  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*c19d619dSJacob Adams  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*c19d619dSJacob Adams  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*c19d619dSJacob Adams  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*c19d619dSJacob Adams  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*c19d619dSJacob Adams  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*c19d619dSJacob Adams  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*c19d619dSJacob Adams  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*c19d619dSJacob Adams  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*c19d619dSJacob Adams  * SUCH DAMAGE.
34*c19d619dSJacob Adams  */
35*c19d619dSJacob Adams 
36*c19d619dSJacob Adams #include <sys/cdefs.h>
37*c19d619dSJacob Adams #ifndef lint
38*c19d619dSJacob Adams #if 0
39*c19d619dSJacob Adams static char sccsid[] = "@(#)position.c	8.3 (Berkeley) 4/2/94";
40*c19d619dSJacob Adams #else
41*c19d619dSJacob Adams __RCSID("$NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $");
42*c19d619dSJacob Adams #endif
43*c19d619dSJacob Adams #endif /* not lint */
44*c19d619dSJacob Adams 
45*c19d619dSJacob Adams #include <sys/types.h>
46*c19d619dSJacob Adams #include <sys/stat.h>
47*c19d619dSJacob Adams #include <sys/ioctl.h>
48*c19d619dSJacob Adams #include <sys/mtio.h>
49*c19d619dSJacob Adams #include <sys/time.h>
50*c19d619dSJacob Adams 
51*c19d619dSJacob Adams #include <err.h>
52*c19d619dSJacob Adams #include <errno.h>
53*c19d619dSJacob Adams #include <stdlib.h>
54*c19d619dSJacob Adams #include <string.h>
55*c19d619dSJacob Adams #include <unistd.h>
56*c19d619dSJacob Adams 
57*c19d619dSJacob Adams #include "dd.h"
58*c19d619dSJacob Adams #include "extern.h"
59*c19d619dSJacob Adams 
60*c19d619dSJacob Adams /*
61*c19d619dSJacob Adams  * Position input/output data streams before starting the copy.  Device type
62*c19d619dSJacob Adams  * dependent.  Seekable devices use lseek, and the rest position by reading.
63*c19d619dSJacob Adams  * Seeking past the end of file can cause null blocks to be written to the
64*c19d619dSJacob Adams  * output.
65*c19d619dSJacob Adams  */
66*c19d619dSJacob Adams void
pos_in(void)67*c19d619dSJacob Adams pos_in(void)
68*c19d619dSJacob Adams {
69*c19d619dSJacob Adams 	int bcnt, cnt, nr, warned;
70*c19d619dSJacob Adams 
71*c19d619dSJacob Adams 	/* If not a pipe or tape device, try to seek on it. */
72*c19d619dSJacob Adams 	if (!(in.flags & (ISPIPE|ISTAPE))) {
73*c19d619dSJacob Adams 		if (ddop_lseek(in, in.fd,
74*c19d619dSJacob Adams 		    (off_t)in.offset * (off_t)in.dbsz, SEEK_CUR) == -1) {
75*c19d619dSJacob Adams 			err(EXIT_FAILURE, "%s", in.name);
76*c19d619dSJacob Adams 			/* NOTREACHED */
77*c19d619dSJacob Adams 		}
78*c19d619dSJacob Adams 		return;
79*c19d619dSJacob Adams 		/* NOTREACHED */
80*c19d619dSJacob Adams 	}
81*c19d619dSJacob Adams 
82*c19d619dSJacob Adams 	/*
83*c19d619dSJacob Adams 	 * Read the data.  If a pipe, read until satisfy the number of bytes
84*c19d619dSJacob Adams 	 * being skipped.  No differentiation for reading complete and partial
85*c19d619dSJacob Adams 	 * blocks for other devices.
86*c19d619dSJacob Adams 	 */
87*c19d619dSJacob Adams 	for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
88*c19d619dSJacob Adams 		if ((nr = ddop_read(in, in.fd, in.db, bcnt)) > 0) {
89*c19d619dSJacob Adams 			if (in.flags & ISPIPE) {
90*c19d619dSJacob Adams 				if (!(bcnt -= nr)) {
91*c19d619dSJacob Adams 					bcnt = in.dbsz;
92*c19d619dSJacob Adams 					--cnt;
93*c19d619dSJacob Adams 				}
94*c19d619dSJacob Adams 			} else
95*c19d619dSJacob Adams 				--cnt;
96*c19d619dSJacob Adams 			continue;
97*c19d619dSJacob Adams 		}
98*c19d619dSJacob Adams 
99*c19d619dSJacob Adams 		if (nr == 0) {
100*c19d619dSJacob Adams 			if (files_cnt > 1) {
101*c19d619dSJacob Adams 				--files_cnt;
102*c19d619dSJacob Adams 				continue;
103*c19d619dSJacob Adams 			}
104*c19d619dSJacob Adams 			errx(EXIT_FAILURE, "skip reached end of input");
105*c19d619dSJacob Adams 			/* NOTREACHED */
106*c19d619dSJacob Adams 		}
107*c19d619dSJacob Adams 
108*c19d619dSJacob Adams 		/*
109*c19d619dSJacob Adams 		 * Input error -- either EOF with no more files, or I/O error.
110*c19d619dSJacob Adams 		 * If noerror not set die.  POSIX requires that the warning
111*c19d619dSJacob Adams 		 * message be followed by an I/O display.
112*c19d619dSJacob Adams 		 */
113*c19d619dSJacob Adams 		if (ddflags & C_NOERROR) {
114*c19d619dSJacob Adams 			if (!warned) {
115*c19d619dSJacob Adams 
116*c19d619dSJacob Adams 				warn("%s", in.name);
117*c19d619dSJacob Adams 				warned = 1;
118*c19d619dSJacob Adams 				summary();
119*c19d619dSJacob Adams 			}
120*c19d619dSJacob Adams 			continue;
121*c19d619dSJacob Adams 		}
122*c19d619dSJacob Adams 		err(EXIT_FAILURE, "%s", in.name);
123*c19d619dSJacob Adams 		/* NOTREACHED */
124*c19d619dSJacob Adams 	}
125*c19d619dSJacob Adams }
126*c19d619dSJacob Adams 
127*c19d619dSJacob Adams void
pos_out(void)128*c19d619dSJacob Adams pos_out(void)
129*c19d619dSJacob Adams {
130*c19d619dSJacob Adams 	struct mtop t_op;
131*c19d619dSJacob Adams 	int n;
132*c19d619dSJacob Adams 	uint64_t cnt;
133*c19d619dSJacob Adams 
134*c19d619dSJacob Adams 	/*
135*c19d619dSJacob Adams 	 * If not a tape, try seeking on the file.  Seeking on a pipe is
136*c19d619dSJacob Adams 	 * going to fail, but don't protect the user -- they shouldn't
137*c19d619dSJacob Adams 	 * have specified the seek operand.
138*c19d619dSJacob Adams 	 */
139*c19d619dSJacob Adams 	if (!(out.flags & ISTAPE)) {
140*c19d619dSJacob Adams 		if (ddop_lseek(out, out.fd,
141*c19d619dSJacob Adams 		    (off_t)out.offset * (off_t)out.dbsz, SEEK_SET) == -1)
142*c19d619dSJacob Adams 			err(EXIT_FAILURE, "%s", out.name);
143*c19d619dSJacob Adams 			/* NOTREACHED */
144*c19d619dSJacob Adams 		return;
145*c19d619dSJacob Adams 	}
146*c19d619dSJacob Adams 
147*c19d619dSJacob Adams 	/* If no read access, try using mtio. */
148*c19d619dSJacob Adams 	if (out.flags & NOREAD) {
149*c19d619dSJacob Adams 		t_op.mt_op = MTFSR;
150*c19d619dSJacob Adams 		t_op.mt_count = out.offset;
151*c19d619dSJacob Adams 
152*c19d619dSJacob Adams 		if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) < 0)
153*c19d619dSJacob Adams 			err(EXIT_FAILURE, "%s", out.name);
154*c19d619dSJacob Adams 			/* NOTREACHED */
155*c19d619dSJacob Adams 		return;
156*c19d619dSJacob Adams 	}
157*c19d619dSJacob Adams 
158*c19d619dSJacob Adams 	/* Read it. */
159*c19d619dSJacob Adams 	for (cnt = 0; cnt < out.offset; ++cnt) {
160*c19d619dSJacob Adams 		if ((n = ddop_read(out, out.fd, out.db, out.dbsz)) > 0)
161*c19d619dSJacob Adams 			continue;
162*c19d619dSJacob Adams 
163*c19d619dSJacob Adams 		if (n < 0)
164*c19d619dSJacob Adams 			err(EXIT_FAILURE, "%s", out.name);
165*c19d619dSJacob Adams 			/* NOTREACHED */
166*c19d619dSJacob Adams 
167*c19d619dSJacob Adams 		/*
168*c19d619dSJacob Adams 		 * If reach EOF, fill with NUL characters; first, back up over
169*c19d619dSJacob Adams 		 * the EOF mark.  Note, cnt has not yet been incremented, so
170*c19d619dSJacob Adams 		 * the EOF read does not count as a seek'd block.
171*c19d619dSJacob Adams 		 */
172*c19d619dSJacob Adams 		t_op.mt_op = MTBSR;
173*c19d619dSJacob Adams 		t_op.mt_count = 1;
174*c19d619dSJacob Adams 		if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) == -1)
175*c19d619dSJacob Adams 			err(EXIT_FAILURE, "%s", out.name);
176*c19d619dSJacob Adams 			/* NOTREACHED */
177*c19d619dSJacob Adams 
178*c19d619dSJacob Adams 		while (cnt++ < out.offset)
179*c19d619dSJacob Adams 			if ((uint64_t)(n = bwrite(&out,
180*c19d619dSJacob Adams 			    out.db, out.dbsz)) != out.dbsz)
181*c19d619dSJacob Adams 				err(EXIT_FAILURE, "%s", out.name);
182*c19d619dSJacob Adams 				/* NOTREACHED */
183*c19d619dSJacob Adams 		break;
184*c19d619dSJacob Adams 	}
185*c19d619dSJacob Adams }
186