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