position.c revision 111629
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 421556Srgrimes#endif /* not lint */ 4399109Sobrien#include <sys/cdefs.h> 4499109Sobrien__FBSDID("$FreeBSD: head/bin/dd/position.c 111629 2003-02-27 18:04:54Z markm $"); 451556Srgrimes 4636007Scharnier#include <sys/types.h> 471556Srgrimes#include <sys/mtio.h> 481556Srgrimes 491556Srgrimes#include <err.h> 5051137Sgreen#include <errno.h> 51111629Smarkm#include <inttypes.h> 521556Srgrimes#include <unistd.h> 531556Srgrimes 541556Srgrimes#include "dd.h" 551556Srgrimes#include "extern.h" 561556Srgrimes 571556Srgrimes/* 581556Srgrimes * Position input/output data streams before starting the copy. Device type 591556Srgrimes * dependent. Seekable devices use lseek, and the rest position by reading. 601556Srgrimes * Seeking past the end of file can cause null blocks to be written to the 611556Srgrimes * output. 621556Srgrimes */ 631556Srgrimesvoid 6490108Simppos_in(void) 651556Srgrimes{ 6651208Sgreen off_t cnt; 6751208Sgreen int warned; 6848026Sgreen ssize_t nr; 6951208Sgreen size_t bcnt; 701556Srgrimes 7151249Sgreen /* If known to be seekable, try to seek on it. */ 7251249Sgreen if (in.flags & ISSEEK) { 7351137Sgreen errno = 0; 7451208Sgreen if (lseek(in.fd, in.offset * in.dbsz, SEEK_CUR) == -1 && 7551208Sgreen errno != 0) 761556Srgrimes err(1, "%s", in.name); 771556Srgrimes return; 781556Srgrimes } 791556Srgrimes 8067451Sgreen /* Don't try to read a really weird amount (like negative). */ 8167451Sgreen if (in.offset < 0) 8267451Sgreen errx(1, "%s: illegal offset", "iseek/skip"); 8367451Sgreen 841556Srgrimes /* 851556Srgrimes * Read the data. If a pipe, read until satisfy the number of bytes 861556Srgrimes * being skipped. No differentiation for reading complete and partial 871556Srgrimes * blocks for other devices. 881556Srgrimes */ 891556Srgrimes for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) { 901556Srgrimes if ((nr = read(in.fd, in.db, bcnt)) > 0) { 911556Srgrimes if (in.flags & ISPIPE) { 921556Srgrimes if (!(bcnt -= nr)) { 931556Srgrimes bcnt = in.dbsz; 941556Srgrimes --cnt; 951556Srgrimes } 961556Srgrimes } else 971556Srgrimes --cnt; 981556Srgrimes continue; 991556Srgrimes } 1001556Srgrimes 1011556Srgrimes if (nr == 0) { 1021556Srgrimes if (files_cnt > 1) { 1031556Srgrimes --files_cnt; 1041556Srgrimes continue; 1051556Srgrimes } 1061556Srgrimes errx(1, "skip reached end of input"); 1071556Srgrimes } 1081556Srgrimes 1091556Srgrimes /* 1101556Srgrimes * Input error -- either EOF with no more files, or I/O error. 1111556Srgrimes * If noerror not set die. POSIX requires that the warning 1121556Srgrimes * message be followed by an I/O display. 1131556Srgrimes */ 1141556Srgrimes if (ddflags & C_NOERROR) { 1151556Srgrimes if (!warned) { 1161556Srgrimes warn("%s", in.name); 1171556Srgrimes warned = 1; 1181556Srgrimes summary(); 1191556Srgrimes } 1201556Srgrimes continue; 1211556Srgrimes } 1221556Srgrimes err(1, "%s", in.name); 1231556Srgrimes } 1241556Srgrimes} 1251556Srgrimes 1261556Srgrimesvoid 12790108Simppos_out(void) 1281556Srgrimes{ 1291556Srgrimes struct mtop t_op; 13048026Sgreen off_t cnt; 13148026Sgreen ssize_t n; 1321556Srgrimes 13351249Sgreen /* 13451249Sgreen * If not a tape, try seeking on the file. Seeking on a pipe is 13551249Sgreen * going to fail, but don't protect the user -- they shouldn't 13651249Sgreen * have specified the seek operand. 13751249Sgreen */ 13862311Sgreen if (out.flags & (ISSEEK | ISPIPE)) { 13951137Sgreen errno = 0; 14051335Sgreen if (lseek(out.fd, out.offset * out.dbsz, SEEK_CUR) == -1 && 14151208Sgreen errno != 0) 1421556Srgrimes err(1, "%s", out.name); 1431556Srgrimes return; 1441556Srgrimes } 1451556Srgrimes 14667451Sgreen /* Don't try to read a really weird amount (like negative). */ 14767451Sgreen if (out.offset < 0) 14867451Sgreen errx(1, "%s: illegal offset", "oseek/seek"); 14967451Sgreen 1501556Srgrimes /* If no read access, try using mtio. */ 1511556Srgrimes if (out.flags & NOREAD) { 1521556Srgrimes t_op.mt_op = MTFSR; 1531556Srgrimes t_op.mt_count = out.offset; 1541556Srgrimes 15548026Sgreen if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) 1561556Srgrimes err(1, "%s", out.name); 1571556Srgrimes return; 1581556Srgrimes } 1591556Srgrimes 1601556Srgrimes /* Read it. */ 1611556Srgrimes for (cnt = 0; cnt < out.offset; ++cnt) { 1621556Srgrimes if ((n = read(out.fd, out.db, out.dbsz)) > 0) 1631556Srgrimes continue; 1641556Srgrimes 16548026Sgreen if (n == -1) 1661556Srgrimes err(1, "%s", out.name); 1671556Srgrimes 1681556Srgrimes /* 1691556Srgrimes * If reach EOF, fill with NUL characters; first, back up over 1701556Srgrimes * the EOF mark. Note, cnt has not yet been incremented, so 1711556Srgrimes * the EOF read does not count as a seek'd block. 1721556Srgrimes */ 1731556Srgrimes t_op.mt_op = MTBSR; 1741556Srgrimes t_op.mt_count = 1; 1751556Srgrimes if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) 1761556Srgrimes err(1, "%s", out.name); 1771556Srgrimes 17862311Sgreen while (cnt++ < out.offset) { 17962311Sgreen n = write(out.fd, out.db, out.dbsz); 18062311Sgreen if (n == -1) 1811556Srgrimes err(1, "%s", out.name); 18262311Sgreen if ((size_t)n != out.dbsz) 18362311Sgreen errx(1, "%s: write failure", out.name); 18462311Sgreen } 1851556Srgrimes break; 1861556Srgrimes } 1871556Srgrimes} 188