xref: /csrg-svn/usr.bin/tcopy/tcopy.c (revision 25590)
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.2 (Berkeley) 12/11/85";
15 #endif not lint
16 
17 #include <stdio.h>
18 #include <signal.h>
19 #include <sys/file.h>
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/mtio.h>
23 
24 #define SIZE	(64 * 1024)
25 
26 char buff[SIZE];
27 int filen=1;
28 long count, lcount;
29 int RUBOUT();
30 long itol();
31 int nfile;
32 long size, tsize;
33 int ln;
34 char *inf, *outf;
35 int copy;
36 
37 main(argc, argv)
38 char **argv;
39 {
40 	register n, nw, inp, outp;
41 	struct mtop op;
42 
43 	if (argc <=1 || argc >= 3) {
44 		fprintf(stderr, "Usage: tcopy src [dest]\n");
45 		exit(1);
46 	}
47 	inf = argv[1];
48 	if (argc == 3) {
49 		outf = argv[2];
50 		copy = 1;
51 	}
52 	if ((inp=open(inf, O_RDONLY, 0666)) < 0) {
53 		fprintf(stderr,"Can't open %s\n", inf);
54 		exit(1);
55 	}
56 	if (copy) {
57 		if ((outp=open(outf, O_WRONLY, 0666)) < 0) {
58 			fprintf(stderr,"Can't open %s\n", outf);
59 			exit(3);
60 		}
61 	}
62 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
63 		(void) signal(SIGINT, RUBOUT);
64 	ln = -2;
65 	for (;;) {
66 		count++;
67 		n = read(inp, buff, SIZE);
68 		if (n > 0) {
69 		    nw = write(outp, buff, n);
70 		    if (copy) {
71 			    if (nw != n) {
72 				fprintf(stderr, "write (%d) != read (%d)\n",
73 					nw, n);
74 				fprintf(stderr, "COPY Aborted\n");
75 				exit(5);
76 			    }
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 			if (copy) {
106 				op.mt_op = MTWEOF;
107 				op.mt_count = (daddr_t)1;
108 				if(ioctl(outp, MTIOCTOP, (char *)&op) < 0) {
109 					perror("Write EOF");
110 					exit(6);
111 				}
112 			}
113 			filen++;
114 			count = 0;
115 			lcount = 0;
116 			tsize += size;
117 			size = 0;
118 			if (nfile && filen > nfile)
119 				break;
120 			ln = n;
121 		}
122 	}
123 	if (copy)
124 		(void) close(outp);
125 	printf("total length: %ld bytes\n", tsize);
126 }
127 
128 RUBOUT()
129 {
130 	if (count > lcount)
131 		--count;
132 	if (count)
133 		if (count > lcount)
134 			printf("file %d: records %ld to %ld: size %d\n",
135 				filen, lcount, count, ln);
136 		else
137 			printf("file %d: record %ld: size %d\n",
138 				filen, lcount, ln);
139 	printf("rubout at file %d: record %ld\n", filen, count);
140 	printf("total length: %ld bytes\n", tsize+size);
141 	exit(1);
142 }
143 
144