position.c revision 51249
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Keith Muller of the University of California, San Diego and Lance
71556Srgrimes * Visser of Convex Computer Corporation.
81556Srgrimes *
91556Srgrimes * Redistribution and use in source and binary forms, with or without
101556Srgrimes * modification, are permitted provided that the following conditions
111556Srgrimes * are met:
121556Srgrimes * 1. Redistributions of source code must retain the above copyright
131556Srgrimes *    notice, this list of conditions and the following disclaimer.
141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151556Srgrimes *    notice, this list of conditions and the following disclaimer in the
161556Srgrimes *    documentation and/or other materials provided with the distribution.
171556Srgrimes * 3. All advertising materials mentioning features or use of this software
181556Srgrimes *    must display the following acknowledgement:
191556Srgrimes *	This product includes software developed by the University of
201556Srgrimes *	California, Berkeley and its contributors.
211556Srgrimes * 4. Neither the name of the University nor the names of its contributors
221556Srgrimes *    may be used to endorse or promote products derived from this software
231556Srgrimes *    without specific prior written permission.
241556Srgrimes *
251556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351556Srgrimes * SUCH DAMAGE.
361556Srgrimes */
371556Srgrimes
381556Srgrimes#ifndef lint
3935773Scharnier#if 0
4036007Scharnierstatic char sccsid[] = "@(#)position.c	8.3 (Berkeley) 4/2/94";
4135773Scharnier#endif
4235773Scharnierstatic const char rcsid[] =
4350471Speter  "$FreeBSD: head/bin/dd/position.c 51249 1999-09-13 21:47:10Z green $";
441556Srgrimes#endif /* not lint */
451556Srgrimes
4636007Scharnier#include <sys/types.h>
471556Srgrimes#include <sys/mtio.h>
481556Srgrimes
491556Srgrimes#include <err.h>
5051137Sgreen#include <errno.h>
511556Srgrimes#include <unistd.h>
521556Srgrimes
531556Srgrimes#include "dd.h"
541556Srgrimes#include "extern.h"
551556Srgrimes
561556Srgrimes/*
571556Srgrimes * Position input/output data streams before starting the copy.  Device type
581556Srgrimes * dependent.  Seekable devices use lseek, and the rest position by reading.
591556Srgrimes * Seeking past the end of file can cause null blocks to be written to the
601556Srgrimes * output.
611556Srgrimes */
621556Srgrimesvoid
631556Srgrimespos_in()
641556Srgrimes{
6551208Sgreen	off_t cnt;
6651208Sgreen	int warned;
6748026Sgreen	ssize_t nr;
6851208Sgreen	size_t bcnt;
691556Srgrimes
7051249Sgreen	/* If known to be seekable, try to seek on it. */
7151249Sgreen	if (in.flags & ISSEEK) {
7251137Sgreen		errno = 0;
7351208Sgreen		if (lseek(in.fd, in.offset * in.dbsz, SEEK_CUR) == -1 &&
7451208Sgreen		    errno != 0)
751556Srgrimes			err(1, "%s", in.name);
761556Srgrimes		return;
771556Srgrimes	}
781556Srgrimes
791556Srgrimes	/*
801556Srgrimes	 * Read the data.  If a pipe, read until satisfy the number of bytes
811556Srgrimes	 * being skipped.  No differentiation for reading complete and partial
821556Srgrimes	 * blocks for other devices.
831556Srgrimes	 */
841556Srgrimes	for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
851556Srgrimes		if ((nr = read(in.fd, in.db, bcnt)) > 0) {
861556Srgrimes			if (in.flags & ISPIPE) {
871556Srgrimes				if (!(bcnt -= nr)) {
881556Srgrimes					bcnt = in.dbsz;
891556Srgrimes					--cnt;
901556Srgrimes				}
911556Srgrimes			} else
921556Srgrimes				--cnt;
931556Srgrimes			continue;
941556Srgrimes		}
951556Srgrimes
961556Srgrimes		if (nr == 0) {
971556Srgrimes			if (files_cnt > 1) {
981556Srgrimes				--files_cnt;
991556Srgrimes				continue;
1001556Srgrimes			}
1011556Srgrimes			errx(1, "skip reached end of input");
1021556Srgrimes		}
1031556Srgrimes
1041556Srgrimes		/*
1051556Srgrimes		 * Input error -- either EOF with no more files, or I/O error.
1061556Srgrimes		 * If noerror not set die.  POSIX requires that the warning
1071556Srgrimes		 * message be followed by an I/O display.
1081556Srgrimes		 */
1091556Srgrimes		if (ddflags & C_NOERROR) {
1101556Srgrimes			if (!warned) {
1111556Srgrimes				warn("%s", in.name);
1121556Srgrimes				warned = 1;
1131556Srgrimes				summary();
1141556Srgrimes			}
1151556Srgrimes			continue;
1161556Srgrimes		}
1171556Srgrimes		err(1, "%s", in.name);
1181556Srgrimes	}
1191556Srgrimes}
1201556Srgrimes
1211556Srgrimesvoid
1221556Srgrimespos_out()
1231556Srgrimes{
1241556Srgrimes	struct mtop t_op;
12548026Sgreen	off_t cnt;
12648026Sgreen	ssize_t n;
1271556Srgrimes
12851249Sgreen	/*
12951249Sgreen	 * If not a tape, try seeking on the file.  Seeking on a pipe is
13051249Sgreen	 * going to fail, but don't protect the user -- they shouldn't
13151249Sgreen	 * have specified the seek operand.
13251249Sgreen	 */
13351249Sgreen	if (!(out.flags & ISTAPE)) {
13451137Sgreen		errno = 0;
13551137Sgreen		if (lseek(out.fd, out.offset * out.dbsz, SEEK_SET) == -1 &&
13651208Sgreen		    errno != 0)
1371556Srgrimes			err(1, "%s", out.name);
1381556Srgrimes		return;
1391556Srgrimes	}
1401556Srgrimes
1411556Srgrimes	/* If no read access, try using mtio. */
1421556Srgrimes	if (out.flags & NOREAD) {
1431556Srgrimes		t_op.mt_op = MTFSR;
1441556Srgrimes		t_op.mt_count = out.offset;
1451556Srgrimes
14648026Sgreen		if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)
1471556Srgrimes			err(1, "%s", out.name);
1481556Srgrimes		return;
1491556Srgrimes	}
1501556Srgrimes
1511556Srgrimes	/* Read it. */
1521556Srgrimes	for (cnt = 0; cnt < out.offset; ++cnt) {
1531556Srgrimes		if ((n = read(out.fd, out.db, out.dbsz)) > 0)
1541556Srgrimes			continue;
1551556Srgrimes
15648026Sgreen		if (n == -1)
1571556Srgrimes			err(1, "%s", out.name);
1581556Srgrimes
1591556Srgrimes		/*
1601556Srgrimes		 * If reach EOF, fill with NUL characters; first, back up over
1611556Srgrimes		 * the EOF mark.  Note, cnt has not yet been incremented, so
1621556Srgrimes		 * the EOF read does not count as a seek'd block.
1631556Srgrimes		 */
1641556Srgrimes		t_op.mt_op = MTBSR;
1651556Srgrimes		t_op.mt_count = 1;
1661556Srgrimes		if (ioctl(out.fd, MTIOCTOP, &t_op) == -1)
1671556Srgrimes			err(1, "%s", out.name);
1681556Srgrimes
1691556Srgrimes		while (cnt++ < out.offset)
1701556Srgrimes			if ((n = write(out.fd, out.db, out.dbsz)) != out.dbsz)
1711556Srgrimes				err(1, "%s", out.name);
1721556Srgrimes		break;
1731556Srgrimes	}
1741556Srgrimes}
175