xref: /csrg-svn/usr.bin/tcopy/tcopy.c (revision 24973)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1985 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)tcopy.c	5.1 (Berkeley) 09/20/85";
15 #endif not lint
16 
17 #include <stdio.h>
18 #include <sys/file.h>
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/mtio.h>
22 
23 #define SIZE	(64 * 1024)
24 
25 char buff[SIZE];
26 int filen=1;
27 long count, lcount;
28 int RUBOUT();
29 long itol();
30 int nfile;
31 long size, tsize;
32 int ln;
33 char *inf, *outf;
34 
35 main(argc, argv)
36 char **argv;
37 {
38 	register n, nw, inp, outp;
39 	struct mtop op;
40 
41 	if (argc != 3) {
42 		fprintf(stderr, "Usage: tcopy src dest\n");
43 		exit(1);
44 	}
45 	inf = argv[1];
46 	outf = argv[2];
47 	if ((inp=open(inf, O_RDONLY, 0666)) < 0) {
48 		fprintf(stderr,"Can't open %s\n", inf);
49 		exit(1);
50 	}
51 	op.mt_op = MTREW;
52 	op.mt_count = (daddr_t)1;
53 	if(ioctl(inp, MTIOCTOP, &op) < 0) {
54 		perror(inf);
55 		exit(2);
56 	}
57 	if ((outp=open(outf, O_WRONLY, 0666)) < 0) {
58 		fprintf(stderr,"Can't open %s\n", outf);
59 		exit(3);
60 	}
61 	if(ioctl(outp, MTIOCTOP, &op) < 0) {
62 		perror(inf);
63 		exit(4);
64 	}
65 	if (signal(2, 1) != 1)
66 		signal(2, RUBOUT);
67 	ln = -2;
68 	for (;;) {
69 		count++;
70 		n = read(inp, buff, SIZE);
71 		if (n > 0) {
72 		    nw = write(outp, buff, n);
73 		    if (nw != n) {
74 			fprintf(stderr, "write (%d) != read (%d)\n", nw, n);
75 			fprintf(stderr, "COPY Aborted\n");
76 			exit(5);
77 		    }
78 		    size += n;
79 		    if (n != ln) {
80 			if (ln > 0)
81 			    if (count - lcount > 1)
82 				printf("file %d: records %ld to %ld: size %d\n",
83 					filen, lcount, count-1, ln);
84 			    else
85 				printf("file %d: record %ld: size %d\n",
86 					filen, lcount, ln);
87 			ln = n;
88 			lcount = count;
89 		    }
90 		}
91 		else {
92 			if (ln <= 0 && ln != -2) {
93 				printf("eot\n");
94 				break;
95 			}
96 			if (ln > 0)
97 			    if (count - lcount > 1)
98 				printf("file %d: records %ld to %ld: size %d\n",
99 					filen, lcount, count-1, ln);
100 			    else
101 				printf("file %d: record %ld: size %d\n",
102 					filen, lcount, ln);
103 			printf("file %d: eof after %ld records: %ld bytes\n",
104 				filen, count-1, size);
105 			op.mt_op = MTWEOF;
106 			op.mt_count = (daddr_t)1;
107 			if(ioctl(outp, MTIOCTOP, &op) < 0) {
108 				perror("Write EOF");
109 				exit(6);
110 			}
111 			filen++;
112 			count = 0;
113 			lcount = 0;
114 			tsize += size;
115 			size = 0;
116 			if (nfile && filen > nfile)
117 				break;
118 			ln = n;
119 		}
120 	}
121 	close(outp);
122 	printf("total length: %ld bytes\n", tsize);
123 }
124 
125 RUBOUT()
126 {
127 	if (count > lcount)
128 		--count;
129 	if (count)
130 		if (count > lcount)
131 			printf("file %d: records %ld to %ld: size %d\n",
132 				filen, lcount, count, ln);
133 		else
134 			printf("file %d: record %ld: size %d\n",
135 				filen, lcount, ln);
136 	printf("rubout at file %d: record %ld\n", filen, count);
137 	printf("total length: %ld bytes\n", tsize+size);
138 	exit(1);
139 }
140 
141