xref: /titanic_54/usr/src/cmd/tcopy/tcopy.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <stdio.h>
43*7c478bd9Sstevel@tonic-gate #include <signal.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
48*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
49*7c478bd9Sstevel@tonic-gate #include <limits.h>
50*7c478bd9Sstevel@tonic-gate #include <errno.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate char *buff;				/* buffer for read/write */
53*7c478bd9Sstevel@tonic-gate int filen = 1;				/* file number being processed */
54*7c478bd9Sstevel@tonic-gate long count, lcount;			/* number of blocks for file */
55*7c478bd9Sstevel@tonic-gate extern void RUBOUT();
56*7c478bd9Sstevel@tonic-gate int nfile;				/* used for ??? */
57*7c478bd9Sstevel@tonic-gate off64_t size, tsize;			/* number of bytes in file, total */
58*7c478bd9Sstevel@tonic-gate int ln;
59*7c478bd9Sstevel@tonic-gate char *inf, *outf;
60*7c478bd9Sstevel@tonic-gate int copy;
61*7c478bd9Sstevel@tonic-gate size_t size_64K = 64 * 1024;
62*7c478bd9Sstevel@tonic-gate size_t size_256K = 256 * 1024;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate int
66*7c478bd9Sstevel@tonic-gate main(argc, argv)
67*7c478bd9Sstevel@tonic-gate char **argv;
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	register n, nw, inp, outp;
70*7c478bd9Sstevel@tonic-gate 	struct mtop op;
71*7c478bd9Sstevel@tonic-gate 	size_t buf_size = size_64K;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if (argc <= 1 || argc > 3) {
74*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Usage: tcopy src [dest]\n");
75*7c478bd9Sstevel@tonic-gate 		return (1);
76*7c478bd9Sstevel@tonic-gate 	}
77*7c478bd9Sstevel@tonic-gate 	inf = argv[1];
78*7c478bd9Sstevel@tonic-gate 	if (argc == 3) {
79*7c478bd9Sstevel@tonic-gate 		outf = argv[2];
80*7c478bd9Sstevel@tonic-gate 		copy = 1;
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 	if ((inp = open(inf, O_RDONLY, 0666)) < 0) {
83*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Can't open %s\n", inf);
84*7c478bd9Sstevel@tonic-gate 		return (1);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 	if (copy) {
87*7c478bd9Sstevel@tonic-gate 		if ((outp = open(outf, O_WRONLY, 0666)) < 0) {
88*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Can't open %s\n", outf);
89*7c478bd9Sstevel@tonic-gate 			return (3);
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 	if ((buff = malloc(buf_size)) == NULL) {
93*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Can't allocate memory for tcopy\n");
94*7c478bd9Sstevel@tonic-gate 		return (4);
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
97*7c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, RUBOUT);
98*7c478bd9Sstevel@tonic-gate 	ln = -2;
99*7c478bd9Sstevel@tonic-gate 	for (;;) {
100*7c478bd9Sstevel@tonic-gate 		count++;
101*7c478bd9Sstevel@tonic-gate 		errno = 0;
102*7c478bd9Sstevel@tonic-gate 		while ((n = read(inp, buff, buf_size)) < 0 &&
103*7c478bd9Sstevel@tonic-gate 		    errno == ENOMEM && buf_size < INT_MAX) {
104*7c478bd9Sstevel@tonic-gate 			if (buf_size < size_256K)
105*7c478bd9Sstevel@tonic-gate 				buf_size = size_256K;
106*7c478bd9Sstevel@tonic-gate 			else
107*7c478bd9Sstevel@tonic-gate 				buf_size *= 2;
108*7c478bd9Sstevel@tonic-gate 			free(buff);
109*7c478bd9Sstevel@tonic-gate 			if ((buff = malloc(buf_size)) == NULL) {
110*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
111*7c478bd9Sstevel@tonic-gate 				    "Can't allocate memory for tcopy\n");
112*7c478bd9Sstevel@tonic-gate 				return (4);
113*7c478bd9Sstevel@tonic-gate 			}
114*7c478bd9Sstevel@tonic-gate 			op.mt_op = MTFSF;	/* Rewind to start of file */
115*7c478bd9Sstevel@tonic-gate 			op.mt_count = (daddr_t)0;
116*7c478bd9Sstevel@tonic-gate 			if (ioctl(inp, MTIOCTOP, (char *)&op) < 0) {
117*7c478bd9Sstevel@tonic-gate 				perror("Read record size");
118*7c478bd9Sstevel@tonic-gate 				return (6);
119*7c478bd9Sstevel@tonic-gate 			}
120*7c478bd9Sstevel@tonic-gate 			errno = 0;
121*7c478bd9Sstevel@tonic-gate 		}
122*7c478bd9Sstevel@tonic-gate 		if (n > 0) {
123*7c478bd9Sstevel@tonic-gate 			if (copy) {
124*7c478bd9Sstevel@tonic-gate 				nw = write(outp, buff, (size_t)n);
125*7c478bd9Sstevel@tonic-gate 				if (nw != n) {
126*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "write (%d) !="
127*7c478bd9Sstevel@tonic-gate 					    " read (%d)\n", nw, n);
128*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "COPY "
129*7c478bd9Sstevel@tonic-gate 					    "Aborted\n");
130*7c478bd9Sstevel@tonic-gate 					return (5);
131*7c478bd9Sstevel@tonic-gate 				}
132*7c478bd9Sstevel@tonic-gate 			}
133*7c478bd9Sstevel@tonic-gate 			size += n;
134*7c478bd9Sstevel@tonic-gate 			if (n != ln) {
135*7c478bd9Sstevel@tonic-gate 				if (ln > 0)
136*7c478bd9Sstevel@tonic-gate 					if (count - lcount > 1)
137*7c478bd9Sstevel@tonic-gate 						(void) printf("file %d: records"
138*7c478bd9Sstevel@tonic-gate 						    " %ld to %ld: size %d\n",
139*7c478bd9Sstevel@tonic-gate 						    filen, lcount, count-1, ln);
140*7c478bd9Sstevel@tonic-gate 					else
141*7c478bd9Sstevel@tonic-gate 						(void) printf("file %d: record"
142*7c478bd9Sstevel@tonic-gate 						    " %ld: size %d\n",
143*7c478bd9Sstevel@tonic-gate 						    filen, lcount, ln);
144*7c478bd9Sstevel@tonic-gate 				ln = n;
145*7c478bd9Sstevel@tonic-gate 				lcount = count;
146*7c478bd9Sstevel@tonic-gate 			}
147*7c478bd9Sstevel@tonic-gate 		} else {
148*7c478bd9Sstevel@tonic-gate 			if (ln <= 0 && ln != -2) {
149*7c478bd9Sstevel@tonic-gate 				(void) printf("eot\n");
150*7c478bd9Sstevel@tonic-gate 				break;
151*7c478bd9Sstevel@tonic-gate 			}
152*7c478bd9Sstevel@tonic-gate 			if (ln > 0)
153*7c478bd9Sstevel@tonic-gate 				if (count - lcount > 1)
154*7c478bd9Sstevel@tonic-gate 					(void) printf("file %d: records %ld to"
155*7c478bd9Sstevel@tonic-gate 					    " %ld: size " "%d\n",
156*7c478bd9Sstevel@tonic-gate 					    filen, lcount, count-1, ln);
157*7c478bd9Sstevel@tonic-gate 				else
158*7c478bd9Sstevel@tonic-gate 					(void) printf("file %d: record %ld:"
159*7c478bd9Sstevel@tonic-gate 					    " size %d\n", filen, lcount, ln);
160*7c478bd9Sstevel@tonic-gate 				(void) printf("file %d: eof after %ld records:"
161*7c478bd9Sstevel@tonic-gate 				    " %lld bytes\n", filen, count-1, size);
162*7c478bd9Sstevel@tonic-gate 			if (copy) {
163*7c478bd9Sstevel@tonic-gate 				op.mt_op = MTWEOF;
164*7c478bd9Sstevel@tonic-gate 				op.mt_count = (daddr_t)1;
165*7c478bd9Sstevel@tonic-gate 				if (ioctl(outp, MTIOCTOP, (char *)&op) < 0) {
166*7c478bd9Sstevel@tonic-gate 					perror("Write EOF");
167*7c478bd9Sstevel@tonic-gate 					return (6);
168*7c478bd9Sstevel@tonic-gate 				}
169*7c478bd9Sstevel@tonic-gate 			}
170*7c478bd9Sstevel@tonic-gate 			filen++;
171*7c478bd9Sstevel@tonic-gate 			count = 0;
172*7c478bd9Sstevel@tonic-gate 			lcount = 0;
173*7c478bd9Sstevel@tonic-gate 			tsize += size;
174*7c478bd9Sstevel@tonic-gate 			size = 0;
175*7c478bd9Sstevel@tonic-gate 			if (nfile && filen > nfile)
176*7c478bd9Sstevel@tonic-gate 				break;
177*7c478bd9Sstevel@tonic-gate 			ln = n;
178*7c478bd9Sstevel@tonic-gate 		}
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 	if (copy)
181*7c478bd9Sstevel@tonic-gate 		(void) close(outp);
182*7c478bd9Sstevel@tonic-gate 	(void) printf("total length: %lld bytes\n", tsize);
183*7c478bd9Sstevel@tonic-gate 	return (0);
184*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate void
188*7c478bd9Sstevel@tonic-gate RUBOUT()
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate 	if (count > lcount)
191*7c478bd9Sstevel@tonic-gate 		--count;
192*7c478bd9Sstevel@tonic-gate 	if (count)
193*7c478bd9Sstevel@tonic-gate 		if (count > lcount)
194*7c478bd9Sstevel@tonic-gate 			(void) printf("file %d: records %ld to %ld: size"
195*7c478bd9Sstevel@tonic-gate 			    " %d\n", filen, lcount, count, ln);
196*7c478bd9Sstevel@tonic-gate 		else
197*7c478bd9Sstevel@tonic-gate 			(void) printf("file %d: record %ld: size %d\n",
198*7c478bd9Sstevel@tonic-gate 			    filen, lcount, ln);
199*7c478bd9Sstevel@tonic-gate 	(void) printf("interrupted at file %d: record %ld\n", filen, count);
200*7c478bd9Sstevel@tonic-gate 	(void) printf("total length: %lld bytes\n", tsize+size);
201*7c478bd9Sstevel@tonic-gate 	exit(1);
202*7c478bd9Sstevel@tonic-gate }
203