position.c revision 321140
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: stable/10/bin/dd/position.c 321140 2017-07-18 17:36:25Z ngie $"); 411556Srgrimes 4236007Scharnier#include <sys/types.h> 431556Srgrimes#include <sys/mtio.h> 441556Srgrimes 451556Srgrimes#include <err.h> 4651137Sgreen#include <errno.h> 47111629Smarkm#include <inttypes.h> 48321140Sngie#include <limits.h> 49250469Seadler#include <signal.h> 501556Srgrimes#include <unistd.h> 511556Srgrimes 521556Srgrimes#include "dd.h" 531556Srgrimes#include "extern.h" 541556Srgrimes 55321140Sngiestatic off_t 56321140Sngieseek_offset(IO *io) 57321140Sngie{ 58321140Sngie off_t n; 59321140Sngie size_t sz; 60321140Sngie 61321140Sngie n = io->offset; 62321140Sngie sz = io->dbsz; 63321140Sngie 64321140Sngie _Static_assert(sizeof(io->offset) == sizeof(int64_t), "64-bit off_t"); 65321140Sngie 66321140Sngie /* 67321140Sngie * If the lseek offset will be negative, verify that this is a special 68321140Sngie * device file. Some such files (e.g. /dev/kmem) permit "negative" 69321140Sngie * offsets. 70321140Sngie * 71321140Sngie * Bail out if the calculation of a file offset would overflow. 72321140Sngie */ 73321140Sngie if ((io->flags & ISCHR) == 0 && (n < 0 || n > OFF_MAX / (ssize_t)sz)) 74321140Sngie errx(1, "seek offsets cannot be larger than %jd", 75321140Sngie (intmax_t)OFF_MAX); 76321140Sngie else if ((io->flags & ISCHR) != 0 && (uint64_t)n > UINT64_MAX / sz) 77321140Sngie errx(1, "seek offsets cannot be larger than %ju", 78321140Sngie (uintmax_t)UINT64_MAX); 79321140Sngie 80321140Sngie return ((off_t)( (uint64_t)n * sz )); 81321140Sngie} 82321140Sngie 831556Srgrimes/* 841556Srgrimes * Position input/output data streams before starting the copy. Device type 851556Srgrimes * dependent. Seekable devices use lseek, and the rest position by reading. 861556Srgrimes * Seeking past the end of file can cause null blocks to be written to the 871556Srgrimes * output. 881556Srgrimes */ 891556Srgrimesvoid 9090108Simppos_in(void) 911556Srgrimes{ 9251208Sgreen off_t cnt; 9351208Sgreen int warned; 9448026Sgreen ssize_t nr; 9551208Sgreen size_t bcnt; 961556Srgrimes 9751249Sgreen /* If known to be seekable, try to seek on it. */ 9851249Sgreen if (in.flags & ISSEEK) { 9951137Sgreen errno = 0; 100321140Sngie if (lseek(in.fd, seek_offset(&in), SEEK_CUR) == -1 && 10151208Sgreen errno != 0) 1021556Srgrimes err(1, "%s", in.name); 1031556Srgrimes return; 1041556Srgrimes } 1051556Srgrimes 10667451Sgreen /* Don't try to read a really weird amount (like negative). */ 10767451Sgreen if (in.offset < 0) 10867451Sgreen errx(1, "%s: illegal offset", "iseek/skip"); 10967451Sgreen 1101556Srgrimes /* 1111556Srgrimes * Read the data. If a pipe, read until satisfy the number of bytes 1121556Srgrimes * being skipped. No differentiation for reading complete and partial 1131556Srgrimes * blocks for other devices. 1141556Srgrimes */ 1151556Srgrimes for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) { 1161556Srgrimes if ((nr = read(in.fd, in.db, bcnt)) > 0) { 1171556Srgrimes if (in.flags & ISPIPE) { 1181556Srgrimes if (!(bcnt -= nr)) { 1191556Srgrimes bcnt = in.dbsz; 1201556Srgrimes --cnt; 1211556Srgrimes } 1221556Srgrimes } else 1231556Srgrimes --cnt; 124250469Seadler if (need_summary) 125250469Seadler summary(); 1261556Srgrimes continue; 1271556Srgrimes } 1281556Srgrimes 1291556Srgrimes if (nr == 0) { 1301556Srgrimes if (files_cnt > 1) { 1311556Srgrimes --files_cnt; 1321556Srgrimes continue; 1331556Srgrimes } 1341556Srgrimes errx(1, "skip reached end of input"); 1351556Srgrimes } 1361556Srgrimes 1371556Srgrimes /* 1381556Srgrimes * Input error -- either EOF with no more files, or I/O error. 1391556Srgrimes * If noerror not set die. POSIX requires that the warning 1401556Srgrimes * message be followed by an I/O display. 1411556Srgrimes */ 1421556Srgrimes if (ddflags & C_NOERROR) { 1431556Srgrimes if (!warned) { 1441556Srgrimes warn("%s", in.name); 1451556Srgrimes warned = 1; 1461556Srgrimes summary(); 1471556Srgrimes } 1481556Srgrimes continue; 1491556Srgrimes } 1501556Srgrimes err(1, "%s", in.name); 1511556Srgrimes } 1521556Srgrimes} 1531556Srgrimes 1541556Srgrimesvoid 15590108Simppos_out(void) 1561556Srgrimes{ 1571556Srgrimes struct mtop t_op; 15848026Sgreen off_t cnt; 15948026Sgreen ssize_t n; 1601556Srgrimes 16151249Sgreen /* 16251249Sgreen * If not a tape, try seeking on the file. Seeking on a pipe is 16351249Sgreen * going to fail, but don't protect the user -- they shouldn't 16451249Sgreen * have specified the seek operand. 16551249Sgreen */ 16662311Sgreen if (out.flags & (ISSEEK | ISPIPE)) { 16751137Sgreen errno = 0; 168321140Sngie if (lseek(out.fd, seek_offset(&out), SEEK_CUR) == -1 && 16951208Sgreen errno != 0) 1701556Srgrimes err(1, "%s", out.name); 1711556Srgrimes return; 1721556Srgrimes } 1731556Srgrimes 17467451Sgreen /* Don't try to read a really weird amount (like negative). */ 17567451Sgreen if (out.offset < 0) 17667451Sgreen errx(1, "%s: illegal offset", "oseek/seek"); 17767451Sgreen 1781556Srgrimes /* If no read access, try using mtio. */ 1791556Srgrimes if (out.flags & NOREAD) { 1801556Srgrimes t_op.mt_op = MTFSR; 1811556Srgrimes t_op.mt_count = out.offset; 1821556Srgrimes 18348026Sgreen if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) 1841556Srgrimes err(1, "%s", out.name); 1851556Srgrimes return; 1861556Srgrimes } 1871556Srgrimes 1881556Srgrimes /* Read it. */ 1891556Srgrimes for (cnt = 0; cnt < out.offset; ++cnt) { 1901556Srgrimes if ((n = read(out.fd, out.db, out.dbsz)) > 0) 1911556Srgrimes continue; 1921556Srgrimes 19348026Sgreen if (n == -1) 1941556Srgrimes err(1, "%s", out.name); 1951556Srgrimes 1961556Srgrimes /* 1971556Srgrimes * If reach EOF, fill with NUL characters; first, back up over 1981556Srgrimes * the EOF mark. Note, cnt has not yet been incremented, so 1991556Srgrimes * the EOF read does not count as a seek'd block. 2001556Srgrimes */ 2011556Srgrimes t_op.mt_op = MTBSR; 2021556Srgrimes t_op.mt_count = 1; 2031556Srgrimes if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) 2041556Srgrimes err(1, "%s", out.name); 2051556Srgrimes 20662311Sgreen while (cnt++ < out.offset) { 20762311Sgreen n = write(out.fd, out.db, out.dbsz); 20862311Sgreen if (n == -1) 2091556Srgrimes err(1, "%s", out.name); 21062311Sgreen if ((size_t)n != out.dbsz) 21162311Sgreen errx(1, "%s: write failure", out.name); 21262311Sgreen } 2131556Srgrimes break; 2141556Srgrimes } 2151556Srgrimes} 216