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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/*
* Copyright (c) 1993 Steve Gerakines
*
* This is freely redistributable software. You may do anything you
* wish with it, so long as the above notice stays intact.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ft.c - simple floppy tape filter
*
* 01/28/94 v0.3b (Jim Babb)
* Fixed bug when all sectors in a segment are marked bad.
*
* 10/30/93 v0.3
* Minor revisions. Seems pretty stable.
*
* 09/02/93 v0.2 pl01
* Initial revision.
*
* usage: ftfilt [ -f tape ] [ description ]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/ftape.h>
#define DEFQIC "/dev/rft0"
char buff[QCV_SEGSIZE]; /* scratch buffer */
char hbuff[QCV_SEGSIZE]; /* header buffer */
QIC_Header *hptr = (QIC_Header *)hbuff; /* header structure */
int hsn = -1; /* segment number of header */
int dhsn = -1; /* segment number of duplicate header */
int tfd; /* tape file descriptor */
QIC_Geom geo; /* tape geometry */
int tvno = 1; /* tape volume number */
int tvlast; /* TRUE if last volume in set */
long tvsize = 0; /* tape volume size in bytes */
long tvtime = NULL; /* tape change time */
char *tvnote = ""; /* tape note */
/* Lookup the badmap for a given track and segment. */
#define BADMAP(t,s) hptr->qh_badmap[(t)*geo.g_segtrk+(s)]
/* Retrieve values from a character array. */
#define UL_VAL(s,p) (*((ULONG *)&(s)[p]))
#define US_VAL(s,p) (*((USHORT *)&(s)[p]))
#define equal(s1,s2) (strcmp(s1, s2) == 0)
/* Entry */
main(int argc, char *argv[])
{
int r, s;
char *tape, *getenv();
if (argc > 2 && (equal(argv[1], "-t") || equal(argv[1], "-f"))) {
argc -= 2;
tape = argv[2];
argv += 2;
} else
if ((tape = getenv("TAPE")) == NULL)
tape = DEFQIC;
if (argc > 1) {
tvnote = argv[1];
if (strlen(tvnote) > 18) argv[1][18] = '\0';
}
/* Open the tape device */
if ((tfd = open(tape, 2)) < 0) {
perror(tape);
exit(1);
}
if (!isatty(0))
do_write();
else if (!isatty(1))
do_read();
else
do_getname();
close(tfd);
exit(0);
}
/* Check status of tape drive */
int check_stat(int fd, int wr)
{
int r, s;
int sawit = 0;
/* get tape status */
if (ioctl(fd, QIOSTATUS, &s) < 0) {
fprintf(stderr, "could not get drive status\n");
return(1);
}
/* wait for the tape drive to become ready */
while ((s & QS_READY) == 0) {
if (!sawit) {
fprintf(stderr, "waiting for drive to become ready...\n");
sawit = 1;
}
sleep(2);
if (ioctl(fd, QIOSTATUS, &s) < 0) {
fprintf(stderr, "could not get drive status\n");
return(1);
}
}
if ((s & QS_FMTOK) == 0) {
fprintf(stderr, "tape is not formatted\n");
return(2);
}
if (wr && (s & QS_RDONLY) != 0) {
fprintf(stderr, "tape is write protected\n");
return(3);
}
return(0);
}
ULONG qtimeval(time_t t)
{
struct tm *tp;
ULONG r;
tp = localtime(&t);
r = 2678400 * tp->tm_mon +
86400 *(tp->tm_mday-1) +
3600 * tp->tm_hour +
60 * tp->tm_min +
tp->tm_sec;
r |= (tp->tm_year - 70) << 25;
return(r);
}
/* Return tm struct from QIC date format. */
struct tm *qtime(UCHAR *qt)
{
ULONG *vp = (ULONG *)qt;
struct tm t;
ULONG v;
time_t tv;
v = *vp;
t.tm_year = ((v >> 25) & 0x7f)+70; v &= 0x1ffffff;
t.tm_mon = v / 2678400; v %= 2678400;
t.tm_mday = v / 86400 + 1; v %= 86400;
t.tm_hour = v / 3600; v %= 3600;
t.tm_min = v / 60; v %= 60;
t.tm_sec = v;
t.tm_wday = 0; /* XXX - let mktime do the real work */
t.tm_yday = 0;
t.tm_isdst = 0;
t.tm_gmtoff = 0;
t.tm_zone = NULL;
tv = mktime(&t);
return(localtime(&tv));
}
/* Return a string, zero terminated */
char *qstr(char *str, int nchar)
{
static char tstr[256];
strncpy(tstr, str, nchar);
tstr[nchar] = '\0';
return(tstr);
}
/* Read header from tape */
get_header(int fd)
{
int r, sn, bytes;
QIC_Segment s;
int gothdr = 0;
if (ioctl(fd, QIOGEOM, &geo) < 0) {
fprintf(stderr, "couldn't determine tape geometry\n");
return(1);
}
/* Get the header and duplicate */
for (sn = 0; sn < 16; sn++) {
s.sg_trk = 0;
s.sg_seg = sn;
s.sg_badmap = 0;
s.sg_data = (UCHAR *)&buff[0];
ioctl(fd, QIOREAD, &s);
r = check_parity(s.sg_data, 0, s.sg_crcmap);
if (s.sg_data[0] == 0x55 && s.sg_data[1] == 0xaa &&
s.sg_data[2] == 0x55 && s.sg_data[3] == 0xaa) {
if (hsn >= 0) {
dhsn = sn;
if (!r && !gothdr) {
fprintf(stderr, "using secondary header\n");
bcopy(s.sg_data, hbuff, QCV_SEGSIZE);
gothdr = 1;
}
break;
}
hsn = sn;
if (!r) {
bcopy(s.sg_data, hbuff, QCV_SEGSIZE);
gothdr = 1;
} else {
fprintf(stderr, "too many errors in primary header\n");
}
}
}
if (!gothdr) {
fprintf(stderr, "couldn't read header segment\n");
ioctl(fd, QIOREWIND);
return(1);
}
return(0);
}
ask_vol(int vn)
{
FILE *inp;
int fd;
char c;
if ((fd = open("/dev/tty", 2)) < 0) {
fprintf(stderr, "argh!! can't open /dev/tty\n");
exit(1);
}
fprintf(stderr, "Insert ftfilt volume %02d and press enter:", vn);
read(fd, &c, 1);
close(fd);
}
/* Return the name of the tape only. */
do_getname()
{
if (check_stat(tfd, 0)) exit(1);
if (get_header(tfd)) exit(1);
fprintf(stderr, "\"%s\" - %s",
qstr(hptr->qh_tname,44), asctime(qtime(hptr->qh_chgdate)));
ioctl(tfd, QIOREWIND);
}
/* Extract data from tape to stdout */
do_read()
{
int sno, vno, sbytes, r;
long curpos;
char *hname;
QIC_Segment s;
/* Process multiple volumes if necessary */
vno = 1;
for (;;) {
if (check_stat(tfd, 0)) {
ask_vol(vno);
continue;
}
if (get_header(tfd)) {
ask_vol(vno);
continue;
}
/* extract volume and header info from label */
hname = hptr->qh_tname;
hname[43] = '\0';
tvno = atoi(&hname[11]);
tvlast = (hname[10] == '*') ? 1 : 0;
tvsize = atoi(&hname[14]);
tvnote = &hname[25];
if (vno != tvno || strncmp(hname, "ftfilt", 6) != 0) {
fprintf(stderr, "Incorrect volume inserted. This tape is:\n");
fprintf(stderr,"\"%s\" - %s\n", hname,
asctime(qtime(hptr->qh_chgdate)));
ask_vol(vno);
continue;
}
/* Process this volume */
curpos = 0;
for (sno = hptr->qh_first; tvsize > 0; sno++) {
s.sg_trk = sno / geo.g_segtrk;
s.sg_seg = sno % geo.g_segtrk;
s.sg_badmap = BADMAP(s.sg_trk,s.sg_seg);
sbytes = sect_bytes(s.sg_badmap) - QCV_ECCSIZE;
s.sg_data = (UCHAR *)&buff[0];
/* skip segments with *all* sectors flagged as bad */
if (sbytes > 0) {
if (ioctl(tfd, QIOREAD, &s) < 0) perror("QIOREAD");
r = check_parity(s.sg_data, s.sg_badmap, s.sg_crcmap);
if (r) fprintf(stderr, "** warning: ecc failed at byte %ld\n",
curpos);
if (tvsize < sbytes) sbytes = tvsize;
write(1, s.sg_data, sbytes);
tvsize -= sbytes;
curpos += sbytes;
}
}
if (tvlast) break;
ioctl(tfd, QIOREWIND);
ask_vol(++vno);
}
ioctl(tfd, QIOREWIND);
return(0);
}
/* Dump data from stdin to tape */
do_write()
{
int sno, vno, amt, sbytes;
int c, maxseg, r;
ULONG qnow;
QIC_Segment s;
char tmpstr[80];
qnow = qtimeval(time(NULL));
vno = 1;
for (;;) {
if (check_stat(tfd, 1)) {
ask_vol(vno);
continue;
}
if (get_header(tfd)) {
ask_vol(vno);
continue;
}
maxseg = geo.g_segtrk * geo.g_trktape - 1;
sno = hptr->qh_first;
tvno = vno;
tvsize = 0;
tvlast = 0;
/* Process until end of volume or end of data */
for (sno = hptr->qh_first; sno < maxseg && tvlast == 0; ++sno) {
/* Prepare to load the next segment */
s.sg_trk = sno / geo.g_segtrk;
s.sg_seg = sno % geo.g_segtrk;
s.sg_badmap = BADMAP(s.sg_trk,s.sg_seg);
sbytes = sect_bytes(s.sg_badmap) - QCV_ECCSIZE;
s.sg_data = (UCHAR *)&buff[0];
/* Ugh. Loop to get the full amt. */
for (amt = 0; amt < sbytes; amt += r) {
r = read(0, &s.sg_data[amt], sbytes - amt);
if (r <= 0) {
tvlast = 1;
break;
}
}
/* skip the segment if *all* sectors are flagged as bad */
if (amt) {
if (amt < sbytes)
bzero(&s.sg_data[amt], sbytes - amt);
r = set_parity(s.sg_data, s.sg_badmap);
if (r) fprintf(stderr, "** warning: ecc problem !!\n");
if (ioctl(tfd, QIOWRITE, &s) < 0) {
perror("QIOWRITE");
exit(1);
}
tvsize += amt;
}
}
/* Build new header info */
/* ftfilt vol*xx yyyyyyyyyy note56789012345678 */
/* 01234567890123456789012345678901234567890123 */
sprintf(tmpstr, "ftfilt vol%s%02d %010d %s",
(tvlast) ? "*" : " ", tvno, tvsize, tvnote);
strncpy(hptr->qh_tname, tmpstr, 44);
UL_VAL(hptr->qh_chgdate,0) = qnow;
/* Update the header for this volume */
if (hsn >= 0) {
s.sg_trk = hsn / geo.g_segtrk;
s.sg_seg = hsn % geo.g_segtrk;
s.sg_badmap = 0;
s.sg_data = (UCHAR *)hbuff;
r = set_parity(s.sg_data, s.sg_badmap);
if (r) fprintf(stderr, "** warning: header ecc problem !!\n");
if (ioctl(tfd, QIOWRITE, &s) < 0) {
perror("QIOWRITE");
exit(1);
}
}
if (dhsn >= 0) {
s.sg_trk = dhsn / geo.g_segtrk;
s.sg_seg = dhsn % geo.g_segtrk;
s.sg_badmap = 0;
s.sg_data = (UCHAR *)hbuff;
r = set_parity(s.sg_data, s.sg_badmap);
if (r) fprintf(stderr, "** warning: duphdr ecc problem !!\n");
if (ioctl(tfd, QIOWRITE, &s) < 0) {
perror("QIOWRITE");
exit(1);
}
}
ioctl(tfd, QIOREWIND);
if (tvlast) break;
ask_vol(++vno);
}
return(0);
}
|