position.c revision 1.9
1/* $OpenBSD: position.c,v 1.9 2006/11/07 18:47:46 otto Exp $ */ 2/* $NetBSD: position.c,v 1.4 1995/03/21 09:04:12 cgd Exp $ */ 3 4/*- 5 * Copyright (c) 1991, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Keith Muller of the University of California, San Diego and Lance 10 * Visser of Convex Computer Corporation. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38#if 0 39static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94"; 40#else 41static char rcsid[] = "$OpenBSD: position.c,v 1.9 2006/11/07 18:47:46 otto Exp $"; 42#endif 43#endif /* not lint */ 44 45#include <sys/types.h> 46#include <sys/stat.h> 47#include <sys/ioctl.h> 48#include <sys/mtio.h> 49#include <sys/time.h> 50 51#include <err.h> 52#include <errno.h> 53#include <string.h> 54#include <unistd.h> 55 56#include "dd.h" 57#include "extern.h" 58 59/* 60 * Position input/output data streams before starting the copy. Device type 61 * dependent. Seekable devices use lseek, and the rest position by reading. 62 * Seeking past the end of file can cause null blocks to be written to the 63 * output. 64 */ 65void 66pos_in(void) 67{ 68 size_t bcnt; 69 ssize_t nr; 70 off_t cnt; 71 int warned; 72 73 /* If not a pipe, tape or tty device, try to seek on it. */ 74 if (!(in.flags & (ISPIPE|ISTAPE)) && !isatty(in.fd)) { 75 if (lseek(in.fd, in.offset * in.dbsz, SEEK_CUR) == -1) 76 err(1, "%s", in.name); 77 return; 78 } 79 80 /* 81 * Read the data. If a pipe, read until satisfy the number of bytes 82 * being skipped. No differentiation for reading complete and partial 83 * blocks for other devices. 84 */ 85 for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) { 86 if ((nr = read(in.fd, in.db, bcnt)) > 0) { 87 if (in.flags & ISPIPE) { 88 if (!(bcnt -= nr)) { 89 bcnt = in.dbsz; 90 --cnt; 91 } 92 } else 93 --cnt; 94 continue; 95 } 96 97 if (nr == 0) { 98 if (files_cnt > 1) { 99 --files_cnt; 100 continue; 101 } 102 errx(1, "skip reached end of input"); 103 } 104 105 /* 106 * Input error -- either EOF with no more files, or I/O error. 107 * If noerror not set die. POSIX requires that the warning 108 * message be followed by an I/O display. 109 */ 110 if (ddflags & C_NOERROR) { 111 if (!warned) { 112 warn("%s", in.name); 113 warned = 1; 114 summary(); 115 } 116 continue; 117 } 118 err(1, "%s", in.name); 119 } 120} 121 122void 123pos_out(void) 124{ 125 struct mtop t_op; 126 off_t cnt; 127 ssize_t n; 128 129 /* 130 * If not a tape, try seeking on the file. Seeking on a pipe is 131 * going to fail, but don't protect the user -- they shouldn't 132 * have specified the seek operand. 133 */ 134 if (!(out.flags & ISTAPE)) { 135 if (lseek(out.fd, out.offset * out.dbsz, SEEK_SET) == -1) 136 err(1, "%s", out.name); 137 return; 138 } 139 140 /* If no read access, try using mtio. */ 141 if (out.flags & NOREAD) { 142 t_op.mt_op = MTFSR; 143 t_op.mt_count = out.offset; 144 145 if (ioctl(out.fd, MTIOCTOP, &t_op) < 0) 146 err(1, "%s", out.name); 147 return; 148 } 149 150 /* Read it. */ 151 for (cnt = 0; cnt < out.offset; ++cnt) { 152 if ((n = read(out.fd, out.db, out.dbsz)) > 0) 153 continue; 154 155 if (n < 0) 156 err(1, "%s", out.name); 157 158 /* 159 * If reach EOF, fill with NUL characters; first, back up over 160 * the EOF mark. Note, cnt has not yet been incremented, so 161 * the EOF read does not count as a seek'd block. 162 */ 163 t_op.mt_op = MTBSR; 164 t_op.mt_count = 1; 165 if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) 166 err(1, "%s", out.name); 167 168 while (cnt++ < out.offset) 169 if ((n = write(out.fd, out.db, out.dbsz)) != out.dbsz) 170 err(1, "%s", out.name); 171 break; 172 } 173} 174