Home | History | Annotate | Line # | Download | only in tcopy
      1  1.17     joerg /*	$NetBSD: tcopy.c,v 1.17 2011/09/06 18:32:26 joerg Exp $	*/
      2   1.3       jtc 
      3   1.1       cgd /*
      4   1.4       jtc  * Copyright (c) 1985, 1987, 1993, 1995
      5   1.3       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.6     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.15     lukem __COPYRIGHT("@(#) Copyright (c) 1985, 1987, 1993\
     35  1.15     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39   1.3       jtc #if 0
     40   1.4       jtc static char sccsid[] = "@(#)tcopy.c	8.3 (Berkeley) 1/23/95";
     41   1.3       jtc #endif
     42  1.17     joerg __RCSID("$NetBSD: tcopy.c,v 1.17 2011/09/06 18:32:26 joerg Exp $");
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/types.h>
     46   1.3       jtc #include <sys/stat.h>
     47   1.1       cgd #include <sys/ioctl.h>
     48   1.1       cgd #include <sys/mtio.h>
     49   1.3       jtc 
     50   1.4       jtc #include <err.h>
     51   1.3       jtc #include <errno.h>
     52   1.5     lukem #include <paths.h>
     53   1.3       jtc #include <fcntl.h>
     54   1.3       jtc #include <signal.h>
     55   1.1       cgd #include <stdio.h>
     56   1.3       jtc #include <stdlib.h>
     57   1.3       jtc #include <string.h>
     58   1.3       jtc #include <unistd.h>
     59  1.14     lukem #include <util.h>
     60   1.1       cgd 
     61   1.1       cgd #define	MAXREC	(64 * 1024)
     62   1.1       cgd #define	NOCOUNT	(-2)
     63   1.1       cgd 
     64  1.17     joerg static int	filen, guesslen, maxblk = MAXREC;
     65  1.17     joerg static long	lastrec, record;
     66  1.17     joerg static off_t	size, tsize;
     67  1.17     joerg static FILE	*msg = stdout;
     68  1.17     joerg 
     69  1.17     joerg static void	*getspace(int);
     70  1.17     joerg __dead static void	 intr(int);
     71  1.17     joerg __dead static void	 usage(void);
     72  1.17     joerg static void	 verify(int, int, char *);
     73  1.17     joerg static void	 writeop(int, int);
     74   1.1       cgd 
     75   1.3       jtc int
     76  1.17     joerg main(int argc, char *argv[])
     77   1.1       cgd {
     78   1.4       jtc 	int ch, needeof, nw, inp, outp;
     79   1.4       jtc 	ssize_t lastnread, nread;
     80   1.1       cgd 	enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
     81   1.1       cgd 	sig_t oldsig;
     82  1.16     lukem 	char *buff;
     83  1.16     lukem 	const char *inf;
     84   1.1       cgd 
     85   1.6     lukem 	outp = 0;
     86   1.6     lukem 	inf = NULL;
     87   1.1       cgd 	guesslen = 1;
     88   1.6     lukem 	while ((ch = getopt(argc, argv, "cs:vx")) != -1)
     89   1.1       cgd 		switch((char)ch) {
     90   1.1       cgd 		case 'c':
     91   1.1       cgd 			op = COPYVERIFY;
     92   1.1       cgd 			break;
     93   1.1       cgd 		case 's':
     94   1.1       cgd 			maxblk = atoi(optarg);
     95   1.1       cgd 			if (maxblk <= 0) {
     96   1.4       jtc 				warnx("illegal block size");
     97   1.1       cgd 				usage();
     98   1.1       cgd 			}
     99   1.1       cgd 			guesslen = 0;
    100   1.1       cgd 			break;
    101   1.1       cgd 		case 'v':
    102   1.1       cgd 			op = VERIFY;
    103   1.1       cgd 			break;
    104   1.3       jtc 		case 'x':
    105   1.3       jtc 			msg = stderr;
    106   1.3       jtc 			break;
    107   1.1       cgd 		case '?':
    108   1.1       cgd 		default:
    109   1.1       cgd 			usage();
    110   1.1       cgd 		}
    111   1.1       cgd 	argc -= optind;
    112   1.1       cgd 	argv += optind;
    113   1.1       cgd 
    114   1.1       cgd 	switch(argc) {
    115   1.1       cgd 	case 0:
    116   1.1       cgd 		if (op != READ)
    117   1.1       cgd 			usage();
    118   1.1       cgd 		inf = _PATH_DEFTAPE;
    119   1.1       cgd 		break;
    120   1.1       cgd 	case 1:
    121   1.1       cgd 		if (op != READ)
    122   1.1       cgd 			usage();
    123   1.1       cgd 		inf = argv[0];
    124   1.1       cgd 		break;
    125   1.1       cgd 	case 2:
    126   1.1       cgd 		if (op == READ)
    127   1.1       cgd 			op = COPY;
    128   1.1       cgd 		inf = argv[0];
    129   1.3       jtc 		if ((outp = open(argv[1], op == VERIFY ? O_RDONLY :
    130   1.3       jtc 		    op == COPY ? O_WRONLY : O_RDWR, DEFFILEMODE)) < 0) {
    131  1.10    itojun 			err(3, "%s", argv[1]);
    132   1.1       cgd 		}
    133   1.1       cgd 		break;
    134   1.1       cgd 	default:
    135   1.1       cgd 		usage();
    136   1.1       cgd 	}
    137   1.1       cgd 
    138   1.4       jtc 	if ((inp = open(inf, O_RDONLY, 0)) < 0)
    139  1.10    itojun 		err(1, "%s", inf);
    140   1.1       cgd 
    141   1.1       cgd 	buff = getspace(maxblk);
    142   1.1       cgd 
    143   1.1       cgd 	if (op == VERIFY) {
    144   1.1       cgd 		verify(inp, outp, buff);
    145   1.1       cgd 		exit(0);
    146   1.1       cgd 	}
    147   1.1       cgd 
    148   1.1       cgd 	if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
    149   1.1       cgd 		(void) signal(SIGINT, intr);
    150   1.1       cgd 
    151   1.1       cgd 	needeof = 0;
    152   1.1       cgd 	for (lastnread = NOCOUNT;;) {
    153   1.1       cgd 		if ((nread = read(inp, buff, maxblk)) == -1) {
    154   1.1       cgd 			while (errno == EINVAL && (maxblk -= 1024)) {
    155   1.1       cgd 				nread = read(inp, buff, maxblk);
    156   1.1       cgd 				if (nread >= 0)
    157   1.1       cgd 					goto r1;
    158   1.1       cgd 			}
    159   1.4       jtc 			err(1, "read error, file %d, record %ld",
    160   1.1       cgd 			    filen, record);
    161   1.1       cgd 		} else if (nread != lastnread) {
    162   1.1       cgd 			if (lastnread != 0 && lastnread != NOCOUNT) {
    163   1.1       cgd 				if (lastrec == 0 && nread == 0)
    164   1.3       jtc 					fprintf(msg, "%ld records\n", record);
    165   1.1       cgd 				else if (record - lastrec > 1)
    166   1.3       jtc 					fprintf(msg, "records %ld to %ld\n",
    167   1.1       cgd 					    lastrec, record);
    168   1.1       cgd 				else
    169   1.3       jtc 					fprintf(msg, "record %ld\n", lastrec);
    170   1.1       cgd 			}
    171   1.1       cgd 			if (nread != 0)
    172   1.7       mrg 				fprintf(msg, "file %d: block size %ld: ",
    173   1.7       mrg 				    filen, (long)nread);
    174   1.1       cgd 			(void) fflush(stdout);
    175   1.1       cgd 			lastrec = record;
    176   1.1       cgd 		}
    177   1.1       cgd r1:		guesslen = 0;
    178   1.1       cgd 		if (nread > 0) {
    179   1.1       cgd 			if (op == COPY || op == COPYVERIFY) {
    180   1.1       cgd 				if (needeof) {
    181   1.1       cgd 					writeop(outp, MTWEOF);
    182   1.1       cgd 					needeof = 0;
    183   1.1       cgd 				}
    184   1.1       cgd 				nw = write(outp, buff, nread);
    185   1.1       cgd 				if (nw != nread) {
    186   1.4       jtc 				    int error = errno;
    187   1.1       cgd 				    fprintf(stderr,
    188   1.1       cgd 					"write error, file %d, record %ld: ",
    189   1.1       cgd 					filen, record);
    190   1.1       cgd 				    if (nw == -1)
    191   1.4       jtc 					fprintf(stderr,
    192   1.4       jtc 						": %s", strerror(error));
    193   1.1       cgd 				    else
    194   1.1       cgd 					fprintf(stderr,
    195   1.7       mrg 					    "write (%d) != read (%ld)\n",
    196   1.7       mrg 					    nw, (long)nread);
    197   1.1       cgd 				    fprintf(stderr, "copy aborted\n");
    198   1.1       cgd 				    exit(5);
    199   1.1       cgd 				}
    200   1.1       cgd 			}
    201   1.1       cgd 			size += nread;
    202   1.1       cgd 			record++;
    203   1.1       cgd 		} else {
    204   1.1       cgd 			if (lastnread <= 0 && lastnread != NOCOUNT) {
    205   1.3       jtc 				fprintf(msg, "eot\n");
    206   1.1       cgd 				break;
    207   1.1       cgd 			}
    208   1.3       jtc 			fprintf(msg,
    209  1.11     lukem 			    "file %d: eof after %ld records: %lld bytes\n",
    210   1.7       mrg 			    filen, record, (long long)size);
    211   1.1       cgd 			needeof = 1;
    212   1.1       cgd 			filen++;
    213   1.1       cgd 			tsize += size;
    214   1.1       cgd 			size = record = lastrec = 0;
    215   1.1       cgd 			lastnread = 0;
    216   1.1       cgd 		}
    217   1.1       cgd 		lastnread = nread;
    218   1.1       cgd 	}
    219  1.11     lukem 	fprintf(msg, "total length: %lld bytes\n", (long long)tsize);
    220   1.1       cgd 	(void)signal(SIGINT, oldsig);
    221   1.1       cgd 	if (op == COPY || op == COPYVERIFY) {
    222   1.1       cgd 		writeop(outp, MTWEOF);
    223   1.1       cgd 		writeop(outp, MTWEOF);
    224   1.1       cgd 		if (op == COPYVERIFY) {
    225   1.1       cgd 			writeop(outp, MTREW);
    226   1.1       cgd 			writeop(inp, MTREW);
    227   1.1       cgd 			verify(inp, outp, buff);
    228   1.1       cgd 		}
    229   1.1       cgd 	}
    230   1.1       cgd 	exit(0);
    231   1.1       cgd }
    232   1.1       cgd 
    233  1.17     joerg static void
    234  1.17     joerg verify(int inp, int outp, char *outb)
    235   1.1       cgd {
    236   1.4       jtc 	int eot, inmaxblk, inn, outmaxblk, outn;
    237   1.4       jtc 	char *inb;
    238   1.1       cgd 
    239   1.1       cgd 	inb = getspace(maxblk);
    240   1.1       cgd 	inmaxblk = outmaxblk = maxblk;
    241   1.1       cgd 	for (eot = 0;; guesslen = 0) {
    242   1.1       cgd 		if ((inn = read(inp, inb, inmaxblk)) == -1) {
    243   1.1       cgd 			if (guesslen)
    244   1.1       cgd 				while (errno == EINVAL && (inmaxblk -= 1024)) {
    245   1.1       cgd 					inn = read(inp, inb, inmaxblk);
    246   1.1       cgd 					if (inn >= 0)
    247   1.1       cgd 						goto r1;
    248   1.1       cgd 				}
    249   1.4       jtc 			warn("read error");
    250   1.1       cgd 			break;
    251   1.1       cgd 		}
    252   1.1       cgd r1:		if ((outn = read(outp, outb, outmaxblk)) == -1) {
    253   1.1       cgd 			if (guesslen)
    254   1.1       cgd 				while (errno == EINVAL && (outmaxblk -= 1024)) {
    255   1.1       cgd 					outn = read(outp, outb, outmaxblk);
    256   1.1       cgd 					if (outn >= 0)
    257   1.1       cgd 						goto r2;
    258   1.1       cgd 				}
    259   1.4       jtc 			warn("read error");
    260   1.1       cgd 			break;
    261   1.1       cgd 		}
    262   1.1       cgd r2:		if (inn != outn) {
    263   1.3       jtc 			fprintf(msg,
    264   1.3       jtc 			    "%s: tapes have different block sizes; %d != %d.\n",
    265   1.3       jtc 			    "tcopy", inn, outn);
    266   1.1       cgd 			break;
    267   1.1       cgd 		}
    268   1.1       cgd 		if (!inn) {
    269   1.1       cgd 			if (eot++) {
    270   1.4       jtc 				fprintf(msg, "%s: tapes are identical.\n",
    271   1.4       jtc 					"tcopy");
    272  1.13  christos 				free(inb);
    273   1.1       cgd 				return;
    274   1.1       cgd 			}
    275   1.1       cgd 		} else {
    276   1.6     lukem 			if (memcmp(inb, outb, inn)) {
    277   1.3       jtc 				fprintf(msg,
    278   1.4       jtc 				    "%s: tapes have different data.\n",
    279   1.4       jtc 					"tcopy");
    280   1.1       cgd 				break;
    281   1.1       cgd 			}
    282   1.1       cgd 			eot = 0;
    283   1.1       cgd 		}
    284   1.1       cgd 	}
    285  1.13  christos 	free(inb);
    286   1.1       cgd 	exit(1);
    287   1.1       cgd }
    288   1.1       cgd 
    289  1.17     joerg static void
    290  1.17     joerg intr(int signo)
    291   1.1       cgd {
    292   1.8      ross 	if (record) {
    293   1.1       cgd 		if (record - lastrec > 1)
    294   1.3       jtc 			fprintf(msg, "records %ld to %ld\n", lastrec, record);
    295   1.1       cgd 		else
    296   1.3       jtc 			fprintf(msg, "record %ld\n", lastrec);
    297   1.8      ross 	}
    298   1.3       jtc 	fprintf(msg, "interrupt at file %d: record %ld\n", filen, record);
    299  1.11     lukem 	fprintf(msg, "total length: %lld bytes\n", (long long)(tsize + size));
    300  1.14     lukem 	(void)raise_default_signal(signo);
    301   1.1       cgd 	exit(1);
    302   1.1       cgd }
    303   1.1       cgd 
    304  1.17     joerg static void *
    305  1.17     joerg getspace(int blk)
    306   1.1       cgd {
    307   1.3       jtc 	void *bp;
    308   1.1       cgd 
    309   1.4       jtc 	if ((bp = malloc((size_t)blk)) == NULL)
    310   1.4       jtc 		errx(11, "no memory");
    311   1.4       jtc 
    312   1.3       jtc 	return (bp);
    313   1.1       cgd }
    314   1.1       cgd 
    315  1.17     joerg static void
    316  1.17     joerg writeop(int fd, int type)
    317   1.1       cgd {
    318   1.1       cgd 	struct mtop op;
    319   1.1       cgd 
    320   1.1       cgd 	op.mt_op = type;
    321   1.1       cgd 	op.mt_count = (daddr_t)1;
    322   1.4       jtc 	if (ioctl(fd, MTIOCTOP, (char *)&op) < 0)
    323   1.4       jtc 		err(6, "tape op");
    324   1.1       cgd }
    325   1.1       cgd 
    326  1.17     joerg static void
    327  1.17     joerg usage(void)
    328   1.1       cgd {
    329   1.4       jtc 
    330   1.3       jtc 	fprintf(stderr, "usage: tcopy [-cvx] [-s maxblk] src [dest]\n");
    331   1.1       cgd 	exit(1);
    332   1.1       cgd }
    333