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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1985, 1987, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)tcopy.c 8.2 (Berkeley) 4/17/94
31 * $FreeBSD: src/usr.bin/tcopy/tcopy.c,v 1.7.2.1 2002/11/07 17:54:42 imp Exp $
32 * $DragonFly: src/usr.bin/tcopy/tcopy.c,v 1.4 2008/10/16 01:52:33 swildner Exp $
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
49 #include "pathnames.h"
51 #define MAXREC (64 * 1024)
54 int filen, guesslen, maxblk = MAXREC;
55 u_int64_t lastrec, record, size, tsize;
60 static void usage(void);
61 void verify(int, int, char *);
62 void writeop(int, int);
63 void rewind_tape(int);
66 main(int argc, char **argv)
68 int lastnread, nread, nw, inp, outp;
69 enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
76 while ((ch = getopt(argc, argv, "cs:vx")) != -1)
82 maxblk = atoi(optarg);
84 warnx("illegal block size");
117 if ((outp = open(argv[1], op == VERIFY ? O_RDONLY :
118 op == COPY ? O_WRONLY : O_RDWR, DEFFILEMODE)) < 0)
119 err(3, "%s", argv[1]);
125 if ((inp = open(inf, O_RDONLY, 0)) < 0)
128 buff = getspace(maxblk);
131 verify(inp, outp, buff);
135 if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
136 (void) signal(SIGINT, intr);
139 for (lastnread = NOCOUNT;;) {
140 if ((nread = read(inp, buff, maxblk)) == -1) {
141 while (errno == EINVAL && (maxblk -= 1024)) {
142 nread = read(inp, buff, maxblk);
146 err(1, "read error, file %d, record %qu", filen, record);
147 } else if (nread != lastnread) {
148 if (lastnread != 0 && lastnread != NOCOUNT) {
149 if (lastrec == 0 && nread == 0)
150 fprintf(msg, "%qu records\n", record);
151 else if (record - lastrec > 1)
152 fprintf(msg, "records %qu to %qu\n",
155 fprintf(msg, "record %qu\n", lastrec);
158 fprintf(msg, "file %d: block size %d: ",
160 (void) fflush(stdout);
165 if (op == COPY || op == COPYVERIFY) {
167 writeop(outp, MTWEOF);
170 nw = write(outp, buff, nread);
173 warn("write error, file %d, record %qu", filen, record);
175 warnx("write error, file %d, record %qu", filen, record);
176 warnx("write (%d) != read (%d)", nw, nread);
178 errx(5, "copy aborted");
184 if (lastnread <= 0 && lastnread != NOCOUNT) {
185 fprintf(msg, "eot\n");
189 "file %d: eof after %qu records: %qu bytes\n",
190 filen, record, size);
194 size = record = lastrec = 0;
199 fprintf(msg, "total length: %qu bytes\n", tsize);
200 (void)signal(SIGINT, oldsig);
201 if (op == COPY || op == COPYVERIFY) {
202 writeop(outp, MTWEOF);
203 writeop(outp, MTWEOF);
204 if (op == COPYVERIFY) {
207 verify(inp, outp, buff);
214 verify(int inp, int outp, char *outb)
216 int eot, inmaxblk, inn, outmaxblk, outn;
219 inb = getspace(maxblk);
220 inmaxblk = outmaxblk = maxblk;
221 for (eot = 0;; guesslen = 0) {
222 if ((inn = read(inp, inb, inmaxblk)) == -1) {
224 while (errno == EINVAL && (inmaxblk -= 1024)) {
225 inn = read(inp, inb, inmaxblk);
232 r1: if ((outn = read(outp, outb, outmaxblk)) == -1) {
234 while (errno == EINVAL && (outmaxblk -= 1024)) {
235 outn = read(outp, outb, outmaxblk);
242 r2: if (inn != outn) {
244 "%s: tapes have different block sizes; %d != %d.\n",
250 fprintf(msg, "tcopy: tapes are identical.\n");
254 if (bcmp(inb, outb, inn)) {
256 "tcopy: tapes have different data.\n");
269 if (record - lastrec > 1)
270 fprintf(msg, "records %qu to %qu\n", lastrec, record);
272 fprintf(msg, "record %qu\n", lastrec);
274 fprintf(msg, "interrupt at file %d: record %qu\n", filen, record);
275 fprintf(msg, "total length: %ld bytes\n", tsize + size);
284 if ((bp = malloc((size_t)blk)) == NULL)
285 errx(11, "no memory");
290 writeop(int fd, int type)
295 op.mt_count = (daddr_t)1;
296 if (ioctl(fd, MTIOCTOP, (char *)&op) < 0)
303 fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n");
313 errx(12, "fstat in rewind");
316 * don't want to do tape ioctl on regular files:
318 if( S_ISREG(sp.st_mode) ) {
319 if( lseek(fd, 0, SEEK_SET) == -1 )
322 /* assume its a tape */