port to tahoe by Nir peleg of CCI
[unix-history] / usr / src / usr.bin / tcopy / tcopy.c
CommitLineData
f7c68695
JB
1/*
2 * Copyright (c) 1985 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1985 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
13#ifndef lint
8927fb63 14static char sccsid[] = "@(#)tcopy.c 5.3 (Berkeley) %G%";
f7c68695
JB
15#endif not lint
16
17#include <stdio.h>
c11c323a 18#include <signal.h>
f7c68695
JB
19#include <sys/file.h>
20#include <sys/types.h>
21#include <sys/ioctl.h>
22#include <sys/mtio.h>
23
24#define SIZE (64 * 1024)
25
26char buff[SIZE];
27int filen=1;
28long count, lcount;
29int RUBOUT();
30long itol();
31int nfile;
32long size, tsize;
33int ln;
34char *inf, *outf;
c11c323a 35int copy;
f7c68695
JB
36
37main(argc, argv)
38char **argv;
39{
40 register n, nw, inp, outp;
41 struct mtop op;
42
8927fb63 43 if (argc <=1 || argc > 3) {
c11c323a 44 fprintf(stderr, "Usage: tcopy src [dest]\n");
f7c68695
JB
45 exit(1);
46 }
47 inf = argv[1];
c11c323a
JB
48 if (argc == 3) {
49 outf = argv[2];
50 copy = 1;
51 }
f7c68695
JB
52 if ((inp=open(inf, O_RDONLY, 0666)) < 0) {
53 fprintf(stderr,"Can't open %s\n", inf);
54 exit(1);
55 }
c11c323a
JB
56 if (copy) {
57 if ((outp=open(outf, O_WRONLY, 0666)) < 0) {
58 fprintf(stderr,"Can't open %s\n", outf);
59 exit(3);
60 }
f7c68695 61 }
c11c323a
JB
62 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
63 (void) signal(SIGINT, RUBOUT);
f7c68695
JB
64 ln = -2;
65 for (;;) {
66 count++;
67 n = read(inp, buff, SIZE);
68 if (n > 0) {
69 nw = write(outp, buff, n);
c11c323a
JB
70 if (copy) {
71 if (nw != n) {
72 fprintf(stderr, "write (%d) != read (%d)\n",
73 nw, n);
74 fprintf(stderr, "COPY Aborted\n");
75 exit(5);
76 }
f7c68695
JB
77 }
78 size += n;
79 if (n != ln) {
80 if (ln > 0)
81 if (count - lcount > 1)
82 printf("file %d: records %ld to %ld: size %d\n",
83 filen, lcount, count-1, ln);
84 else
85 printf("file %d: record %ld: size %d\n",
86 filen, lcount, ln);
87 ln = n;
88 lcount = count;
89 }
90 }
91 else {
92 if (ln <= 0 && ln != -2) {
93 printf("eot\n");
94 break;
95 }
96 if (ln > 0)
97 if (count - lcount > 1)
98 printf("file %d: records %ld to %ld: size %d\n",
99 filen, lcount, count-1, ln);
100 else
101 printf("file %d: record %ld: size %d\n",
102 filen, lcount, ln);
103 printf("file %d: eof after %ld records: %ld bytes\n",
104 filen, count-1, size);
c11c323a
JB
105 if (copy) {
106 op.mt_op = MTWEOF;
107 op.mt_count = (daddr_t)1;
108 if(ioctl(outp, MTIOCTOP, (char *)&op) < 0) {
109 perror("Write EOF");
110 exit(6);
111 }
f7c68695
JB
112 }
113 filen++;
114 count = 0;
115 lcount = 0;
116 tsize += size;
117 size = 0;
118 if (nfile && filen > nfile)
119 break;
120 ln = n;
121 }
122 }
c11c323a
JB
123 if (copy)
124 (void) close(outp);
f7c68695
JB
125 printf("total length: %ld bytes\n", tsize);
126}
127
128RUBOUT()
129{
130 if (count > lcount)
131 --count;
132 if (count)
133 if (count > lcount)
134 printf("file %d: records %ld to %ld: size %d\n",
135 filen, lcount, count, ln);
136 else
137 printf("file %d: record %ld: size %d\n",
138 filen, lcount, ln);
139 printf("rubout at file %d: record %ld\n", filen, count);
140 printf("total length: %ld bytes\n", tsize+size);
141 exit(1);
142}
143