]>
git.saurik.com Git - apple/file_cmds.git/blob - tcopy/tcopy.c
2 * Copyright (c) 1985, 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
37 static const char copyright
[] =
38 "@(#) Copyright (c) 1985, 1987, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static const char sccsid
[] = "@(#)tcopy.c 8.2 (Berkeley) 4/17/94";
46 #include <sys/types.h>
48 #include <sys/ioctl.h>
62 #define MAXREC (64 * 1024)
65 int filen
, guesslen
, maxblk
= MAXREC
;
66 u_int64_t lastrec
, record
, size
, tsize
;
71 static void usage(void);
72 void verify(int, int, char *);
73 void writeop(int, int);
74 void rewind_tape(int);
81 register int lastnread
, nread
, nw
, inp
, outp
;
82 enum {READ
, VERIFY
, COPY
, COPYVERIFY
} op
= READ
;
90 while ((ch
= getopt(argc
, argv
, "cs:vx")) != -1)
96 maxblk
= atoi(optarg
);
98 warnx("illegal block size");
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]);
139 if ((inp
= open(inf
, O_RDONLY
, 0)) < 0)
142 buff
= getspace(maxblk
);
145 verify(inp
, outp
, buff
);
149 if ((oldsig
= signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
150 (void) signal(SIGINT
, intr
);
153 for (lastnread
= NOCOUNT
;;) {
154 if ((nread
= read(inp
, buff
, maxblk
)) == -1) {
155 while (errno
== EINVAL
&& (maxblk
-= 1024)) {
156 nread
= read(inp
, buff
, maxblk
);
160 err(1, "read error, file %d, record %qu", filen
, record
);
161 } else if (nread
!= lastnread
) {
162 if (lastnread
!= 0 && lastnread
!= NOCOUNT
) {
163 if (lastrec
== 0 && nread
== 0)
164 fprintf(msg
, "%qu records\n", record
);
165 else if (record
- lastrec
> 1)
166 fprintf(msg
, "records %qu to %qu\n",
169 fprintf(msg
, "record %qu\n", lastrec
);
172 fprintf(msg
, "file %d: block size %d: ",
174 (void) fflush(stdout
);
179 if (op
== COPY
|| op
== COPYVERIFY
) {
181 writeop(outp
, MTWEOF
);
184 nw
= write(outp
, buff
, nread
);
187 warn("write error, file %d, record %qu", filen
, record
);
189 warnx("write error, file %d, record %qu", filen
, record
);
190 warnx("write (%d) != read (%d)", nw
, nread
);
192 errx(5, "copy aborted");
198 if (lastnread
<= 0 && lastnread
!= NOCOUNT
) {
199 fprintf(msg
, "eot\n");
203 "file %d: eof after %qu records: %qu bytes\n",
204 filen
, record
, size
);
208 size
= record
= lastrec
= 0;
213 fprintf(msg
, "total length: %qu bytes\n", tsize
);
214 (void)signal(SIGINT
, oldsig
);
215 if (op
== COPY
|| op
== COPYVERIFY
) {
216 writeop(outp
, MTWEOF
);
217 writeop(outp
, MTWEOF
);
218 if (op
== COPYVERIFY
) {
221 verify(inp
, outp
, buff
);
228 verify(inp
, outp
, outb
)
229 register int inp
, outp
;
232 register int eot
, inmaxblk
, inn
, outmaxblk
, outn
;
235 inb
= getspace(maxblk
);
236 inmaxblk
= outmaxblk
= maxblk
;
237 for (eot
= 0;; guesslen
= 0) {
238 if ((inn
= read(inp
, inb
, inmaxblk
)) == -1) {
240 while (errno
== EINVAL
&& (inmaxblk
-= 1024)) {
241 inn
= read(inp
, inb
, inmaxblk
);
248 r1
: if ((outn
= read(outp
, outb
, outmaxblk
)) == -1) {
250 while (errno
== EINVAL
&& (outmaxblk
-= 1024)) {
251 outn
= read(outp
, outb
, outmaxblk
);
258 r2
: if (inn
!= outn
) {
260 "%s: tapes have different block sizes; %d != %d.\n",
266 fprintf(msg
, "tcopy: tapes are identical.\n");
270 if (bcmp(inb
, outb
, inn
)) {
272 "tcopy: tapes have different data.\n");
286 if (record
- lastrec
> 1)
287 fprintf(msg
, "records %qu to %qu\n", lastrec
, record
);
289 fprintf(msg
, "record %qu\n", lastrec
);
291 fprintf(msg
, "interrupt at file %d: record %qu\n", filen
, record
);
292 fprintf(msg
, "total length: %ju bytes\n", (uintmax_t)(tsize
+ size
));
302 if ((bp
= malloc((size_t)blk
)) == NULL
)
303 errx(11, "no memory");
314 op
.mt_count
= (daddr_t
)1;
315 if (ioctl(fd
, MTIOCTOP
, (char *)&op
) < 0)
322 fprintf(stderr
, "usage: tcopy [-cvx] [-s maxblk] [src [dest]]\n");
332 errx(12, "fstat in rewind");
335 * don't want to do tape ioctl on regular files:
337 if( S_ISREG(sp
.st_mode
) ) {
338 if( lseek(fd
, 0, SEEK_SET
) == -1 )
341 /* assume its a tape */