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