17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <stdio.h> 437c478bd9Sstevel@tonic-gate #include <signal.h> 447c478bd9Sstevel@tonic-gate #include <sys/types.h> 457c478bd9Sstevel@tonic-gate #include <sys/file.h> 467c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 477c478bd9Sstevel@tonic-gate #include <sys/mtio.h> 487c478bd9Sstevel@tonic-gate #include <stdlib.h> 497c478bd9Sstevel@tonic-gate #include <limits.h> 507c478bd9Sstevel@tonic-gate #include <errno.h> 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate char *buff; /* buffer for read/write */ 537c478bd9Sstevel@tonic-gate int filen = 1; /* file number being processed */ 547c478bd9Sstevel@tonic-gate long count, lcount; /* number of blocks for file */ 557c478bd9Sstevel@tonic-gate extern void RUBOUT(); 567c478bd9Sstevel@tonic-gate int nfile; /* used for ??? */ 577c478bd9Sstevel@tonic-gate off64_t size, tsize; /* number of bytes in file, total */ 587c478bd9Sstevel@tonic-gate int ln; 597c478bd9Sstevel@tonic-gate char *inf, *outf; 607c478bd9Sstevel@tonic-gate int copy; 617c478bd9Sstevel@tonic-gate size_t size_64K = 64 * 1024; 627c478bd9Sstevel@tonic-gate size_t size_256K = 256 * 1024; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate int 667c478bd9Sstevel@tonic-gate main(argc, argv) 677c478bd9Sstevel@tonic-gate char **argv; 687c478bd9Sstevel@tonic-gate { 69*4fce32e1Smuffin int n, nw, inp, outp; 707c478bd9Sstevel@tonic-gate struct mtop op; 717c478bd9Sstevel@tonic-gate size_t buf_size = size_64K; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate if (argc <= 1 || argc > 3) { 747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Usage: tcopy src [dest]\n"); 757c478bd9Sstevel@tonic-gate return (1); 767c478bd9Sstevel@tonic-gate } 777c478bd9Sstevel@tonic-gate inf = argv[1]; 787c478bd9Sstevel@tonic-gate if (argc == 3) { 797c478bd9Sstevel@tonic-gate outf = argv[2]; 807c478bd9Sstevel@tonic-gate copy = 1; 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate if ((inp = open(inf, O_RDONLY, 0666)) < 0) { 837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Can't open %s\n", inf); 847c478bd9Sstevel@tonic-gate return (1); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate if (copy) { 877c478bd9Sstevel@tonic-gate if ((outp = open(outf, O_WRONLY, 0666)) < 0) { 887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Can't open %s\n", outf); 897c478bd9Sstevel@tonic-gate return (3); 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate if ((buff = malloc(buf_size)) == NULL) { 937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Can't allocate memory for tcopy\n"); 947c478bd9Sstevel@tonic-gate return (4); 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != SIG_IGN) 977c478bd9Sstevel@tonic-gate (void) signal(SIGINT, RUBOUT); 987c478bd9Sstevel@tonic-gate ln = -2; 997c478bd9Sstevel@tonic-gate for (;;) { 1007c478bd9Sstevel@tonic-gate count++; 1017c478bd9Sstevel@tonic-gate errno = 0; 1027c478bd9Sstevel@tonic-gate while ((n = read(inp, buff, buf_size)) < 0 && 1037c478bd9Sstevel@tonic-gate errno == ENOMEM && buf_size < INT_MAX) { 1047c478bd9Sstevel@tonic-gate if (buf_size < size_256K) 1057c478bd9Sstevel@tonic-gate buf_size = size_256K; 1067c478bd9Sstevel@tonic-gate else 1077c478bd9Sstevel@tonic-gate buf_size *= 2; 1087c478bd9Sstevel@tonic-gate free(buff); 1097c478bd9Sstevel@tonic-gate if ((buff = malloc(buf_size)) == NULL) { 1107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1117c478bd9Sstevel@tonic-gate "Can't allocate memory for tcopy\n"); 1127c478bd9Sstevel@tonic-gate return (4); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate op.mt_op = MTFSF; /* Rewind to start of file */ 1157c478bd9Sstevel@tonic-gate op.mt_count = (daddr_t)0; 1167c478bd9Sstevel@tonic-gate if (ioctl(inp, MTIOCTOP, (char *)&op) < 0) { 1177c478bd9Sstevel@tonic-gate perror("Read record size"); 1187c478bd9Sstevel@tonic-gate return (6); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate errno = 0; 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate if (n > 0) { 1237c478bd9Sstevel@tonic-gate if (copy) { 1247c478bd9Sstevel@tonic-gate nw = write(outp, buff, (size_t)n); 1257c478bd9Sstevel@tonic-gate if (nw != n) { 1267c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "write (%d) !=" 1277c478bd9Sstevel@tonic-gate " read (%d)\n", nw, n); 1287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "COPY " 1297c478bd9Sstevel@tonic-gate "Aborted\n"); 1307c478bd9Sstevel@tonic-gate return (5); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate size += n; 1347c478bd9Sstevel@tonic-gate if (n != ln) { 1357c478bd9Sstevel@tonic-gate if (ln > 0) 1367c478bd9Sstevel@tonic-gate if (count - lcount > 1) 1377c478bd9Sstevel@tonic-gate (void) printf("file %d: records" 1387c478bd9Sstevel@tonic-gate " %ld to %ld: size %d\n", 1397c478bd9Sstevel@tonic-gate filen, lcount, count-1, ln); 1407c478bd9Sstevel@tonic-gate else 1417c478bd9Sstevel@tonic-gate (void) printf("file %d: record" 1427c478bd9Sstevel@tonic-gate " %ld: size %d\n", 1437c478bd9Sstevel@tonic-gate filen, lcount, ln); 1447c478bd9Sstevel@tonic-gate ln = n; 1457c478bd9Sstevel@tonic-gate lcount = count; 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate } else { 1487c478bd9Sstevel@tonic-gate if (ln <= 0 && ln != -2) { 1497c478bd9Sstevel@tonic-gate (void) printf("eot\n"); 1507c478bd9Sstevel@tonic-gate break; 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate if (ln > 0) 1537c478bd9Sstevel@tonic-gate if (count - lcount > 1) 1547c478bd9Sstevel@tonic-gate (void) printf("file %d: records %ld to" 1557c478bd9Sstevel@tonic-gate " %ld: size " "%d\n", 1567c478bd9Sstevel@tonic-gate filen, lcount, count-1, ln); 1577c478bd9Sstevel@tonic-gate else 1587c478bd9Sstevel@tonic-gate (void) printf("file %d: record %ld:" 1597c478bd9Sstevel@tonic-gate " size %d\n", filen, lcount, ln); 1607c478bd9Sstevel@tonic-gate (void) printf("file %d: eof after %ld records:" 1617c478bd9Sstevel@tonic-gate " %lld bytes\n", filen, count-1, size); 1627c478bd9Sstevel@tonic-gate if (copy) { 1637c478bd9Sstevel@tonic-gate op.mt_op = MTWEOF; 1647c478bd9Sstevel@tonic-gate op.mt_count = (daddr_t)1; 1657c478bd9Sstevel@tonic-gate if (ioctl(outp, MTIOCTOP, (char *)&op) < 0) { 1667c478bd9Sstevel@tonic-gate perror("Write EOF"); 1677c478bd9Sstevel@tonic-gate return (6); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate filen++; 1717c478bd9Sstevel@tonic-gate count = 0; 1727c478bd9Sstevel@tonic-gate lcount = 0; 1737c478bd9Sstevel@tonic-gate tsize += size; 1747c478bd9Sstevel@tonic-gate size = 0; 1757c478bd9Sstevel@tonic-gate if (nfile && filen > nfile) 1767c478bd9Sstevel@tonic-gate break; 1777c478bd9Sstevel@tonic-gate ln = n; 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate if (copy) 1817c478bd9Sstevel@tonic-gate (void) close(outp); 1827c478bd9Sstevel@tonic-gate (void) printf("total length: %lld bytes\n", tsize); 1837c478bd9Sstevel@tonic-gate return (0); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate void 1877c478bd9Sstevel@tonic-gate RUBOUT() 1887c478bd9Sstevel@tonic-gate { 1897c478bd9Sstevel@tonic-gate if (count > lcount) 1907c478bd9Sstevel@tonic-gate --count; 1917c478bd9Sstevel@tonic-gate if (count) 1927c478bd9Sstevel@tonic-gate if (count > lcount) 1937c478bd9Sstevel@tonic-gate (void) printf("file %d: records %ld to %ld: size" 1947c478bd9Sstevel@tonic-gate " %d\n", filen, lcount, count, ln); 1957c478bd9Sstevel@tonic-gate else 1967c478bd9Sstevel@tonic-gate (void) printf("file %d: record %ld: size %d\n", 1977c478bd9Sstevel@tonic-gate filen, lcount, ln); 1987c478bd9Sstevel@tonic-gate (void) printf("interrupted at file %d: record %ld\n", filen, count); 1997c478bd9Sstevel@tonic-gate (void) printf("total length: %lld bytes\n", tsize+size); 2007c478bd9Sstevel@tonic-gate exit(1); 2017c478bd9Sstevel@tonic-gate } 202