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