10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <sys/file.h>
460Sstevel@tonic-gate #include <sys/fcntl.h>
470Sstevel@tonic-gate #include <sys/mtio.h>
480Sstevel@tonic-gate #include <stdlib.h>
490Sstevel@tonic-gate #include <limits.h>
500Sstevel@tonic-gate #include <errno.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate char *buff; /* buffer for read/write */
530Sstevel@tonic-gate int filen = 1; /* file number being processed */
540Sstevel@tonic-gate long count, lcount; /* number of blocks for file */
550Sstevel@tonic-gate extern void RUBOUT();
560Sstevel@tonic-gate int nfile; /* used for ??? */
570Sstevel@tonic-gate off64_t size, tsize; /* number of bytes in file, total */
580Sstevel@tonic-gate int ln;
590Sstevel@tonic-gate char *inf, *outf;
600Sstevel@tonic-gate int copy;
610Sstevel@tonic-gate size_t size_64K = 64 * 1024;
620Sstevel@tonic-gate size_t size_256K = 256 * 1024;
630Sstevel@tonic-gate
640Sstevel@tonic-gate
650Sstevel@tonic-gate int
main(argc,argv)660Sstevel@tonic-gate main(argc, argv)
670Sstevel@tonic-gate char **argv;
680Sstevel@tonic-gate {
69*213Smuffin int n, nw, inp, outp;
700Sstevel@tonic-gate struct mtop op;
710Sstevel@tonic-gate size_t buf_size = size_64K;
720Sstevel@tonic-gate
730Sstevel@tonic-gate if (argc <= 1 || argc > 3) {
740Sstevel@tonic-gate (void) fprintf(stderr, "Usage: tcopy src [dest]\n");
750Sstevel@tonic-gate return (1);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate inf = argv[1];
780Sstevel@tonic-gate if (argc == 3) {
790Sstevel@tonic-gate outf = argv[2];
800Sstevel@tonic-gate copy = 1;
810Sstevel@tonic-gate }
820Sstevel@tonic-gate if ((inp = open(inf, O_RDONLY, 0666)) < 0) {
830Sstevel@tonic-gate (void) fprintf(stderr, "Can't open %s\n", inf);
840Sstevel@tonic-gate return (1);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate if (copy) {
870Sstevel@tonic-gate if ((outp = open(outf, O_WRONLY, 0666)) < 0) {
880Sstevel@tonic-gate (void) fprintf(stderr, "Can't open %s\n", outf);
890Sstevel@tonic-gate return (3);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate }
920Sstevel@tonic-gate if ((buff = malloc(buf_size)) == NULL) {
930Sstevel@tonic-gate (void) fprintf(stderr, "Can't allocate memory for tcopy\n");
940Sstevel@tonic-gate return (4);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != SIG_IGN)
970Sstevel@tonic-gate (void) signal(SIGINT, RUBOUT);
980Sstevel@tonic-gate ln = -2;
990Sstevel@tonic-gate for (;;) {
1000Sstevel@tonic-gate count++;
1010Sstevel@tonic-gate errno = 0;
1020Sstevel@tonic-gate while ((n = read(inp, buff, buf_size)) < 0 &&
1030Sstevel@tonic-gate errno == ENOMEM && buf_size < INT_MAX) {
1040Sstevel@tonic-gate if (buf_size < size_256K)
1050Sstevel@tonic-gate buf_size = size_256K;
1060Sstevel@tonic-gate else
1070Sstevel@tonic-gate buf_size *= 2;
1080Sstevel@tonic-gate free(buff);
1090Sstevel@tonic-gate if ((buff = malloc(buf_size)) == NULL) {
1100Sstevel@tonic-gate (void) fprintf(stderr,
1110Sstevel@tonic-gate "Can't allocate memory for tcopy\n");
1120Sstevel@tonic-gate return (4);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate op.mt_op = MTFSF; /* Rewind to start of file */
1150Sstevel@tonic-gate op.mt_count = (daddr_t)0;
1160Sstevel@tonic-gate if (ioctl(inp, MTIOCTOP, (char *)&op) < 0) {
1170Sstevel@tonic-gate perror("Read record size");
1180Sstevel@tonic-gate return (6);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate errno = 0;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate if (n > 0) {
1230Sstevel@tonic-gate if (copy) {
1240Sstevel@tonic-gate nw = write(outp, buff, (size_t)n);
1250Sstevel@tonic-gate if (nw != n) {
1260Sstevel@tonic-gate (void) fprintf(stderr, "write (%d) !="
1270Sstevel@tonic-gate " read (%d)\n", nw, n);
1280Sstevel@tonic-gate (void) fprintf(stderr, "COPY "
1290Sstevel@tonic-gate "Aborted\n");
1300Sstevel@tonic-gate return (5);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate size += n;
1340Sstevel@tonic-gate if (n != ln) {
1350Sstevel@tonic-gate if (ln > 0)
1360Sstevel@tonic-gate if (count - lcount > 1)
1370Sstevel@tonic-gate (void) printf("file %d: records"
1380Sstevel@tonic-gate " %ld to %ld: size %d\n",
1390Sstevel@tonic-gate filen, lcount, count-1, ln);
1400Sstevel@tonic-gate else
1410Sstevel@tonic-gate (void) printf("file %d: record"
1420Sstevel@tonic-gate " %ld: size %d\n",
1430Sstevel@tonic-gate filen, lcount, ln);
1440Sstevel@tonic-gate ln = n;
1450Sstevel@tonic-gate lcount = count;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate } else {
1480Sstevel@tonic-gate if (ln <= 0 && ln != -2) {
1490Sstevel@tonic-gate (void) printf("eot\n");
1500Sstevel@tonic-gate break;
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate if (ln > 0)
1530Sstevel@tonic-gate if (count - lcount > 1)
1540Sstevel@tonic-gate (void) printf("file %d: records %ld to"
1550Sstevel@tonic-gate " %ld: size " "%d\n",
1560Sstevel@tonic-gate filen, lcount, count-1, ln);
1570Sstevel@tonic-gate else
1580Sstevel@tonic-gate (void) printf("file %d: record %ld:"
1590Sstevel@tonic-gate " size %d\n", filen, lcount, ln);
1600Sstevel@tonic-gate (void) printf("file %d: eof after %ld records:"
1610Sstevel@tonic-gate " %lld bytes\n", filen, count-1, size);
1620Sstevel@tonic-gate if (copy) {
1630Sstevel@tonic-gate op.mt_op = MTWEOF;
1640Sstevel@tonic-gate op.mt_count = (daddr_t)1;
1650Sstevel@tonic-gate if (ioctl(outp, MTIOCTOP, (char *)&op) < 0) {
1660Sstevel@tonic-gate perror("Write EOF");
1670Sstevel@tonic-gate return (6);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate filen++;
1710Sstevel@tonic-gate count = 0;
1720Sstevel@tonic-gate lcount = 0;
1730Sstevel@tonic-gate tsize += size;
1740Sstevel@tonic-gate size = 0;
1750Sstevel@tonic-gate if (nfile && filen > nfile)
1760Sstevel@tonic-gate break;
1770Sstevel@tonic-gate ln = n;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate if (copy)
1810Sstevel@tonic-gate (void) close(outp);
1820Sstevel@tonic-gate (void) printf("total length: %lld bytes\n", tsize);
1830Sstevel@tonic-gate return (0);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate void
RUBOUT()1870Sstevel@tonic-gate RUBOUT()
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate if (count > lcount)
1900Sstevel@tonic-gate --count;
1910Sstevel@tonic-gate if (count)
1920Sstevel@tonic-gate if (count > lcount)
1930Sstevel@tonic-gate (void) printf("file %d: records %ld to %ld: size"
1940Sstevel@tonic-gate " %d\n", filen, lcount, count, ln);
1950Sstevel@tonic-gate else
1960Sstevel@tonic-gate (void) printf("file %d: record %ld: size %d\n",
1970Sstevel@tonic-gate filen, lcount, ln);
1980Sstevel@tonic-gate (void) printf("interrupted at file %d: record %ld\n", filen, count);
1990Sstevel@tonic-gate (void) printf("total length: %lld bytes\n", tsize+size);
2000Sstevel@tonic-gate exit(1);
2010Sstevel@tonic-gate }
202