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. 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
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char copyright[] =
36 "@(#) Copyright (c) 1985, 1987, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)tcopy.c 8.2 (Berkeley) 4/17/94";
44 #include <sys/types.h>
46 #include <sys/ioctl.h>
60 #define MAXREC (64 * 1024)
63 static int filen, guesslen, maxblk = MAXREC;
64 static uint64_t lastrec, record, size, tsize;
67 static void *getspace(int);
68 static void intr(int);
69 static void usage(void) __dead2;
70 static void verify(int, int, char *);
71 static void writeop(int, int);
72 static void rewind_tape(int);
75 main(int argc, char *argv[])
77 int lastnread, nread, nw, inp, outp;
78 enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
87 while ((ch = getopt(argc, argv, "cs:vx")) != -1)
93 maxblk = atoi(optarg);
95 warnx("illegal block size");
128 if ((outp = open(argv[1], op == VERIFY ? O_RDONLY :
129 op == COPY ? O_WRONLY : O_RDWR, DEFFILEMODE)) < 0)
130 err(3, "%s", argv[1]);
136 if ((inp = open(inf, O_RDONLY, 0)) < 0)
139 buff = getspace(maxblk);
142 verify(inp, outp, buff);
146 if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
147 (void) signal(SIGINT, intr);
150 for (lastnread = NOCOUNT;;) {
151 if ((nread = read(inp, buff, maxblk)) == -1) {
152 while (errno == EINVAL && (maxblk -= 1024)) {
153 nread = read(inp, buff, maxblk);
157 err(1, "read error, file %d, record %ju", filen, (intmax_t)record);
158 } else if (nread != lastnread) {
159 if (lastnread != 0 && lastnread != NOCOUNT) {
160 if (lastrec == 0 && nread == 0)
161 fprintf(msg, "%ju records\n", (intmax_t)record);
162 else if (record - lastrec > 1)
163 fprintf(msg, "records %ju to %ju\n",
164 (intmax_t)lastrec, (intmax_t)record);
166 fprintf(msg, "record %ju\n", (intmax_t)lastrec);
169 fprintf(msg, "file %d: block size %d: ",
171 (void) fflush(stdout);
176 if (op == COPY || op == COPYVERIFY) {
178 writeop(outp, MTWEOF);
181 nw = write(outp, buff, nread);
184 warn("write error, file %d, record %ju", filen,
187 warnx("write error, file %d, record %ju", filen,
189 warnx("write (%d) != read (%d)", nw, nread);
191 errx(5, "copy aborted");
197 if (lastnread <= 0 && lastnread != NOCOUNT) {
198 fprintf(msg, "eot\n");
202 "file %d: eof after %ju records: %ju bytes\n",
203 filen, (intmax_t)record, (intmax_t)size);
207 size = record = lastrec = 0;
212 fprintf(msg, "total length: %ju bytes\n", (intmax_t)tsize);
213 (void)signal(SIGINT, oldsig);
214 if (op == COPY || op == COPYVERIFY) {
215 writeop(outp, MTWEOF);
216 writeop(outp, MTWEOF);
217 if (op == COPYVERIFY) {
220 verify(inp, outp, buff);
227 verify(int inp, int outp, char *outb)
229 int eot, inmaxblk, inn, outmaxblk, outn;
232 inb = getspace(maxblk);
233 inmaxblk = outmaxblk = maxblk;
234 for (eot = 0;; guesslen = 0) {
235 if ((inn = read(inp, inb, inmaxblk)) == -1) {
237 while (errno == EINVAL && (inmaxblk -= 1024)) {
238 inn = read(inp, inb, inmaxblk);
245 r1: if ((outn = read(outp, outb, outmaxblk)) == -1) {
247 while (errno == EINVAL && (outmaxblk -= 1024)) {
248 outn = read(outp, outb, outmaxblk);
255 r2: if (inn != outn) {
257 "%s: tapes have different block sizes; %d != %d.\n",
263 fprintf(msg, "tcopy: tapes are identical.\n");
267 if (bcmp(inb, outb, inn)) {
269 "tcopy: tapes have different data.\n");
279 intr(int signo __unused)
282 if (record - lastrec > 1)
283 fprintf(msg, "records %ju to %ju\n", (intmax_t)lastrec, (intmax_t)record);
285 fprintf(msg, "record %ju\n", (intmax_t)lastrec);
287 fprintf(msg, "interrupt at file %d: record %ju\n", filen, (intmax_t)record);
288 fprintf(msg, "total length: %ju bytes\n", (uintmax_t)(tsize + size));
297 if ((bp = malloc((size_t)blk)) == NULL)
298 errx(11, "no memory");
303 writeop(int fd, int type)
308 op.mt_count = (daddr_t)1;
309 if (ioctl(fd, MTIOCTOP, (char *)&op) < 0)
316 fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n");
326 errx(12, "fstat in rewind");
329 * don't want to do tape ioctl on regular files:
331 if( S_ISREG(sp.st_mode) ) {
332 if( lseek(fd, 0, SEEK_SET) == -1 )
335 /* assume its a tape */