2006-05-01 03:30:13 +04:00
|
|
|
/* $NetBSD: tcopy.c,v 1.13 2006/04/30 23:30:13 christos Exp $ */
|
1994-12-09 05:14:09 +03:00
|
|
|
|
1993-04-09 16:58:42 +04:00
|
|
|
/*
|
1995-09-01 02:11:37 +04:00
|
|
|
* Copyright (c) 1985, 1987, 1993, 1995
|
1994-12-09 05:14:09 +03:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-09 16:58:42 +04:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-04-09 16:58:42 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-10-20 04:35:14 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-04-09 16:58:42 +04:00
|
|
|
#ifndef lint
|
1997-10-20 04:35:14 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1985, 1987, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-04-09 16:58:42 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-12-09 05:14:09 +03:00
|
|
|
#if 0
|
1995-09-01 02:11:37 +04:00
|
|
|
static char sccsid[] = "@(#)tcopy.c 8.3 (Berkeley) 1/23/95";
|
1994-12-09 05:14:09 +03:00
|
|
|
#endif
|
2006-05-01 03:30:13 +04:00
|
|
|
__RCSID("$NetBSD: tcopy.c,v 1.13 2006/04/30 23:30:13 christos Exp $");
|
1993-04-09 16:58:42 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1994-12-09 05:14:09 +03:00
|
|
|
#include <sys/stat.h>
|
1993-04-09 16:58:42 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mtio.h>
|
1994-12-09 05:14:09 +03:00
|
|
|
|
1995-09-01 02:11:37 +04:00
|
|
|
#include <err.h>
|
1994-12-09 05:14:09 +03:00
|
|
|
#include <errno.h>
|
1997-04-15 11:23:06 +04:00
|
|
|
#include <paths.h>
|
1994-12-09 05:14:09 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
1993-04-09 16:58:42 +04:00
|
|
|
#include <stdio.h>
|
1994-12-09 05:14:09 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1993-04-09 16:58:42 +04:00
|
|
|
#define MAXREC (64 * 1024)
|
|
|
|
#define NOCOUNT (-2)
|
|
|
|
|
|
|
|
int filen, guesslen, maxblk = MAXREC;
|
1995-09-01 02:11:37 +04:00
|
|
|
long lastrec, record;
|
|
|
|
off_t size, tsize;
|
1994-12-09 05:14:09 +03:00
|
|
|
FILE *msg = stdout;
|
|
|
|
|
|
|
|
void *getspace __P((int));
|
|
|
|
void intr __P((int));
|
1997-10-20 04:35:14 +04:00
|
|
|
int main __P((int, char **));
|
1994-12-09 05:14:09 +03:00
|
|
|
void usage __P((void));
|
|
|
|
void verify __P((int, int, char *));
|
|
|
|
void writeop __P((int, int));
|
1993-04-09 16:58:42 +04:00
|
|
|
|
1994-12-09 05:14:09 +03:00
|
|
|
int
|
1993-04-09 16:58:42 +04:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
1994-12-09 05:14:09 +03:00
|
|
|
char *argv[];
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
1995-09-01 02:11:37 +04:00
|
|
|
int ch, needeof, nw, inp, outp;
|
|
|
|
ssize_t lastnread, nread;
|
1993-04-09 16:58:42 +04:00
|
|
|
enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
|
|
|
|
sig_t oldsig;
|
1994-12-09 05:14:09 +03:00
|
|
|
char *buff, *inf;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
1997-10-20 04:35:14 +04:00
|
|
|
outp = 0;
|
|
|
|
inf = NULL;
|
1993-04-09 16:58:42 +04:00
|
|
|
guesslen = 1;
|
1997-10-20 04:35:14 +04:00
|
|
|
while ((ch = getopt(argc, argv, "cs:vx")) != -1)
|
1993-04-09 16:58:42 +04:00
|
|
|
switch((char)ch) {
|
|
|
|
case 'c':
|
|
|
|
op = COPYVERIFY;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
maxblk = atoi(optarg);
|
|
|
|
if (maxblk <= 0) {
|
1995-09-01 02:11:37 +04:00
|
|
|
warnx("illegal block size");
|
1993-04-09 16:58:42 +04:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
guesslen = 0;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
op = VERIFY;
|
|
|
|
break;
|
1994-12-09 05:14:09 +03:00
|
|
|
case 'x':
|
|
|
|
msg = stderr;
|
|
|
|
break;
|
1993-04-09 16:58:42 +04:00
|
|
|
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];
|
1994-12-09 05:14:09 +03:00
|
|
|
if ((outp = open(argv[1], op == VERIFY ? O_RDONLY :
|
|
|
|
op == COPY ? O_WRONLY : O_RDWR, DEFFILEMODE)) < 0) {
|
2000-07-07 19:10:32 +04:00
|
|
|
err(3, "%s", argv[1]);
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1995-09-01 02:11:37 +04:00
|
|
|
if ((inp = open(inf, O_RDONLY, 0)) < 0)
|
2000-07-07 19:10:32 +04:00
|
|
|
err(1, "%s", inf);
|
1993-04-09 16:58:42 +04: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;
|
|
|
|
}
|
1995-09-01 02:11:37 +04:00
|
|
|
err(1, "read error, file %d, record %ld",
|
1993-04-09 16:58:42 +04:00
|
|
|
filen, record);
|
|
|
|
} else if (nread != lastnread) {
|
|
|
|
if (lastnread != 0 && lastnread != NOCOUNT) {
|
|
|
|
if (lastrec == 0 && nread == 0)
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg, "%ld records\n", record);
|
1993-04-09 16:58:42 +04:00
|
|
|
else if (record - lastrec > 1)
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg, "records %ld to %ld\n",
|
1993-04-09 16:58:42 +04:00
|
|
|
lastrec, record);
|
|
|
|
else
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg, "record %ld\n", lastrec);
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
|
|
|
if (nread != 0)
|
1997-10-20 07:25:26 +04:00
|
|
|
fprintf(msg, "file %d: block size %ld: ",
|
|
|
|
filen, (long)nread);
|
1993-04-09 16:58:42 +04:00
|
|
|
(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) {
|
1995-09-01 02:11:37 +04:00
|
|
|
int error = errno;
|
1993-04-09 16:58:42 +04:00
|
|
|
fprintf(stderr,
|
|
|
|
"write error, file %d, record %ld: ",
|
|
|
|
filen, record);
|
|
|
|
if (nw == -1)
|
1995-09-01 02:11:37 +04:00
|
|
|
fprintf(stderr,
|
|
|
|
": %s", strerror(error));
|
1993-04-09 16:58:42 +04:00
|
|
|
else
|
|
|
|
fprintf(stderr,
|
1997-10-20 07:25:26 +04:00
|
|
|
"write (%d) != read (%ld)\n",
|
|
|
|
nw, (long)nread);
|
1993-04-09 16:58:42 +04:00
|
|
|
fprintf(stderr, "copy aborted\n");
|
|
|
|
exit(5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size += nread;
|
|
|
|
record++;
|
|
|
|
} else {
|
|
|
|
if (lastnread <= 0 && lastnread != NOCOUNT) {
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg, "eot\n");
|
1993-04-09 16:58:42 +04:00
|
|
|
break;
|
|
|
|
}
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg,
|
2001-01-05 02:05:54 +03:00
|
|
|
"file %d: eof after %ld records: %lld bytes\n",
|
1997-10-20 07:25:26 +04:00
|
|
|
filen, record, (long long)size);
|
1993-04-09 16:58:42 +04:00
|
|
|
needeof = 1;
|
|
|
|
filen++;
|
|
|
|
tsize += size;
|
|
|
|
size = record = lastrec = 0;
|
|
|
|
lastnread = 0;
|
|
|
|
}
|
|
|
|
lastnread = nread;
|
|
|
|
}
|
2001-01-05 02:05:54 +03:00
|
|
|
fprintf(msg, "total length: %lld bytes\n", (long long)tsize);
|
1993-04-09 16:58:42 +04:00
|
|
|
(void)signal(SIGINT, oldsig);
|
|
|
|
if (op == COPY || op == COPYVERIFY) {
|
|
|
|
writeop(outp, MTWEOF);
|
|
|
|
writeop(outp, MTWEOF);
|
|
|
|
if (op == COPYVERIFY) {
|
|
|
|
writeop(outp, MTREW);
|
|
|
|
writeop(inp, MTREW);
|
|
|
|
verify(inp, outp, buff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1994-12-09 05:14:09 +03:00
|
|
|
void
|
1993-04-09 16:58:42 +04:00
|
|
|
verify(inp, outp, outb)
|
1995-09-01 02:11:37 +04:00
|
|
|
int inp, outp;
|
|
|
|
char *outb;
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
1995-09-01 02:11:37 +04:00
|
|
|
int eot, inmaxblk, inn, outmaxblk, outn;
|
|
|
|
char *inb;
|
1993-04-09 16:58:42 +04: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;
|
|
|
|
}
|
1995-09-01 02:11:37 +04:00
|
|
|
warn("read error");
|
1993-04-09 16:58:42 +04: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;
|
|
|
|
}
|
1995-09-01 02:11:37 +04:00
|
|
|
warn("read error");
|
1993-04-09 16:58:42 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
r2: if (inn != outn) {
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg,
|
|
|
|
"%s: tapes have different block sizes; %d != %d.\n",
|
|
|
|
"tcopy", inn, outn);
|
1993-04-09 16:58:42 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!inn) {
|
|
|
|
if (eot++) {
|
1995-09-01 02:11:37 +04:00
|
|
|
fprintf(msg, "%s: tapes are identical.\n",
|
|
|
|
"tcopy");
|
2006-05-01 03:30:13 +04:00
|
|
|
free(inb);
|
1993-04-09 16:58:42 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
1997-10-20 04:35:14 +04:00
|
|
|
if (memcmp(inb, outb, inn)) {
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg,
|
1995-09-01 02:11:37 +04:00
|
|
|
"%s: tapes have different data.\n",
|
|
|
|
"tcopy");
|
1993-04-09 16:58:42 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
eot = 0;
|
|
|
|
}
|
|
|
|
}
|
2006-05-01 03:30:13 +04:00
|
|
|
free(inb);
|
1993-04-09 16:58:42 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1994-12-09 05:14:09 +03:00
|
|
|
intr(signo)
|
|
|
|
int signo;
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
1998-08-26 00:59:36 +04:00
|
|
|
if (record) {
|
1993-04-09 16:58:42 +04:00
|
|
|
if (record - lastrec > 1)
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg, "records %ld to %ld\n", lastrec, record);
|
1993-04-09 16:58:42 +04:00
|
|
|
else
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg, "record %ld\n", lastrec);
|
1998-08-26 00:59:36 +04:00
|
|
|
}
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(msg, "interrupt at file %d: record %ld\n", filen, record);
|
2001-01-05 02:05:54 +03:00
|
|
|
fprintf(msg, "total length: %lld bytes\n", (long long)(tsize + size));
|
1993-04-09 16:58:42 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1994-12-09 05:14:09 +03:00
|
|
|
void *
|
1993-04-09 16:58:42 +04:00
|
|
|
getspace(blk)
|
|
|
|
int blk;
|
|
|
|
{
|
1994-12-09 05:14:09 +03:00
|
|
|
void *bp;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
1995-09-01 02:11:37 +04:00
|
|
|
if ((bp = malloc((size_t)blk)) == NULL)
|
|
|
|
errx(11, "no memory");
|
|
|
|
|
1994-12-09 05:14:09 +03:00
|
|
|
return (bp);
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
|
|
|
|
1994-12-09 05:14:09 +03:00
|
|
|
void
|
1993-04-09 16:58:42 +04:00
|
|
|
writeop(fd, type)
|
|
|
|
int fd, type;
|
|
|
|
{
|
|
|
|
struct mtop op;
|
|
|
|
|
|
|
|
op.mt_op = type;
|
|
|
|
op.mt_count = (daddr_t)1;
|
1995-09-01 02:11:37 +04:00
|
|
|
if (ioctl(fd, MTIOCTOP, (char *)&op) < 0)
|
|
|
|
err(6, "tape op");
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
|
|
|
|
1994-12-09 05:14:09 +03:00
|
|
|
void
|
1993-04-09 16:58:42 +04:00
|
|
|
usage()
|
|
|
|
{
|
1995-09-01 02:11:37 +04:00
|
|
|
|
1994-12-09 05:14:09 +03:00
|
|
|
fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] src [dest]\n");
|
1993-04-09 16:58:42 +04:00
|
|
|
exit(1);
|
|
|
|
}
|