position.c revision 67451
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 67451 2000-10-22 23:00:32Z 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 7967451Sgreen /* Don't try to read a really weird amount (like negative). */ 8067451Sgreen if (in.offset < 0) 8167451Sgreen errx(1, "%s: illegal offset", "iseek/skip"); 8267451Sgreen 831556Srgrimes /* 841556Srgrimes * Read the data. If a pipe, read until satisfy the number of bytes 851556Srgrimes * being skipped. No differentiation for reading complete and partial 861556Srgrimes * blocks for other devices. 871556Srgrimes */ 881556Srgrimes for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) { 891556Srgrimes if ((nr = read(in.fd, in.db, bcnt)) > 0) { 901556Srgrimes if (in.flags & ISPIPE) { 911556Srgrimes if (!(bcnt -= nr)) { 921556Srgrimes bcnt = in.dbsz; 931556Srgrimes --cnt; 941556Srgrimes } 951556Srgrimes } else 961556Srgrimes --cnt; 971556Srgrimes continue; 981556Srgrimes } 991556Srgrimes 1001556Srgrimes if (nr == 0) { 1011556Srgrimes if (files_cnt > 1) { 1021556Srgrimes --files_cnt; 1031556Srgrimes continue; 1041556Srgrimes } 1051556Srgrimes errx(1, "skip reached end of input"); 1061556Srgrimes } 1071556Srgrimes 1081556Srgrimes /* 1091556Srgrimes * Input error -- either EOF with no more files, or I/O error. 1101556Srgrimes * If noerror not set die. POSIX requires that the warning 1111556Srgrimes * message be followed by an I/O display. 1121556Srgrimes */ 1131556Srgrimes if (ddflags & C_NOERROR) { 1141556Srgrimes if (!warned) { 1151556Srgrimes warn("%s", in.name); 1161556Srgrimes warned = 1; 1171556Srgrimes summary(); 1181556Srgrimes } 1191556Srgrimes continue; 1201556Srgrimes } 1211556Srgrimes err(1, "%s", in.name); 1221556Srgrimes } 1231556Srgrimes} 1241556Srgrimes 1251556Srgrimesvoid 1261556Srgrimespos_out() 1271556Srgrimes{ 1281556Srgrimes struct mtop t_op; 12948026Sgreen off_t cnt; 13048026Sgreen ssize_t n; 1311556Srgrimes 13251249Sgreen /* 13351249Sgreen * If not a tape, try seeking on the file. Seeking on a pipe is 13451249Sgreen * going to fail, but don't protect the user -- they shouldn't 13551249Sgreen * have specified the seek operand. 13651249Sgreen */ 13762311Sgreen if (out.flags & (ISSEEK | ISPIPE)) { 13851137Sgreen errno = 0; 13951335Sgreen if (lseek(out.fd, out.offset * out.dbsz, SEEK_CUR) == -1 && 14051208Sgreen errno != 0) 1411556Srgrimes err(1, "%s", out.name); 1421556Srgrimes return; 1431556Srgrimes } 1441556Srgrimes 14567451Sgreen /* Don't try to read a really weird amount (like negative). */ 14667451Sgreen if (out.offset < 0) 14767451Sgreen errx(1, "%s: illegal offset", "oseek/seek"); 14867451Sgreen 1491556Srgrimes /* If no read access, try using mtio. */ 1501556Srgrimes if (out.flags & NOREAD) { 1511556Srgrimes t_op.mt_op = MTFSR; 1521556Srgrimes t_op.mt_count = out.offset; 1531556Srgrimes 15448026Sgreen if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) 1551556Srgrimes err(1, "%s", out.name); 1561556Srgrimes return; 1571556Srgrimes } 1581556Srgrimes 1591556Srgrimes /* Read it. */ 1601556Srgrimes for (cnt = 0; cnt < out.offset; ++cnt) { 1611556Srgrimes if ((n = read(out.fd, out.db, out.dbsz)) > 0) 1621556Srgrimes continue; 1631556Srgrimes 16448026Sgreen if (n == -1) 1651556Srgrimes err(1, "%s", out.name); 1661556Srgrimes 1671556Srgrimes /* 1681556Srgrimes * If reach EOF, fill with NUL characters; first, back up over 1691556Srgrimes * the EOF mark. Note, cnt has not yet been incremented, so 1701556Srgrimes * the EOF read does not count as a seek'd block. 1711556Srgrimes */ 1721556Srgrimes t_op.mt_op = MTBSR; 1731556Srgrimes t_op.mt_count = 1; 1741556Srgrimes if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) 1751556Srgrimes err(1, "%s", out.name); 1761556Srgrimes 17762311Sgreen while (cnt++ < out.offset) { 17862311Sgreen n = write(out.fd, out.db, out.dbsz); 17962311Sgreen if (n == -1) 1801556Srgrimes err(1, "%s", out.name); 18162311Sgreen if ((size_t)n != out.dbsz) 18262311Sgreen errx(1, "%s: write failure", out.name); 18362311Sgreen } 1841556Srgrimes break; 1851556Srgrimes } 1861556Srgrimes} 187