]>
git.saurik.com Git - apple/file_cmds.git/blob - tcopy/tcopy.c
297ad01c7726d0c2bd58a526c9e9b3fcd7a4873c
1 /* $NetBSD: tcopy.c,v 1.8 1998/08/25 20:59:41 ross Exp $ */
4 * Copyright (c) 1985, 1987, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1985, 1987, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)tcopy.c 8.3 (Berkeley) 1/23/95";
46 __RCSID("$NetBSD: tcopy.c,v 1.8 1998/08/25 20:59:41 ross Exp $");
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
64 #define MAXREC (64 * 1024)
67 int filen
, guesslen
, maxblk
= MAXREC
;
72 void *getspace
__P((int));
74 int main
__P((int, char **));
75 void usage
__P((void));
76 void verify
__P((int, int, char *));
77 void writeop
__P((int, int));
84 int ch
, needeof
, nw
, inp
, outp
;
85 ssize_t lastnread
, nread
;
86 enum {READ
, VERIFY
, COPY
, COPYVERIFY
} op
= READ
;
93 while ((ch
= getopt(argc
, argv
, "cs:vx")) != -1)
99 maxblk
= atoi(optarg
);
101 warnx("illegal block size");
134 if ((outp
= open(argv
[1], op
== VERIFY
? O_RDONLY
:
135 op
== COPY
? O_WRONLY
: O_RDWR
, DEFFILEMODE
)) < 0) {
143 if ((inp
= open(inf
, O_RDONLY
, 0)) < 0)
146 buff
= getspace(maxblk
);
149 verify(inp
, outp
, buff
);
153 if ((oldsig
= signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
154 (void) signal(SIGINT
, intr
);
157 for (lastnread
= NOCOUNT
;;) {
158 if ((nread
= read(inp
, buff
, maxblk
)) == -1) {
159 while (errno
== EINVAL
&& (maxblk
-= 1024)) {
160 nread
= read(inp
, buff
, maxblk
);
164 err(1, "read error, file %d, record %ld",
166 } else if (nread
!= lastnread
) {
167 if (lastnread
!= 0 && lastnread
!= NOCOUNT
) {
168 if (lastrec
== 0 && nread
== 0)
169 fprintf(msg
, "%ld records\n", record
);
170 else if (record
- lastrec
> 1)
171 fprintf(msg
, "records %ld to %ld\n",
174 fprintf(msg
, "record %ld\n", lastrec
);
177 fprintf(msg
, "file %d: block size %ld: ",
179 (void) fflush(stdout
);
184 if (op
== COPY
|| op
== COPYVERIFY
) {
186 writeop(outp
, MTWEOF
);
189 nw
= write(outp
, buff
, nread
);
193 "write error, file %d, record %ld: ",
197 ": %s", strerror(error
));
200 "write (%d) != read (%ld)\n",
202 fprintf(stderr
, "copy aborted\n");
209 if (lastnread
<= 0 && lastnread
!= NOCOUNT
) {
210 fprintf(msg
, "eot\n");
214 "file %d: eof after %ld records: %qd bytes\n",
215 filen
, record
, (long long)size
);
219 size
= record
= lastrec
= 0;
224 fprintf(msg
, "total length: %qd bytes\n", (long long)tsize
);
225 (void)signal(SIGINT
, oldsig
);
226 if (op
== COPY
|| op
== COPYVERIFY
) {
227 writeop(outp
, MTWEOF
);
228 writeop(outp
, MTWEOF
);
229 if (op
== COPYVERIFY
) {
230 writeop(outp
, MTREW
);
232 verify(inp
, outp
, buff
);
239 verify(inp
, outp
, outb
)
243 int eot
, inmaxblk
, inn
, outmaxblk
, outn
;
246 inb
= getspace(maxblk
);
247 inmaxblk
= outmaxblk
= maxblk
;
248 for (eot
= 0;; guesslen
= 0) {
249 if ((inn
= read(inp
, inb
, inmaxblk
)) == -1) {
251 while (errno
== EINVAL
&& (inmaxblk
-= 1024)) {
252 inn
= read(inp
, inb
, inmaxblk
);
259 r1
: if ((outn
= read(outp
, outb
, outmaxblk
)) == -1) {
261 while (errno
== EINVAL
&& (outmaxblk
-= 1024)) {
262 outn
= read(outp
, outb
, outmaxblk
);
269 r2
: if (inn
!= outn
) {
271 "%s: tapes have different block sizes; %d != %d.\n",
277 fprintf(msg
, "%s: tapes are identical.\n",
282 if (memcmp(inb
, outb
, inn
)) {
284 "%s: tapes have different data.\n",
299 if (record
- lastrec
> 1)
300 fprintf(msg
, "records %ld to %ld\n", lastrec
, record
);
302 fprintf(msg
, "record %ld\n", lastrec
);
304 fprintf(msg
, "interrupt at file %d: record %ld\n", filen
, record
);
305 fprintf(msg
, "total length: %qd bytes\n", (long long)(tsize
+ size
));
315 if ((bp
= malloc((size_t)blk
)) == NULL
)
316 errx(11, "no memory");
328 op
.mt_count
= (daddr_t
)1;
329 if (ioctl(fd
, MTIOCTOP
, (char *)&op
) < 0)
337 fprintf(stderr
, "usage: tcopy [-cvx] [-s maxblk] src [dest]\n");