2 * Copyright (c) 1985, 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char copyright[] =
40 "@(#) Copyright (c) 1985, 1987, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)tcopy.c 8.2 (Berkeley) 4/17/94";
48 #include <sys/types.h>
50 #include <sys/ioctl.h>
64 #define MAXREC (64 * 1024)
67 int filen, guesslen, maxblk = MAXREC;
68 u_int64_t lastrec, record, size, tsize;
73 static void usage(void);
74 void verify(int, int, char *);
75 void writeop(int, int);
76 void rewind_tape(int);
83 register int lastnread, nread, nw, inp, outp;
84 enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
92 while ((ch = getopt(argc, argv, "cs:vx")) != -1)
98 maxblk = atoi(optarg);
100 warnx("illegal block size");
133 if ((outp = open(argv[1], op == VERIFY ? O_RDONLY :
134 op == COPY ? O_WRONLY : O_RDWR, DEFFILEMODE)) < 0)
135 err(3, "%s", argv[1]);
141 if ((inp = open(inf, O_RDONLY, 0)) < 0)
144 buff = getspace(maxblk);
147 verify(inp, outp, buff);
151 if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
152 (void) signal(SIGINT, intr);
155 for (lastnread = NOCOUNT;;) {
156 if ((nread = read(inp, buff, maxblk)) == -1) {
157 while (errno == EINVAL && (maxblk -= 1024)) {
158 nread = read(inp, buff, maxblk);
162 err(1, "read error, file %d, record %qu", filen, record);
163 } else if (nread != lastnread) {
164 if (lastnread != 0 && lastnread != NOCOUNT) {
165 if (lastrec == 0 && nread == 0)
166 fprintf(msg, "%qu records\n", record);
167 else if (record - lastrec > 1)
168 fprintf(msg, "records %qu to %qu\n",
171 fprintf(msg, "record %qu\n", lastrec);
174 fprintf(msg, "file %d: block size %d: ",
176 (void) fflush(stdout);
181 if (op == COPY || op == COPYVERIFY) {
183 writeop(outp, MTWEOF);
186 nw = write(outp, buff, nread);
189 warn("write error, file %d, record %qu", filen, record);
191 warnx("write error, file %d, record %qu", filen, record);
192 warnx("write (%d) != read (%d)", nw, nread);
194 errx(5, "copy aborted");
200 if (lastnread <= 0 && lastnread != NOCOUNT) {
201 fprintf(msg, "eot\n");
205 "file %d: eof after %qu records: %qu bytes\n",
206 filen, record, size);
210 size = record = lastrec = 0;
215 fprintf(msg, "total length: %qu bytes\n", tsize);
216 (void)signal(SIGINT, oldsig);
217 if (op == COPY || op == COPYVERIFY) {
218 writeop(outp, MTWEOF);
219 writeop(outp, MTWEOF);
220 if (op == COPYVERIFY) {
223 verify(inp, outp, buff);
230 verify(inp, outp, outb)
231 register int inp, outp;
234 register int eot, inmaxblk, inn, outmaxblk, outn;
237 inb = getspace(maxblk);
238 inmaxblk = outmaxblk = maxblk;
239 for (eot = 0;; guesslen = 0) {
240 if ((inn = read(inp, inb, inmaxblk)) == -1) {
242 while (errno == EINVAL && (inmaxblk -= 1024)) {
243 inn = read(inp, inb, inmaxblk);
250 r1: if ((outn = read(outp, outb, outmaxblk)) == -1) {
252 while (errno == EINVAL && (outmaxblk -= 1024)) {
253 outn = read(outp, outb, outmaxblk);
260 r2: if (inn != outn) {
262 "%s: tapes have different block sizes; %d != %d.\n",
268 fprintf(msg, "tcopy: tapes are identical.\n");
272 if (bcmp(inb, outb, inn)) {
274 "tcopy: tapes have different data.\n");
288 if (record - lastrec > 1)
289 fprintf(msg, "records %qu to %qu\n", lastrec, record);
291 fprintf(msg, "record %qu\n", lastrec);
293 fprintf(msg, "interrupt at file %d: record %qu\n", filen, record);
294 fprintf(msg, "total length: %ju bytes\n", (uintmax_t)(tsize + size));
304 if ((bp = malloc((size_t)blk)) == NULL)
305 errx(11, "no memory");
316 op.mt_count = (daddr_t)1;
317 if (ioctl(fd, MTIOCTOP, (char *)&op) < 0)
324 fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n");
334 errx(12, "fstat in rewind");
337 * don't want to do tape ioctl on regular files:
339 if( S_ISREG(sp.st_mode) ) {
340 if( lseek(fd, 0, SEEK_SET) == -1 )
343 /* assume its a tape */