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. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)position.c 5.1 (Berkeley) 07/26/91"; 13 #endif /* not lint */ 14 15 #include <sys/types.h> 16 #include <sys/stat.h> 17 #include <sys/ioctl.h> 18 #include <sys/mtio.h> 19 #include <errno.h> 20 #include <unistd.h> 21 #include <string.h> 22 #include "dd.h" 23 #include "extern.h" 24 25 /* 26 * Position input/output data streams before starting the copy. Device type 27 * dependent. Seekable devices use lseek, and the rest position by reading. 28 * Seeking past the end of file can cause null blocks to be written to the 29 * output. 30 */ 31 void 32 pos_in() 33 { 34 register int bcnt, cnt, nr, warned; 35 36 /* If not a character, pipe or tape device, try to seek on it. */ 37 if (!(in.flags & (ISCHR|ISPIPE|ISTAPE))) { 38 if (lseek(in.fd, (off_t)(in.offset * in.dbsz), SEEK_CUR) == -1) 39 err("%s: %s", in.name, strerror(errno)); 40 return; 41 } 42 43 /* 44 * Read the data. If a pipe, read until satisfy the number of bytes 45 * being skipped. No differentiation for reading complete and partial 46 * blocks for other devices. 47 */ 48 for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) { 49 if ((nr = read(in.fd, in.db, bcnt)) > 0) 50 if (in.flags & ISPIPE) { 51 if (!(bcnt -= nr)) { 52 bcnt = in.dbsz; 53 --cnt; 54 } 55 continue; 56 } else 57 --cnt; 58 59 if (nr == 0) { 60 if (files_cnt > 1) { 61 --files_cnt; 62 continue; 63 } 64 err("skip reached end of input"); 65 } 66 67 /* 68 * Input error -- either EOF with no more files, or I/O error. 69 * If noerror not set die. POSIX requires that the warning 70 * message be followed by an I/O display. 71 */ 72 if (ddflags & C_NOERROR) { 73 if (!warned) { 74 warn("%s: %s", in.name, strerror(errno)); 75 warned = 1; 76 summary(0); 77 } 78 continue; 79 } 80 err("%s: %s", in.name, strerror(errno)); 81 } 82 } 83 84 void 85 pos_out() 86 { 87 register int cnt, n; 88 struct mtop t_op; 89 90 /* 91 * If not a tape, try seeking on the file. Seeking on a pipe is 92 * going to fail, but don't protect the user -- they shouldn't 93 * have specified the seek operand. 94 */ 95 if (!(out.flags & ISTAPE)) { 96 if (lseek(out.fd, 97 (off_t)out.offset * out.dbsz, SEEK_SET) == -1) 98 err("%s: %s", out.name, strerror(errno)); 99 return; 100 } 101 102 /* If no read access, try using mtio. */ 103 if (out.flags & NOREAD) { 104 t_op.mt_op = MTFSR; 105 t_op.mt_count = out.offset; 106 107 if (ioctl(out.fd, MTIOCTOP, &t_op) < 0) 108 err("%s: %s", out.name, strerror(errno)); 109 return; 110 } 111 112 /* Read it. */ 113 for (cnt = 0; cnt < out.offset; ++cnt) { 114 if ((n = read(out.fd, out.db, out.dbsz)) > 0) 115 continue; 116 117 if (n < 0) 118 err("%s: %s", out.name, strerror(errno)); 119 120 /* 121 * If reach EOF, fill with NUL characters; first, back up over 122 * the EOF mark. Note, cnt has not yet been incremented, so 123 * the EOF read does not count as a seek'd block. 124 */ 125 t_op.mt_op = MTBSR; 126 t_op.mt_count = 1; 127 if (ioctl(out.fd, MTIOCTOP, &t_op) == -1) 128 err("%s: %s", out.name, strerror(errno)); 129 130 while (cnt++ < out.offset) 131 if ((n = write(out.fd, out.db, out.dbsz)) != out.dbsz) 132 err("%s: %s", out.name, strerror(errno)); 133 break; 134 } 135 } 136