-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtapewrite.c
149 lines (122 loc) · 3.02 KB
/
tapewrite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
tapewrite
Copyright 1999, 2000 Eric Smith
Copyright 2016 Lars Brinkhoff
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as published
by the Free Software Foundation. Note that permission is not granted
to redistribute this program under the terms of any other version of the
General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdio.h"
#include "stdarg.h"
#include "stdlib.h"
#include "tapeio.h"
#define MAX_REC_LEN 32768
typedef unsigned int u32; /* non-portable!!! */
char *progname;
int print_verbose = 0;
void print_usage (FILE *f)
{
fprintf (f, "Usage: %s out files...\n", progname);
}
void fatal (int retval, char *fmt, ...)
{
va_list ap;
if (fmt)
{
fprintf (stderr, "%s: ", progname);
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
}
if (retval == 1)
print_usage (stderr);
exit (retval);
}
void verbose (char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
if (print_verbose)
vfprintf (stdout, fmt, ap);
va_end (ap);
}
int main (int argc, char *argv[])
{
u32 file = 0;
u32 record = 0;
u32 recordlen = 1024;
u32 filebytes = 0;
u32 tapebytes = 0;
u32 len;
tape_handle_t dst = NULL;
FILE *src = NULL;
char *buf;
int tape_flags = TF_DEFAULT;
progname = argv [0];
while (++argv, --argc)
{
if ((argv [0][0] == '-') && (argv [0][1] != '\0'))
{
if (argv [0][1] == 's')
tape_flags |= TF_SIMH;
else if (argv [0][1] == 'v')
print_verbose = 1;
else if (argv [0][1] == 'n')
{
++argv, --argc;
recordlen = atoi(argv [0]);
}
else
fatal (1, "unrecognized option '%s'\n", argv [0]);
}
else
{
dst = opentape (argv [0], 1, 1);
if (! dst)
fatal (3, "can't open destination tape\n");
break;
}
}
buf = malloc (MAX_REC_LEN);
if (! buf)
fatal (2, "can't allocate buffer\n");
tapeflags (dst, tape_flags);
while (++argv, --argc)
{
src = fopen(argv [0], "rb");
file++;
verbose ("reading from file %s\n", argv [0]);
for (;;)
{
len = fread (buf, 1, recordlen, src);
verbose ("read %u bytes\n", len);
if (len > 0)
{
putrec (dst, buf, recordlen);
tapebytes += recordlen;
filebytes += recordlen;
record++;
}
if (len < recordlen)
{
verbose ("end of file, %u records, %u bytes\n", record, filebytes);
fclose (src);
tapemark (dst);
record = 0;
filebytes = 0;
break;
}
}
}
closetape (dst);
verbose ("end of tape, %u files, %u bytes\n", file, tapebytes);
return (0);
}