tcopy.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1985, 1987 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1985, 1987 Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.1 cgd static char sccsid[] = "@(#)tcopy.c 5.15 (Berkeley) 11/5/90";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/types.h>
45 1.1 cgd #include <sys/signal.h>
46 1.1 cgd #include <sys/file.h>
47 1.1 cgd #include <sys/ioctl.h>
48 1.1 cgd #include <sys/mtio.h>
49 1.1 cgd #include <sys/errno.h>
50 1.1 cgd #include <stdio.h>
51 1.1 cgd #include "pathnames.h"
52 1.1 cgd
53 1.1 cgd #define MAXREC (64 * 1024)
54 1.1 cgd #define NOCOUNT (-2)
55 1.1 cgd
56 1.1 cgd int filen, guesslen, maxblk = MAXREC;
57 1.1 cgd long lastrec, record, size, tsize;
58 1.1 cgd
59 1.1 cgd main(argc, argv)
60 1.1 cgd int argc;
61 1.1 cgd char **argv;
62 1.1 cgd {
63 1.1 cgd extern char *optarg;
64 1.1 cgd extern int optind, errno;
65 1.1 cgd register int lastnread, nread, nw, inp, outp;
66 1.1 cgd enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
67 1.1 cgd sig_t oldsig;
68 1.1 cgd int ch, needeof;
69 1.1 cgd char *buff, *inf, *getspace();
70 1.1 cgd void intr();
71 1.1 cgd
72 1.1 cgd guesslen = 1;
73 1.1 cgd while ((ch = getopt(argc, argv, "cs:v")) != EOF)
74 1.1 cgd switch((char)ch) {
75 1.1 cgd case 'c':
76 1.1 cgd op = COPYVERIFY;
77 1.1 cgd break;
78 1.1 cgd case 's':
79 1.1 cgd maxblk = atoi(optarg);
80 1.1 cgd if (maxblk <= 0) {
81 1.1 cgd fprintf(stderr, "tcopy: illegal block size\n");
82 1.1 cgd usage();
83 1.1 cgd }
84 1.1 cgd guesslen = 0;
85 1.1 cgd break;
86 1.1 cgd case 'v':
87 1.1 cgd op = VERIFY;
88 1.1 cgd break;
89 1.1 cgd case '?':
90 1.1 cgd default:
91 1.1 cgd usage();
92 1.1 cgd }
93 1.1 cgd argc -= optind;
94 1.1 cgd argv += optind;
95 1.1 cgd
96 1.1 cgd switch(argc) {
97 1.1 cgd case 0:
98 1.1 cgd if (op != READ)
99 1.1 cgd usage();
100 1.1 cgd inf = _PATH_DEFTAPE;
101 1.1 cgd break;
102 1.1 cgd case 1:
103 1.1 cgd if (op != READ)
104 1.1 cgd usage();
105 1.1 cgd inf = argv[0];
106 1.1 cgd break;
107 1.1 cgd case 2:
108 1.1 cgd if (op == READ)
109 1.1 cgd op = COPY;
110 1.1 cgd inf = argv[0];
111 1.1 cgd if ((outp = open(argv[1], op == VERIFY ? O_RDONLY : O_RDWR,
112 1.1 cgd 0666)) < 0) {
113 1.1 cgd perror(argv[1]);
114 1.1 cgd exit(3);
115 1.1 cgd }
116 1.1 cgd break;
117 1.1 cgd default:
118 1.1 cgd usage();
119 1.1 cgd }
120 1.1 cgd
121 1.1 cgd if ((inp = open(inf, O_RDONLY, 0)) < 0) {
122 1.1 cgd perror(inf);
123 1.1 cgd exit(1);
124 1.1 cgd }
125 1.1 cgd
126 1.1 cgd buff = getspace(maxblk);
127 1.1 cgd
128 1.1 cgd if (op == VERIFY) {
129 1.1 cgd verify(inp, outp, buff);
130 1.1 cgd exit(0);
131 1.1 cgd }
132 1.1 cgd
133 1.1 cgd if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
134 1.1 cgd (void) signal(SIGINT, intr);
135 1.1 cgd
136 1.1 cgd needeof = 0;
137 1.1 cgd for (lastnread = NOCOUNT;;) {
138 1.1 cgd if ((nread = read(inp, buff, maxblk)) == -1) {
139 1.1 cgd while (errno == EINVAL && (maxblk -= 1024)) {
140 1.1 cgd nread = read(inp, buff, maxblk);
141 1.1 cgd if (nread >= 0)
142 1.1 cgd goto r1;
143 1.1 cgd }
144 1.1 cgd fprintf(stderr, "read error, file %d, record %ld: ",
145 1.1 cgd filen, record);
146 1.1 cgd perror("");
147 1.1 cgd exit(1);
148 1.1 cgd } else if (nread != lastnread) {
149 1.1 cgd if (lastnread != 0 && lastnread != NOCOUNT) {
150 1.1 cgd if (lastrec == 0 && nread == 0)
151 1.1 cgd printf("%ld records\n", record);
152 1.1 cgd else if (record - lastrec > 1)
153 1.1 cgd printf("records %ld to %ld\n",
154 1.1 cgd lastrec, record);
155 1.1 cgd else
156 1.1 cgd printf("record %ld\n", lastrec);
157 1.1 cgd }
158 1.1 cgd if (nread != 0)
159 1.1 cgd printf("file %d: block size %d: ",
160 1.1 cgd filen, nread);
161 1.1 cgd (void) fflush(stdout);
162 1.1 cgd lastrec = record;
163 1.1 cgd }
164 1.1 cgd r1: guesslen = 0;
165 1.1 cgd if (nread > 0) {
166 1.1 cgd if (op == COPY || op == COPYVERIFY) {
167 1.1 cgd if (needeof) {
168 1.1 cgd writeop(outp, MTWEOF);
169 1.1 cgd needeof = 0;
170 1.1 cgd }
171 1.1 cgd nw = write(outp, buff, nread);
172 1.1 cgd if (nw != nread) {
173 1.1 cgd fprintf(stderr,
174 1.1 cgd "write error, file %d, record %ld: ",
175 1.1 cgd filen, record);
176 1.1 cgd if (nw == -1)
177 1.1 cgd perror("");
178 1.1 cgd else
179 1.1 cgd fprintf(stderr,
180 1.1 cgd "write (%d) != read (%d)\n",
181 1.1 cgd nw, nread);
182 1.1 cgd fprintf(stderr, "copy aborted\n");
183 1.1 cgd exit(5);
184 1.1 cgd }
185 1.1 cgd }
186 1.1 cgd size += nread;
187 1.1 cgd record++;
188 1.1 cgd } else {
189 1.1 cgd if (lastnread <= 0 && lastnread != NOCOUNT) {
190 1.1 cgd printf("eot\n");
191 1.1 cgd break;
192 1.1 cgd }
193 1.1 cgd printf("file %d: eof after %ld records: %ld bytes\n",
194 1.1 cgd filen, record, size);
195 1.1 cgd needeof = 1;
196 1.1 cgd filen++;
197 1.1 cgd tsize += size;
198 1.1 cgd size = record = lastrec = 0;
199 1.1 cgd lastnread = 0;
200 1.1 cgd }
201 1.1 cgd lastnread = nread;
202 1.1 cgd }
203 1.1 cgd printf("total length: %ld bytes\n", tsize);
204 1.1 cgd (void)signal(SIGINT, oldsig);
205 1.1 cgd if (op == COPY || op == COPYVERIFY) {
206 1.1 cgd writeop(outp, MTWEOF);
207 1.1 cgd writeop(outp, MTWEOF);
208 1.1 cgd if (op == COPYVERIFY) {
209 1.1 cgd writeop(outp, MTREW);
210 1.1 cgd writeop(inp, MTREW);
211 1.1 cgd verify(inp, outp, buff);
212 1.1 cgd }
213 1.1 cgd }
214 1.1 cgd exit(0);
215 1.1 cgd }
216 1.1 cgd
217 1.1 cgd verify(inp, outp, outb)
218 1.1 cgd register int inp, outp;
219 1.1 cgd register char *outb;
220 1.1 cgd {
221 1.1 cgd extern int errno;
222 1.1 cgd register int eot, inmaxblk, inn, outmaxblk, outn;
223 1.1 cgd register char *inb;
224 1.1 cgd char *getspace();
225 1.1 cgd
226 1.1 cgd inb = getspace(maxblk);
227 1.1 cgd inmaxblk = outmaxblk = maxblk;
228 1.1 cgd for (eot = 0;; guesslen = 0) {
229 1.1 cgd if ((inn = read(inp, inb, inmaxblk)) == -1) {
230 1.1 cgd if (guesslen)
231 1.1 cgd while (errno == EINVAL && (inmaxblk -= 1024)) {
232 1.1 cgd inn = read(inp, inb, inmaxblk);
233 1.1 cgd if (inn >= 0)
234 1.1 cgd goto r1;
235 1.1 cgd }
236 1.1 cgd perror("tcopy: read error");
237 1.1 cgd break;
238 1.1 cgd }
239 1.1 cgd r1: if ((outn = read(outp, outb, outmaxblk)) == -1) {
240 1.1 cgd if (guesslen)
241 1.1 cgd while (errno == EINVAL && (outmaxblk -= 1024)) {
242 1.1 cgd outn = read(outp, outb, outmaxblk);
243 1.1 cgd if (outn >= 0)
244 1.1 cgd goto r2;
245 1.1 cgd }
246 1.1 cgd perror("tcopy: read error");
247 1.1 cgd break;
248 1.1 cgd }
249 1.1 cgd r2: if (inn != outn) {
250 1.1 cgd printf("tcopy: tapes have different block sizes; %d != %d.\n", inn, outn);
251 1.1 cgd break;
252 1.1 cgd }
253 1.1 cgd if (!inn) {
254 1.1 cgd if (eot++) {
255 1.1 cgd printf("tcopy: tapes are identical.\n");
256 1.1 cgd return;
257 1.1 cgd }
258 1.1 cgd } else {
259 1.1 cgd if (bcmp(inb, outb, inn)) {
260 1.1 cgd printf("tcopy: tapes have different data.\n");
261 1.1 cgd break;
262 1.1 cgd }
263 1.1 cgd eot = 0;
264 1.1 cgd }
265 1.1 cgd }
266 1.1 cgd exit(1);
267 1.1 cgd }
268 1.1 cgd
269 1.1 cgd void
270 1.1 cgd intr()
271 1.1 cgd {
272 1.1 cgd if (record)
273 1.1 cgd if (record - lastrec > 1)
274 1.1 cgd printf("records %ld to %ld\n", lastrec, record);
275 1.1 cgd else
276 1.1 cgd printf("record %ld\n", lastrec);
277 1.1 cgd printf("interrupt at file %d: record %ld\n", filen, record);
278 1.1 cgd printf("total length: %ld bytes\n", tsize + size);
279 1.1 cgd exit(1);
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd char *
283 1.1 cgd getspace(blk)
284 1.1 cgd int blk;
285 1.1 cgd {
286 1.1 cgd char *bp, *malloc();
287 1.1 cgd
288 1.1 cgd if ((bp = malloc((u_int)blk)) == NULL) {
289 1.1 cgd fprintf(stderr, "tcopy: no memory\n");
290 1.1 cgd exit(11);
291 1.1 cgd }
292 1.1 cgd return(bp);
293 1.1 cgd }
294 1.1 cgd
295 1.1 cgd writeop(fd, type)
296 1.1 cgd int fd, type;
297 1.1 cgd {
298 1.1 cgd struct mtop op;
299 1.1 cgd
300 1.1 cgd op.mt_op = type;
301 1.1 cgd op.mt_count = (daddr_t)1;
302 1.1 cgd if (ioctl(fd, MTIOCTOP, (char *)&op) < 0) {
303 1.1 cgd perror("tcopy: tape op");
304 1.1 cgd exit(6);
305 1.1 cgd }
306 1.1 cgd }
307 1.1 cgd
308 1.1 cgd usage()
309 1.1 cgd {
310 1.1 cgd fprintf(stderr, "usage: tcopy [-cv] [-s maxblk] src [dest]\n");
311 1.1 cgd exit(1);
312 1.1 cgd }
313