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 * 4. Neither the name of the University nor the names of its contributors 181556Srgrimes * may be used to endorse or promote products derived from this software 191556Srgrimes * without specific prior written permission. 201556Srgrimes * 211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311556Srgrimes * SUCH DAMAGE. 321556Srgrimes */ 331556Srgrimes 341556Srgrimes#ifndef lint 3535773Scharnier#if 0 3636007Scharnierstatic char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94"; 3735773Scharnier#endif 381556Srgrimes#endif /* not lint */ 3999109Sobrien#include <sys/cdefs.h> 4099109Sobrien__FBSDID("$FreeBSD: releng/10.3/bin/dd/position.c 250469 2013-05-10 18:43:36Z eadler $"); 411556Srgrimes 4236007Scharnier#include <sys/types.h> 431556Srgrimes#include <sys/mtio.h> 441556Srgrimes 451556Srgrimes#include <err.h> 4651137Sgreen#include <errno.h> 47111629Smarkm#include <inttypes.h> 48250469Seadler#include <signal.h> 491556Srgrimes#include <unistd.h> 501556Srgrimes 511556Srgrimes#include "dd.h" 521556Srgrimes#include "extern.h" 531556Srgrimes 541556Srgrimes/* 551556Srgrimes * Position input/output data streams before starting the copy. Device type 561556Srgrimes * dependent. Seekable devices use lseek, and the rest position by reading. 571556Srgrimes * Seeking past the end of file can cause null blocks to be written to the 581556Srgrimes * output. 591556Srgrimes */ 601556Srgrimesvoid 6190108Simppos_in(void) 621556Srgrimes{ 6351208Sgreen off_t cnt; 6451208Sgreen int warned; 6548026Sgreen ssize_t nr; 6651208Sgreen size_t bcnt; 671556Srgrimes 6851249Sgreen /* If known to be seekable, try to seek on it. */ 6951249Sgreen if (in.flags & ISSEEK) { 7051137Sgreen errno = 0; 7151208Sgreen if (lseek(in.fd, in.offset * in.dbsz, SEEK_CUR) == -1 && 7251208Sgreen errno != 0) 731556Srgrimes err(1, "%s", in.name); 741556Srgrimes return; 751556Srgrimes } 761556Srgrimes 7767451Sgreen /* Don't try to read a really weird amount (like negative). */ 7867451Sgreen if (in.offset < 0) 7967451Sgreen errx(1, "%s: illegal offset", "iseek/skip"); 8067451Sgreen 811556Srgrimes /* 821556Srgrimes * Read the data. If a pipe, read until satisfy the number of bytes 831556Srgrimes * being skipped. No differentiation for reading complete and partial 841556Srgrimes * blocks for other devices. 851556Srgrimes */ 861556Srgrimes for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) { 871556Srgrimes if ((nr = read(in.fd, in.db, bcnt)) > 0) { 881556Srgrimes if (in.flags & ISPIPE) { 891556Srgrimes if (!(bcnt -= nr)) { 901556Srgrimes bcnt = in.dbsz; 911556Srgrimes --cnt; 921556Srgrimes } 931556Srgrimes } else 941556Srgrimes --cnt; 95250469Seadler if (need_summary) 96250469Seadler summary(); 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 12690108Simppos_out(void) 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