1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1985, 1987, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-12-11 23:44:53 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
1997-08-14 06:41:00 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1985, 1987, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
2001-12-11 23:44:53 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#ifndef lint
|
2001-12-11 23:44:53 +00:00
|
|
|
static const char sccsid[] = "@(#)tcopy.c 8.2 (Berkeley) 4/17/94";
|
1997-08-14 06:41:00 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mtio.h>
|
|
|
|
|
1997-08-14 06:41:00 +00:00
|
|
|
#include <err.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2001-03-08 09:04:40 +00:00
|
|
|
#include <paths.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <signal.h>
|
2002-03-25 05:23:45 +00:00
|
|
|
#include <stdint.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define MAXREC (64 * 1024)
|
|
|
|
#define NOCOUNT (-2)
|
|
|
|
|
|
|
|
int filen, guesslen, maxblk = MAXREC;
|
1999-04-30 13:13:32 +00:00
|
|
|
u_int64_t lastrec, record, size, tsize;
|
2001-08-13 21:58:16 +00:00
|
|
|
FILE *msg;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-03-22 01:42:45 +00:00
|
|
|
void *getspace(int);
|
|
|
|
void intr(int);
|
2010-01-05 21:09:59 +00:00
|
|
|
static void usage(void) __dead2;
|
2002-03-22 01:42:45 +00:00
|
|
|
void verify(int, int, char *);
|
|
|
|
void writeop(int, int);
|
1999-04-30 13:13:32 +00:00
|
|
|
void rewind_tape(int);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2010-01-05 21:09:59 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2010-01-05 21:09:59 +00:00
|
|
|
int lastnread, nread, nw, inp, outp;
|
1994-05-27 12:33:43 +00:00
|
|
|
enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
|
|
|
|
sig_t oldsig;
|
|
|
|
int ch, needeof;
|
2001-12-11 23:44:53 +00:00
|
|
|
char *buff;
|
|
|
|
const char *inf;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-08-13 21:58:16 +00:00
|
|
|
msg = stdout;
|
1994-05-27 12:33:43 +00:00
|
|
|
guesslen = 1;
|
2010-06-28 12:00:20 +00:00
|
|
|
outp = -1;
|
1997-03-29 04:34:07 +00:00
|
|
|
while ((ch = getopt(argc, argv, "cs:vx")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch((char)ch) {
|
|
|
|
case 'c':
|
|
|
|
op = COPYVERIFY;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
maxblk = atoi(optarg);
|
|
|
|
if (maxblk <= 0) {
|
1997-08-14 06:41:00 +00:00
|
|
|
warnx("illegal block size");
|
1994-05-27 12:33:43 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
guesslen = 0;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
op = VERIFY;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
msg = stderr;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
switch(argc) {
|
|
|
|
case 0:
|
|
|
|
if (op != READ)
|
|
|
|
usage();
|
|
|
|
inf = _PATH_DEFTAPE;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (op != READ)
|
|
|
|
usage();
|
|
|
|
inf = argv[0];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (op == READ)
|
|
|
|
op = COPY;
|
|
|
|
inf = argv[0];
|
|
|
|
if ((outp = open(argv[1], op == VERIFY ? O_RDONLY :
|
1997-08-14 06:41:00 +00:00
|
|
|
op == COPY ? O_WRONLY : O_RDWR, DEFFILEMODE)) < 0)
|
|
|
|
err(3, "%s", argv[1]);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1997-08-14 06:41:00 +00:00
|
|
|
if ((inp = open(inf, O_RDONLY, 0)) < 0)
|
|
|
|
err(1, "%s", inf);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
buff = getspace(maxblk);
|
|
|
|
|
|
|
|
if (op == VERIFY) {
|
|
|
|
verify(inp, outp, buff);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
|
|
|
|
(void) signal(SIGINT, intr);
|
|
|
|
|
|
|
|
needeof = 0;
|
|
|
|
for (lastnread = NOCOUNT;;) {
|
|
|
|
if ((nread = read(inp, buff, maxblk)) == -1) {
|
|
|
|
while (errno == EINVAL && (maxblk -= 1024)) {
|
|
|
|
nread = read(inp, buff, maxblk);
|
|
|
|
if (nread >= 0)
|
|
|
|
goto r1;
|
|
|
|
}
|
2010-06-28 12:00:20 +00:00
|
|
|
err(1, "read error, file %d, record %ju", filen, (intmax_t)record);
|
1994-05-27 12:33:43 +00:00
|
|
|
} else if (nread != lastnread) {
|
|
|
|
if (lastnread != 0 && lastnread != NOCOUNT) {
|
|
|
|
if (lastrec == 0 && nread == 0)
|
2010-06-28 12:00:20 +00:00
|
|
|
fprintf(msg, "%ju records\n", (intmax_t)record);
|
1994-05-27 12:33:43 +00:00
|
|
|
else if (record - lastrec > 1)
|
2010-06-28 12:00:20 +00:00
|
|
|
fprintf(msg, "records %ju to %ju\n",
|
|
|
|
(intmax_t)lastrec, (intmax_t)record);
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
2010-06-28 12:00:20 +00:00
|
|
|
fprintf(msg, "record %ju\n", (intmax_t)lastrec);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
if (nread != 0)
|
|
|
|
fprintf(msg, "file %d: block size %d: ",
|
|
|
|
filen, nread);
|
|
|
|
(void) fflush(stdout);
|
|
|
|
lastrec = record;
|
|
|
|
}
|
|
|
|
r1: guesslen = 0;
|
|
|
|
if (nread > 0) {
|
|
|
|
if (op == COPY || op == COPYVERIFY) {
|
|
|
|
if (needeof) {
|
|
|
|
writeop(outp, MTWEOF);
|
|
|
|
needeof = 0;
|
|
|
|
}
|
|
|
|
nw = write(outp, buff, nread);
|
|
|
|
if (nw != nread) {
|
1997-08-14 06:41:00 +00:00
|
|
|
if (nw == -1) {
|
2010-06-28 12:00:20 +00:00
|
|
|
warn("write error, file %d, record %ju", filen,
|
|
|
|
(intmax_t)record);
|
1997-08-14 06:41:00 +00:00
|
|
|
} else {
|
2010-06-28 12:00:20 +00:00
|
|
|
warnx("write error, file %d, record %ju", filen,
|
|
|
|
(intmax_t)record);
|
|
|
|
warnx("write (%d) != read (%d)", nw, nread);
|
1997-08-14 06:41:00 +00:00
|
|
|
}
|
|
|
|
errx(5, "copy aborted");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
size += nread;
|
|
|
|
record++;
|
|
|
|
} else {
|
|
|
|
if (lastnread <= 0 && lastnread != NOCOUNT) {
|
|
|
|
fprintf(msg, "eot\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(msg,
|
2010-06-28 12:00:20 +00:00
|
|
|
"file %d: eof after %ju records: %ju bytes\n",
|
|
|
|
filen, (intmax_t)record, (intmax_t)size);
|
1994-05-27 12:33:43 +00:00
|
|
|
needeof = 1;
|
|
|
|
filen++;
|
|
|
|
tsize += size;
|
|
|
|
size = record = lastrec = 0;
|
|
|
|
lastnread = 0;
|
|
|
|
}
|
|
|
|
lastnread = nread;
|
|
|
|
}
|
2010-06-28 12:00:20 +00:00
|
|
|
fprintf(msg, "total length: %ju bytes\n", (intmax_t)tsize);
|
1994-05-27 12:33:43 +00:00
|
|
|
(void)signal(SIGINT, oldsig);
|
|
|
|
if (op == COPY || op == COPYVERIFY) {
|
|
|
|
writeop(outp, MTWEOF);
|
|
|
|
writeop(outp, MTWEOF);
|
|
|
|
if (op == COPYVERIFY) {
|
1999-04-30 13:13:32 +00:00
|
|
|
rewind_tape(outp);
|
|
|
|
rewind_tape(inp);
|
1994-05-27 12:33:43 +00:00
|
|
|
verify(inp, outp, buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-01-05 21:09:59 +00:00
|
|
|
verify(int inp, int outp, char *outb)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2010-01-05 21:09:59 +00:00
|
|
|
int eot, inmaxblk, inn, outmaxblk, outn;
|
|
|
|
char *inb;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
inb = getspace(maxblk);
|
|
|
|
inmaxblk = outmaxblk = maxblk;
|
|
|
|
for (eot = 0;; guesslen = 0) {
|
|
|
|
if ((inn = read(inp, inb, inmaxblk)) == -1) {
|
|
|
|
if (guesslen)
|
|
|
|
while (errno == EINVAL && (inmaxblk -= 1024)) {
|
|
|
|
inn = read(inp, inb, inmaxblk);
|
|
|
|
if (inn >= 0)
|
|
|
|
goto r1;
|
|
|
|
}
|
1997-08-14 06:41:00 +00:00
|
|
|
warn("read error");
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
r1: if ((outn = read(outp, outb, outmaxblk)) == -1) {
|
|
|
|
if (guesslen)
|
|
|
|
while (errno == EINVAL && (outmaxblk -= 1024)) {
|
|
|
|
outn = read(outp, outb, outmaxblk);
|
|
|
|
if (outn >= 0)
|
|
|
|
goto r2;
|
|
|
|
}
|
1997-08-14 06:41:00 +00:00
|
|
|
warn("read error");
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
r2: if (inn != outn) {
|
|
|
|
fprintf(msg,
|
|
|
|
"%s: tapes have different block sizes; %d != %d.\n",
|
|
|
|
"tcopy", inn, outn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!inn) {
|
|
|
|
if (eot++) {
|
|
|
|
fprintf(msg, "tcopy: tapes are identical.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (bcmp(inb, outb, inn)) {
|
|
|
|
fprintf(msg,
|
|
|
|
"tcopy: tapes have different data.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
eot = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-01-05 21:09:59 +00:00
|
|
|
intr(int signo __unused)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1999-07-04 17:26:16 +00:00
|
|
|
if (record) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (record - lastrec > 1)
|
2010-06-28 12:00:20 +00:00
|
|
|
fprintf(msg, "records %ju to %ju\n", (intmax_t)lastrec, (intmax_t)record);
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
2010-06-28 12:00:20 +00:00
|
|
|
fprintf(msg, "record %ju\n", (intmax_t)lastrec);
|
1999-07-04 17:26:16 +00:00
|
|
|
}
|
2010-06-28 12:00:20 +00:00
|
|
|
fprintf(msg, "interrupt at file %d: record %ju\n", filen, (intmax_t)record);
|
2001-12-12 22:46:56 +00:00
|
|
|
fprintf(msg, "total length: %ju bytes\n", (uintmax_t)(tsize + size));
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2010-01-05 21:09:59 +00:00
|
|
|
getspace(int blk)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
void *bp;
|
|
|
|
|
1997-08-14 06:41:00 +00:00
|
|
|
if ((bp = malloc((size_t)blk)) == NULL)
|
|
|
|
errx(11, "no memory");
|
1994-05-27 12:33:43 +00:00
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-01-05 21:09:59 +00:00
|
|
|
writeop(int fd, int type)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
struct mtop op;
|
|
|
|
|
|
|
|
op.mt_op = type;
|
|
|
|
op.mt_count = (daddr_t)1;
|
1997-08-14 06:41:00 +00:00
|
|
|
if (ioctl(fd, MTIOCTOP, (char *)&op) < 0)
|
|
|
|
err(6, "tape op");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
1997-08-14 06:41:00 +00:00
|
|
|
static void
|
2010-01-05 21:09:59 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1997-08-14 06:41:00 +00:00
|
|
|
fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1999-04-30 13:13:32 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
rewind_tape(int fd)
|
|
|
|
{
|
|
|
|
struct stat sp;
|
|
|
|
|
|
|
|
if(fstat(fd, &sp))
|
|
|
|
errx(12, "fstat in rewind");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* don't want to do tape ioctl on regular files:
|
|
|
|
*/
|
|
|
|
if( S_ISREG(sp.st_mode) ) {
|
|
|
|
if( lseek(fd, 0, SEEK_SET) == -1 )
|
|
|
|
errx(13, "lseek");
|
|
|
|
} else
|
|
|
|
/* assume its a tape */
|
|
|
|
writeop(fd, MTREW);
|
|
|
|
}
|