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