tcopy.c revision 1.2 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.2 mycroft /*static char sccsid[] = "from: @(#)tcopy.c 5.15 (Berkeley) 11/5/90";*/
42 1.2 mycroft static char rcsid[] = "$Id: tcopy.c,v 1.2 1993/08/01 18:07:38 mycroft Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/types.h>
46 1.1 cgd #include <sys/signal.h>
47 1.1 cgd #include <sys/file.h>
48 1.1 cgd #include <sys/ioctl.h>
49 1.1 cgd #include <sys/mtio.h>
50 1.1 cgd #include <sys/errno.h>
51 1.1 cgd #include <stdio.h>
52 1.1 cgd #include "pathnames.h"
53 1.1 cgd
54 1.1 cgd #define MAXREC (64 * 1024)
55 1.1 cgd #define NOCOUNT (-2)
56 1.1 cgd
57 1.1 cgd int filen, guesslen, maxblk = MAXREC;
58 1.1 cgd long lastrec, record, size, tsize;
59 1.1 cgd
60 1.1 cgd main(argc, argv)
61 1.1 cgd int argc;
62 1.1 cgd char **argv;
63 1.1 cgd {
64 1.1 cgd extern char *optarg;
65 1.1 cgd extern int optind, errno;
66 1.1 cgd register int lastnread, nread, nw, inp, outp;
67 1.1 cgd enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
68 1.1 cgd sig_t oldsig;
69 1.1 cgd int ch, needeof;
70 1.1 cgd char *buff, *inf, *getspace();
71 1.1 cgd void intr();
72 1.1 cgd
73 1.1 cgd guesslen = 1;
74 1.1 cgd while ((ch = getopt(argc, argv, "cs:v")) != EOF)
75 1.1 cgd switch((char)ch) {
76 1.1 cgd case 'c':
77 1.1 cgd op = COPYVERIFY;
78 1.1 cgd break;
79 1.1 cgd case 's':
80 1.1 cgd maxblk = atoi(optarg);
81 1.1 cgd if (maxblk <= 0) {
82 1.1 cgd fprintf(stderr, "tcopy: illegal block size\n");
83 1.1 cgd usage();
84 1.1 cgd }
85 1.1 cgd guesslen = 0;
86 1.1 cgd break;
87 1.1 cgd case 'v':
88 1.1 cgd op = VERIFY;
89 1.1 cgd break;
90 1.1 cgd case '?':
91 1.1 cgd default:
92 1.1 cgd usage();
93 1.1 cgd }
94 1.1 cgd argc -= optind;
95 1.1 cgd argv += optind;
96 1.1 cgd
97 1.1 cgd switch(argc) {
98 1.1 cgd case 0:
99 1.1 cgd if (op != READ)
100 1.1 cgd usage();
101 1.1 cgd inf = _PATH_DEFTAPE;
102 1.1 cgd break;
103 1.1 cgd case 1:
104 1.1 cgd if (op != READ)
105 1.1 cgd usage();
106 1.1 cgd inf = argv[0];
107 1.1 cgd break;
108 1.1 cgd case 2:
109 1.1 cgd if (op == READ)
110 1.1 cgd op = COPY;
111 1.1 cgd inf = argv[0];
112 1.1 cgd if ((outp = open(argv[1], op == VERIFY ? O_RDONLY : O_RDWR,
113 1.1 cgd 0666)) < 0) {
114 1.1 cgd perror(argv[1]);
115 1.1 cgd exit(3);
116 1.1 cgd }
117 1.1 cgd break;
118 1.1 cgd default:
119 1.1 cgd usage();
120 1.1 cgd }
121 1.1 cgd
122 1.1 cgd if ((inp = open(inf, O_RDONLY, 0)) < 0) {
123 1.1 cgd perror(inf);
124 1.1 cgd exit(1);
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd buff = getspace(maxblk);
128 1.1 cgd
129 1.1 cgd if (op == VERIFY) {
130 1.1 cgd verify(inp, outp, buff);
131 1.1 cgd exit(0);
132 1.1 cgd }
133 1.1 cgd
134 1.1 cgd if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
135 1.1 cgd (void) signal(SIGINT, intr);
136 1.1 cgd
137 1.1 cgd needeof = 0;
138 1.1 cgd for (lastnread = NOCOUNT;;) {
139 1.1 cgd if ((nread = read(inp, buff, maxblk)) == -1) {
140 1.1 cgd while (errno == EINVAL && (maxblk -= 1024)) {
141 1.1 cgd nread = read(inp, buff, maxblk);
142 1.1 cgd if (nread >= 0)
143 1.1 cgd goto r1;
144 1.1 cgd }
145 1.1 cgd fprintf(stderr, "read error, file %d, record %ld: ",
146 1.1 cgd filen, record);
147 1.1 cgd perror("");
148 1.1 cgd exit(1);
149 1.1 cgd } else if (nread != lastnread) {
150 1.1 cgd if (lastnread != 0 && lastnread != NOCOUNT) {
151 1.1 cgd if (lastrec == 0 && nread == 0)
152 1.1 cgd printf("%ld records\n", record);
153 1.1 cgd else if (record - lastrec > 1)
154 1.1 cgd printf("records %ld to %ld\n",
155 1.1 cgd lastrec, record);
156 1.1 cgd else
157 1.1 cgd printf("record %ld\n", lastrec);
158 1.1 cgd }
159 1.1 cgd if (nread != 0)
160 1.1 cgd printf("file %d: block size %d: ",
161 1.1 cgd filen, nread);
162 1.1 cgd (void) fflush(stdout);
163 1.1 cgd lastrec = record;
164 1.1 cgd }
165 1.1 cgd r1: guesslen = 0;
166 1.1 cgd if (nread > 0) {
167 1.1 cgd if (op == COPY || op == COPYVERIFY) {
168 1.1 cgd if (needeof) {
169 1.1 cgd writeop(outp, MTWEOF);
170 1.1 cgd needeof = 0;
171 1.1 cgd }
172 1.1 cgd nw = write(outp, buff, nread);
173 1.1 cgd if (nw != nread) {
174 1.1 cgd fprintf(stderr,
175 1.1 cgd "write error, file %d, record %ld: ",
176 1.1 cgd filen, record);
177 1.1 cgd if (nw == -1)
178 1.1 cgd perror("");
179 1.1 cgd else
180 1.1 cgd fprintf(stderr,
181 1.1 cgd "write (%d) != read (%d)\n",
182 1.1 cgd nw, nread);
183 1.1 cgd fprintf(stderr, "copy aborted\n");
184 1.1 cgd exit(5);
185 1.1 cgd }
186 1.1 cgd }
187 1.1 cgd size += nread;
188 1.1 cgd record++;
189 1.1 cgd } else {
190 1.1 cgd if (lastnread <= 0 && lastnread != NOCOUNT) {
191 1.1 cgd printf("eot\n");
192 1.1 cgd break;
193 1.1 cgd }
194 1.1 cgd printf("file %d: eof after %ld records: %ld bytes\n",
195 1.1 cgd filen, record, size);
196 1.1 cgd needeof = 1;
197 1.1 cgd filen++;
198 1.1 cgd tsize += size;
199 1.1 cgd size = record = lastrec = 0;
200 1.1 cgd lastnread = 0;
201 1.1 cgd }
202 1.1 cgd lastnread = nread;
203 1.1 cgd }
204 1.1 cgd printf("total length: %ld bytes\n", tsize);
205 1.1 cgd (void)signal(SIGINT, oldsig);
206 1.1 cgd if (op == COPY || op == COPYVERIFY) {
207 1.1 cgd writeop(outp, MTWEOF);
208 1.1 cgd writeop(outp, MTWEOF);
209 1.1 cgd if (op == COPYVERIFY) {
210 1.1 cgd writeop(outp, MTREW);
211 1.1 cgd writeop(inp, MTREW);
212 1.1 cgd verify(inp, outp, buff);
213 1.1 cgd }
214 1.1 cgd }
215 1.1 cgd exit(0);
216 1.1 cgd }
217 1.1 cgd
218 1.1 cgd verify(inp, outp, outb)
219 1.1 cgd register int inp, outp;
220 1.1 cgd register char *outb;
221 1.1 cgd {
222 1.1 cgd extern int errno;
223 1.1 cgd register int eot, inmaxblk, inn, outmaxblk, outn;
224 1.1 cgd register char *inb;
225 1.1 cgd char *getspace();
226 1.1 cgd
227 1.1 cgd inb = getspace(maxblk);
228 1.1 cgd inmaxblk = outmaxblk = maxblk;
229 1.1 cgd for (eot = 0;; guesslen = 0) {
230 1.1 cgd if ((inn = read(inp, inb, inmaxblk)) == -1) {
231 1.1 cgd if (guesslen)
232 1.1 cgd while (errno == EINVAL && (inmaxblk -= 1024)) {
233 1.1 cgd inn = read(inp, inb, inmaxblk);
234 1.1 cgd if (inn >= 0)
235 1.1 cgd goto r1;
236 1.1 cgd }
237 1.1 cgd perror("tcopy: read error");
238 1.1 cgd break;
239 1.1 cgd }
240 1.1 cgd r1: if ((outn = read(outp, outb, outmaxblk)) == -1) {
241 1.1 cgd if (guesslen)
242 1.1 cgd while (errno == EINVAL && (outmaxblk -= 1024)) {
243 1.1 cgd outn = read(outp, outb, outmaxblk);
244 1.1 cgd if (outn >= 0)
245 1.1 cgd goto r2;
246 1.1 cgd }
247 1.1 cgd perror("tcopy: read error");
248 1.1 cgd break;
249 1.1 cgd }
250 1.1 cgd r2: if (inn != outn) {
251 1.1 cgd printf("tcopy: tapes have different block sizes; %d != %d.\n", inn, outn);
252 1.1 cgd break;
253 1.1 cgd }
254 1.1 cgd if (!inn) {
255 1.1 cgd if (eot++) {
256 1.1 cgd printf("tcopy: tapes are identical.\n");
257 1.1 cgd return;
258 1.1 cgd }
259 1.1 cgd } else {
260 1.1 cgd if (bcmp(inb, outb, inn)) {
261 1.1 cgd printf("tcopy: tapes have different data.\n");
262 1.1 cgd break;
263 1.1 cgd }
264 1.1 cgd eot = 0;
265 1.1 cgd }
266 1.1 cgd }
267 1.1 cgd exit(1);
268 1.1 cgd }
269 1.1 cgd
270 1.1 cgd void
271 1.1 cgd intr()
272 1.1 cgd {
273 1.1 cgd if (record)
274 1.1 cgd if (record - lastrec > 1)
275 1.1 cgd printf("records %ld to %ld\n", lastrec, record);
276 1.1 cgd else
277 1.1 cgd printf("record %ld\n", lastrec);
278 1.1 cgd printf("interrupt at file %d: record %ld\n", filen, record);
279 1.1 cgd printf("total length: %ld bytes\n", tsize + size);
280 1.1 cgd exit(1);
281 1.1 cgd }
282 1.1 cgd
283 1.1 cgd char *
284 1.1 cgd getspace(blk)
285 1.1 cgd int blk;
286 1.1 cgd {
287 1.1 cgd char *bp, *malloc();
288 1.1 cgd
289 1.1 cgd if ((bp = malloc((u_int)blk)) == NULL) {
290 1.1 cgd fprintf(stderr, "tcopy: no memory\n");
291 1.1 cgd exit(11);
292 1.1 cgd }
293 1.1 cgd return(bp);
294 1.1 cgd }
295 1.1 cgd
296 1.1 cgd writeop(fd, type)
297 1.1 cgd int fd, type;
298 1.1 cgd {
299 1.1 cgd struct mtop op;
300 1.1 cgd
301 1.1 cgd op.mt_op = type;
302 1.1 cgd op.mt_count = (daddr_t)1;
303 1.1 cgd if (ioctl(fd, MTIOCTOP, (char *)&op) < 0) {
304 1.1 cgd perror("tcopy: tape op");
305 1.1 cgd exit(6);
306 1.1 cgd }
307 1.1 cgd }
308 1.1 cgd
309 1.1 cgd usage()
310 1.1 cgd {
311 1.1 cgd fprintf(stderr, "usage: tcopy [-cv] [-s maxblk] src [dest]\n");
312 1.1 cgd exit(1);
313 1.1 cgd }
314