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
33 * @(#) Copyright (c) 1985, 1987, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)tcopy.c 8.2 (Berkeley) 4/17/94
35 * $FreeBSD: src/usr.bin/tcopy/tcopy.c,v 1.7.2.1 2002/11/07 17:54:42 imp Exp $
36 * $DragonFly: src/usr.bin/tcopy/tcopy.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
39 #include <sys/types.h>
41 #include <sys/ioctl.h>
53 #include "pathnames.h"
55 #define MAXREC (64 * 1024)
58 int filen, guesslen, maxblk = MAXREC;
59 u_int64_t lastrec, record, size, tsize;
62 void *getspace __P((int));
64 static void usage __P((void));
65 void verify __P((int, int, char *));
66 void writeop __P((int, int));
67 void rewind_tape(int);
74 register int lastnread, nread, nw, inp, outp;
75 enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
82 while ((ch = getopt(argc, argv, "cs:vx")) != -1)
88 maxblk = atoi(optarg);
90 warnx("illegal block size");
123 if ((outp = open(argv[1], op == VERIFY ? O_RDONLY :
124 op == COPY ? O_WRONLY : O_RDWR, DEFFILEMODE)) < 0)
125 err(3, "%s", argv[1]);
131 if ((inp = open(inf, O_RDONLY, 0)) < 0)
134 buff = getspace(maxblk);
137 verify(inp, outp, buff);
141 if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
142 (void) signal(SIGINT, intr);
145 for (lastnread = NOCOUNT;;) {
146 if ((nread = read(inp, buff, maxblk)) == -1) {
147 while (errno == EINVAL && (maxblk -= 1024)) {
148 nread = read(inp, buff, maxblk);
152 err(1, "read error, file %d, record %qu", filen, record);
153 } else if (nread != lastnread) {
154 if (lastnread != 0 && lastnread != NOCOUNT) {
155 if (lastrec == 0 && nread == 0)
156 fprintf(msg, "%qu records\n", record);
157 else if (record - lastrec > 1)
158 fprintf(msg, "records %qu to %qu\n",
161 fprintf(msg, "record %qu\n", lastrec);
164 fprintf(msg, "file %d: block size %d: ",
166 (void) fflush(stdout);
171 if (op == COPY || op == COPYVERIFY) {
173 writeop(outp, MTWEOF);
176 nw = write(outp, buff, nread);
179 warn("write error, file %d, record %qu", filen, record);
181 warnx("write error, file %d, record %qu", filen, record);
182 warnx("write (%d) != read (%d)", nw, nread);
184 errx(5, "copy aborted");
190 if (lastnread <= 0 && lastnread != NOCOUNT) {
191 fprintf(msg, "eot\n");
195 "file %d: eof after %qu records: %qu bytes\n",
196 filen, record, size);
200 size = record = lastrec = 0;
205 fprintf(msg, "total length: %qu bytes\n", tsize);
206 (void)signal(SIGINT, oldsig);
207 if (op == COPY || op == COPYVERIFY) {
208 writeop(outp, MTWEOF);
209 writeop(outp, MTWEOF);
210 if (op == COPYVERIFY) {
213 verify(inp, outp, buff);
220 verify(inp, outp, outb)
221 register int inp, outp;
224 register int eot, inmaxblk, inn, outmaxblk, outn;
227 inb = getspace(maxblk);
228 inmaxblk = outmaxblk = maxblk;
229 for (eot = 0;; guesslen = 0) {
230 if ((inn = read(inp, inb, inmaxblk)) == -1) {
232 while (errno == EINVAL && (inmaxblk -= 1024)) {
233 inn = read(inp, inb, inmaxblk);
240 r1: if ((outn = read(outp, outb, outmaxblk)) == -1) {
242 while (errno == EINVAL && (outmaxblk -= 1024)) {
243 outn = read(outp, outb, outmaxblk);
250 r2: if (inn != outn) {
252 "%s: tapes have different block sizes; %d != %d.\n",
258 fprintf(msg, "tcopy: tapes are identical.\n");
262 if (bcmp(inb, outb, inn)) {
264 "tcopy: tapes have different data.\n");
278 if (record - lastrec > 1)
279 fprintf(msg, "records %qu to %qu\n", lastrec, record);
281 fprintf(msg, "record %qu\n", lastrec);
283 fprintf(msg, "interrupt at file %d: record %qu\n", filen, record);
284 fprintf(msg, "total length: %ld bytes\n", tsize + size);
294 if ((bp = malloc((size_t)blk)) == NULL)
295 errx(11, "no memory");
306 op.mt_count = (daddr_t)1;
307 if (ioctl(fd, MTIOCTOP, (char *)&op) < 0)
314 fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n");
324 errx(12, "fstat in rewind");
327 * don't want to do tape ioctl on regular files:
329 if( S_ISREG(sp.st_mode) ) {
330 if( lseek(fd, 0, SEEK_SET) == -1 )
333 /* assume its a tape */