17bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes/*	$NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $	*/
27bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
37bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes/*-
47bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * Copyright (c) 1991, 1993, 1994
57bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes *	The Regents of the University of California.  All rights reserved.
67bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes *
77bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * This code is derived from software contributed to Berkeley by
87bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * Keith Muller of the University of California, San Diego and Lance
97bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * Visser of Convex Computer Corporation.
107bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes *
117bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * Redistribution and use in source and binary forms, with or without
127bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * modification, are permitted provided that the following conditions
137bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * are met:
147bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * 1. Redistributions of source code must retain the above copyright
157bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes *    notice, this list of conditions and the following disclaimer.
167bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
177bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes *    notice, this list of conditions and the following disclaimer in the
187bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes *    documentation and/or other materials provided with the distribution.
197bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * 3. Neither the name of the University nor the names of its contributors
207bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes *    may be used to endorse or promote products derived from this software
217bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes *    without specific prior written permission.
227bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes *
237bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
247bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
257bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
267bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
277bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
287bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
297bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
307bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
317bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
327bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
337bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * SUCH DAMAGE.
347bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes */
357bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
367bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <sys/cdefs.h>
377bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#ifndef lint
387bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#if 0
397bb5660647d0106f96b000c25f5690a45734c38cElliott Hughesstatic char sccsid[] = "@(#)position.c	8.3 (Berkeley) 4/2/94";
407bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#else
417bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes__RCSID("$NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $");
427bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#endif
437bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#endif /* not lint */
447bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
457bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <sys/types.h>
467bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <sys/stat.h>
477bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <sys/ioctl.h>
487bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <sys/mtio.h>
497bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <sys/time.h>
507bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
517bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <err.h>
527bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <errno.h>
537bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <stdlib.h>
547bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <string.h>
557bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <unistd.h>
567bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
577bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include "dd.h"
587bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include "extern.h"
597bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
607bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes/*
617bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * Position input/output data streams before starting the copy.  Device type
627bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * dependent.  Seekable devices use lseek, and the rest position by reading.
637bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * Seeking past the end of file can cause null blocks to be written to the
647bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * output.
657bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes */
667bb5660647d0106f96b000c25f5690a45734c38cElliott Hughesvoid
677bb5660647d0106f96b000c25f5690a45734c38cElliott Hughespos_in(void)
687bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes{
697bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	int bcnt, cnt, nr, warned;
707bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
717bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	/* If not a pipe or tape device, try to seek on it. */
727bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	if (!(in.flags & (ISPIPE|ISTAPE))) {
737bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		if (ddop_lseek(in, in.fd,
747bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		    (off_t)in.offset * (off_t)in.dbsz, SEEK_CUR) == -1) {
757bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			err(EXIT_FAILURE, "%s", in.name);
767bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			/* NOTREACHED */
777bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		}
787bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		return;
797bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		/* NOTREACHED */
807bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	}
817bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
827bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	/*
837bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	 * Read the data.  If a pipe, read until satisfy the number of bytes
847bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	 * being skipped.  No differentiation for reading complete and partial
857bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	 * blocks for other devices.
867bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	 */
877bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
887bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		if ((nr = ddop_read(in, in.fd, in.db, bcnt)) > 0) {
897bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			if (in.flags & ISPIPE) {
907bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				if (!(bcnt -= nr)) {
917bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes					bcnt = in.dbsz;
927bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes					--cnt;
937bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				}
947bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			} else
957bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				--cnt;
967bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			continue;
977bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		}
987bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
997bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		if (nr == 0) {
1007bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			if (files_cnt > 1) {
1017bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				--files_cnt;
1027bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				continue;
1037bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			}
1047bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			errx(EXIT_FAILURE, "skip reached end of input");
1057bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			/* NOTREACHED */
1067bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		}
1077bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1087bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		/*
1097bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		 * Input error -- either EOF with no more files, or I/O error.
1107bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		 * If noerror not set die.  POSIX requires that the warning
1117bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		 * message be followed by an I/O display.
1127bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		 */
1137bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		if (ddflags & C_NOERROR) {
1147bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			if (!warned) {
1157bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1167bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				warn("%s", in.name);
1177bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				warned = 1;
1187bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				summary();
1197bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			}
1207bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			continue;
1217bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		}
1227bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		err(EXIT_FAILURE, "%s", in.name);
1237bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		/* NOTREACHED */
1247bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	}
1257bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes}
1267bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1277bb5660647d0106f96b000c25f5690a45734c38cElliott Hughesvoid
1287bb5660647d0106f96b000c25f5690a45734c38cElliott Hughespos_out(void)
1297bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes{
1307bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	struct mtop t_op;
1317bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	int n;
1327bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	uint64_t cnt;
1337bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1347bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	/*
1357bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	 * If not a tape, try seeking on the file.  Seeking on a pipe is
1367bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	 * going to fail, but don't protect the user -- they shouldn't
1377bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	 * have specified the seek operand.
1387bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	 */
1397bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	if (!(out.flags & ISTAPE)) {
1407bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		if (ddop_lseek(out, out.fd,
1417bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		    (off_t)out.offset * (off_t)out.dbsz, SEEK_SET) == -1)
1427bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			err(EXIT_FAILURE, "%s", out.name);
1437bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			/* NOTREACHED */
1447bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		return;
1457bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	}
1467bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1477bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	/* If no read access, try using mtio. */
1487bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	if (out.flags & NOREAD) {
1497bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		t_op.mt_op = MTFSR;
1507bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		t_op.mt_count = out.offset;
1517bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1527bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) < 0)
1537bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			err(EXIT_FAILURE, "%s", out.name);
1547bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			/* NOTREACHED */
1557bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		return;
1567bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	}
1577bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1587bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	/* Read it. */
1597bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	for (cnt = 0; cnt < out.offset; ++cnt) {
1607bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		if ((n = ddop_read(out, out.fd, out.db, out.dbsz)) > 0)
1617bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			continue;
1627bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1637bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		if (n < 0)
1647bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			err(EXIT_FAILURE, "%s", out.name);
1657bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			/* NOTREACHED */
1667bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1677bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		/*
1687bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		 * If reach EOF, fill with NUL characters; first, back up over
1697bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		 * the EOF mark.  Note, cnt has not yet been incremented, so
1707bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		 * the EOF read does not count as a seek'd block.
1717bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		 */
1727bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		t_op.mt_op = MTBSR;
1737bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		t_op.mt_count = 1;
1747bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) == -1)
1757bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			err(EXIT_FAILURE, "%s", out.name);
1767bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			/* NOTREACHED */
1777bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes
1787bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		while (cnt++ < out.offset)
1797bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			if ((uint64_t)(n = bwrite(&out,
1807bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes			    out.db, out.dbsz)) != out.dbsz)
1817bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				err(EXIT_FAILURE, "%s", out.name);
1827bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes				/* NOTREACHED */
1837bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes		break;
1847bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes	}
1857bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes}
186