-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathansitape.c
More file actions
1430 lines (1157 loc) · 29.5 KB
/
ansitape.c
File metadata and controls
1430 lines (1157 loc) · 29.5 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ANSITAPE.C
*
* This program creates, reads, writes, and takes directory listings of
* ANSI-formatted tapes.
*
* This program was developed at
*
* HQDA Army Artificial Intelligence Center
* Pentagon Attn: DACS-DMA
* Washington, DC 20310-0200
*
* Phone: (202) 694-6900
*
********************************************
*
* THIS PROGRAM IS IN THE PUBLIC DOMAIN
*
********************************************
*
* Modification History:
*
* 28 January 1987 David S. Hayes Cleaned up for Usenet,
and changed switch formats.
* 6 August 1986 David S. Hayes Added -st I/O switch.
* 28 July 1986 David S. Hayes Changed to stream-style tape
access, added IBM labels and EBCDIC code conversion.
* 18 July 1986 David S. Hayes Changed to correct problem with
files spanning more than one block.
* 17 June 1986 David S. Hayes Original version.
*/
#define SYSID "UNIXTAPEV2.0" /* version 2.0 patch level 0 */
#include <stdio.h>
#include <strings.h>
#include <ctype.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "ansitape.h"
#define DEFAULT_TAPE "/dev/rmt8"
#define IGN ' ' /* empty constant for hdr space fill */
/*
* The largest file that we will try to determine recordsize for.
* Anything larger than this, we just use recordsize = blocksize.
*/
#define READMAX 100000
#define LIST 1
#define EXTRACT 2
#define WRITE 3
#define CREATE 4
#define makelower(c) ( isupper(c) ? tolower(c) : c)
#define makeupper(c) ( islower(c) ? toupper(c) : c)
#define max(a,b) ( a >= b ? a : b )
#define min(a,b) ( a < b ? a : b )
#define SAME 0
typedef unsigned char FLAG; /* boolean values only */
#define TRUE '\001'
#define FALSE '\000'
extern int errno;
char *upstring();
char *downstring();
char *tail();
char *malloc();
char *alloca();
FLAG match();
FLAG eot();
char *labels[] = {"", "HDR", "EOV", "EOF", ""};
#define TOP_OF_FILE 1
#define END_OF_TAPE 2
#define END_OF_FILE 3
#define END_OF_SET 4
#define UNKNOWN_LABEL -1
struct ansi_vol1 vol1;
struct ansi_hdr1 hdr1;
struct ansi_hdr2 hdr2;
struct ansi_hdr3 hdr3;
struct ansi_hdr4 hdr4;
struct tape_control_block {
/* Stuff pertaining to the tape set. */
FLAG ebcdic;
FLAG ibm_format;
int reel_switch;
char set_name[6];
char tapesys[13];
char owner[14];
char vol_access;
int ansi_level;
int reel_count;
/* Stuff pertaining to an individual volume. */
char vol_name[6];
FLAG mounted;
int file_count;
/* Stuff for an individual file. */
char tape_filename1[17];
char tape_filename2[63];
char unix_filename[80];
int blk_size;
int rec_size;
char rec_format;
char car_control;
int generation;
int genversion;
char file_access;
long created;
long expires;
int density;
int blk_count;
int blk_offset;
/* Stuff to be able to read/write on the tape. */
int fd;
char *buffer;
int char_count;
int next;
FLAG eof;
FLAG open;
int rw_mode;
} tcb;
int func; /* What we're supposed to do */
char *tapename = DEFAULT_TAPE;
FLAG verbose = FALSE,/* be wordy */
query = FALSE, /* ask before doing */
no_hdr3 = FALSE,/* don't write HDR3 or HDR4 */
ibm_format = FALSE, /* use IBM standard label
* format */
ebcdic = FALSE, /* tape should be EBCDIC */
flag_bs = FALSE,/* blocksize was given */
flag_rs = FALSE,/* recordsize was given */
flag_stdio = FALSE; /* do file i/o using stdin/out */
char main_set_name[6] = "UNIX"; /* file set id */
char main_vol_name[6] = "UNIX"; /* currently-mounted
* volume */
char *username,
*progname;
int main_blk_size = 2048,
main_rec_size;
char main_rec_format = REC_VARIABLE; /* fixed or variable */
char main_car_control = CC_IMPLIED; /* carriage control */
char **main_file_list;
int main_file_count;
/*
* Here's the code.
*/
main(argc, argv)
int argc;
char *argv[];
{
int i;
char *getlogin();
setbuf(stderr, NULL); /* unbuffer error output */
username = getlogin();
progname = argv[0];
if (username) {
downstring(username, main_vol_name, 6);
downstring(username, main_set_name, 6);
};
if (argc >= 2) {
/* Now 1-character option names. */
for (i = 0; argv[1][i]; i++) {
if (argv[1][i] == '-')
continue; /* minus is optional on first keylist */
switch (argv[1][i]) {
case 't':
func = LIST;
break;
case 'x':
func = EXTRACT;
break;
case 'c':
func = CREATE;
break;
case 'r':
if (func != CREATE)
func = WRITE;
break;
case 'v':
verbose = TRUE;
break;
case 'q':
query = TRUE;
break;
/* Do file I/O to stdio instead. */
case 'f':
flag_stdio = TRUE;
break;
/*
* Suppress HDR3 headers for systems that can't handle
* them.
*/
case '3':
no_hdr3 = TRUE;
break;
/* Tape is to be written in ASCII. */
case 'a':
ebcdic = FALSE;
break;
#ifdef EBCDIC
case 'e':
ebcdic = TRUE;
break;
case 'i':
ibm_format = TRUE;
ebcdic = TRUE;
break;
#endif
default:
fprintf(stderr, "unknown key %c - ignored\n", argv[1][i]);
};
};
};
for (i = 2; i < argc; i++) {
/* Do 2-character options */
if (strncmp(argv[i], "cc=", 3) == SAME) {
switch (argv[i][3]) {
case 'f':
main_car_control = CC_FORTRAN;
break;
case 'i':
main_car_control = CC_IMPLIED;
break;
case 'e':
main_car_control = CC_EMBEDDED;
break;
default:
fprintf(stderr, "unknown carriage control option %s\n", argv[i]);
};
continue;
};
if (strncmp(argv[i], "rs=", 3) == SAME) {
if (strcmp(argv[i], "rs=r") == SAME) {
main_rec_size = 0;
main_rec_format = REC_VARIABLE;
} else {
flag_rs = TRUE;
sscanf(argv[i], "rs=%d", &main_rec_size);
main_rec_format = REC_FIXED;
};
continue;
};
if (strncmp(argv[i], "bs=", 3) == SAME) {
flag_bs = TRUE;
sscanf(argv[i], "bs=%d", &main_blk_size);
continue;
};
if (strncmp(argv[i], "vo=", 3) == SAME) {
downstring(&argv[i][3], main_set_name, 6);
downstring(&argv[i][3], main_vol_name, 6);
continue;
};
if (strncmp(argv[i], "rf=", 3) == SAME) {
switch (argv[i][3]) {
case 'f':
main_rec_format = REC_FIXED;
break;
case 'v':
main_rec_format = REC_VARIABLE;
break;
default:
fprintf(stderr, "unknown record format %c - ignored\n",
argv[i][3]);
};
continue;
};
if (strncmp(argv[i], "mt=", 3) == SAME) {
tapename = &argv[i][3];
continue;
};
/* That's not an option, assume it's a file name. */
break;
};
/* Figure out where and how many file names we have. */
main_file_count = argc - i;
main_file_list = argc ? &argv[i] : (char **) 0;
if (main_rec_size > main_blk_size) {
if (flag_rs) {
main_blk_size = main_rec_size;
fprintf(stderr, "blocksize adjusted up to recordsize (%d)\n",
main_rec_size);
} else {
main_rec_size = main_blk_size;
fprintf(stderr, "recordsize adjusted down to blocksize (%d)\n",
main_rec_size);
};
};
switch (func) {
case WRITE:
tcb.fd = open(tapename, O_RDWR, 0666);
break;
case CREATE:
tcb.fd = open(tapename, O_RDWR | O_CREAT | O_TRUNC, 0666);
break;
default:
tcb.fd = open(tapename, O_RDONLY, 0666);
}
if (errno) {
perror("can't open tape device");
exit(-1);
};
switch (func) {
case LIST:
list_volume_set();
break;
case CREATE:
case WRITE:
write_volume_set();
break;
case EXTRACT:
read_volume_set();
break;
default:
fprintf(stderr, "specify one of -t, -x, -c, -r\n");
exit(-1);
};
exit(0);
}
/* List contents of a full volume set. */
list_volume_set()
{
mount_tape();
for (; ansi_open(FREAD) == TOP_OF_FILE;) {
ansi_close();
list_file();
};
}
/* List one file, possibly with maximized verbosity. */
list_file()
{
printf("%-22s", tcb.unix_filename);
if (verbose) {
printf(" %5d blocks", tcb.blk_count);
printf(", rec fmt %s %d bytes",
tcb.rec_format == REC_FIXED ? "fixed" : "var max",
tcb.rec_size);
switch (tcb.car_control) {
case CC_IMPLIED:
printf(", cc implied");
break;
case CC_EMBEDDED:
printf(", cc embedded");
break;
case CC_FORTRAN:
printf(", cc fortran");
break;
default:
printf(", cc code '%c'", tcb.car_control);
};
};
putchar('\n');
}
/*
* Write files to a volume set. Uses the ansi_write buffered output
* function, which automatically chains to new tapes if it has to.
*/
write_volume_set()
{
int j;
/*
* First, make ready to write the tape. If this is create, then we
* must init the tape. If not, mount the tape, and advance to the
* logical end-of-volume-set.
*/
if (func == CREATE) {
tcb.reel_count = 0;
strncpy(tcb.vol_name, main_vol_name, 6);
strncpy(tcb.set_name, main_set_name, 6);
strncpy(tcb.owner, username, 14);
init_tape();
} else {
mount_tape();
do {
j = ansi_open(FREAD);
ansi_close();
} while (j != END_OF_SET);
tape(MTBSF, 1); /* back up past last tape mark */
};
for (j = 0; j < main_file_count; j++)
write_file(main_file_list[j]);
tape(MTWEOF, 2);
}
/*
* Write from a stream to a tape. Lots of special stuff, like record
* and block sizes, are imported implicitly from static variables that
* were initialized by command-line options.
*/
write_file(fn)
char *fn;
{
int stat;
FILE *fp;
struct stat sb; /* check on our input file */
int rlen; /* length of a single record */
FLAG disk_file; /* our input is from a rewind-able
* source */
if (query) {
fprintf(stderr, "r %s ? ", fn);
if (!yes_or_no())
return END_OF_FILE;
};
if (flag_stdio)
fp = stdin;
else
fp = fopen(fn, "r");
if (fp == NULL) {
fprintf(stderr, "can't read %s", fn);
perror(" - ignored");
return END_OF_FILE;
};
strncpy(tcb.unix_filename, tail(fn), 80);
if (verbose)
fprintf(stderr, "r %-17s", tcb.unix_filename);
tcb.ibm_format = ibm_format;
tcb.ebcdic = ebcdic;
tcb.rec_format = main_rec_format;
tcb.car_control = main_car_control;
tcb.generation = 1;
tcb.genversion = 0;
tcb.file_access = NOPROTECT;
tcb.blk_offset = 0;
tcb.blk_size = main_blk_size;
tcb.rec_size = flag_rs ? main_rec_size : main_blk_size;
errno = 0;
fstat(fileno(fp), &sb);
disk_file = (errno == 0) && (sb.st_mode & S_IFMT) == S_IFREG;
tcb.created = sb.st_ctime;
if (!flag_rs && disk_file && main_rec_format == REC_VARIABLE) {
if (sb.st_size < READMAX || main_rec_size == 0) {
tcb.rec_size = 0;
rlen = 4; /* line length field */
while (!feof(fp)) {
if (getc(fp) == '\n') {
tcb.rec_size = max(tcb.rec_size, rlen);
rlen = 4; /* line length field */
} else
rlen++;
};
rewind(fp);
};
};
if (tcb.rec_size > tcb.blk_size) {
fprintf(stderr, "%s recordsize (%d) is too big for blocksize - ignored\n", fn, tcb.rec_size);
return END_OF_FILE;
};
ansi_open(FWRITE);
do {
stat = write_record(fp);
} while (stat != END_OF_FILE);
ansi_close();
if (!flag_stdio)
fclose(fp);
if (verbose)
fprintf(stderr, " %8d blocks\n", tcb.blk_count);
return END_OF_FILE;
}
/* Write a block of data */
write_record(fp)
FILE *fp;
{
char *linebuf;
int rl;
linebuf = alloca(tcb.blk_size); /* goes away on function exit */
if (tcb.rec_format == REC_FIXED) {
rl = fread(linebuf, sizeof(char), tcb.rec_size, fp);
if (rl == 0)
return END_OF_FILE;
} else {
/* format is variable-length records */
if (fgets(linebuf, tcb.rec_size, fp) == NULL)
return END_OF_FILE;
rl = strlen(linebuf);
};
ansi_write(linebuf, rl);
return 0;
}
/*
* Read from a tape. The var main_file_list is a list of files that we
* should extract. We apply filename wildcarding using ? and * to the
* names. (These must be quoted to prevent shell interpretation.) If
* no filenames are supplied, then we extract everything.
*/
read_volume_set()
{
int i;
FLAG extract;
for (; ansi_open(FREAD) != END_OF_SET;) {
extract = main_file_count == 0;
for (i = 0; i < main_file_count && !extract; i++) {
extract = (match(tcb.unix_filename, main_file_list[i]));
};
if (extract) {
if (query) {
fprintf(stderr, "x %s ? ", tcb.unix_filename);
if (!yes_or_no())
continue;
};
if (verbose)
fprintf(stderr, "x %-17s", tcb.unix_filename);
read_file();
};
ansi_close();
scan_labels();
if (extract && verbose)
fprintf(stderr, " %8d blocks\n", tcb.blk_count);
};
}
read_file()
{
int max_reclen,
reclen;
char *record;
FILE *fp;
record = malloc(max_reclen = tcb.rec_size + 20);
/* a little extra room */
if (!flag_stdio)
fp = fopen(tcb.unix_filename, "w");
else
fp = stdout;
while ((reclen = ansi_read(record, max_reclen)) != NULL) {
fwrite(record, sizeof(char), reclen, fp);
};
if (!flag_stdio)
fclose(fp);
}
/***************************************************
* *
* A N S I S U P P O R T R O U T I N E S *
* *
***************************************************/
/* ANSI_OPEN. Open up a tape file, and prepare to read it. */
ansi_open(mode)
int mode;
{
int err;
err = 0;
if (!tcb.mounted) {
if (mode == FREAD)
mount_tape();
else
init_tape();
};
if (mode == FREAD) {
/* We are going to read a tape. */
err = read_labels();
if (err != TOP_OF_FILE)
return err;
scan_labels();
} else {
/* We are writing a tape. */
tcb.reel_switch = FALSE;
tcb.file_count++;
tcb.blk_count = 0;
make_labels(TOP_OF_FILE);
write_labels();
err = TOP_OF_FILE;
};
if (err != UNKNOWN_LABEL) {
tcb.open = TRUE;
tcb.eof = FALSE;
tcb.rw_mode = mode;
tcb.blk_count = 0;
tcb.next = 0;
tcb.char_count = 0;
};
return err;
}
/*
* ANSI_READ. Read in a record from an buffered, open tape file. We
* assume that nothing perverted has happened, like the recordsize
* being longer than the buffersize. We do automatic reel switching if
* needed. Return is number of charcters read, or END_OF_TAPE.
*/
ansi_read(bufp, bufl)
char bufp[];
int bufl;
{
char rl[5];
int read_min,
read_count;
if (tcb.eof || !tcb.open)
return NULL;
read_min = (tcb.rec_format == REC_VARIABLE) ? 4
: tcb.rec_size;
if (tcb.next + read_min > tcb.char_count) {
while (tcb.next < tcb.char_count)
ansi_getc();
};
if (tcb.rec_format == REC_VARIABLE) {
do {
rl[0] = ansi_getc();
} while (!isdigit(rl[0]) && !tcb.eof);
rl[1] = ansi_getc();
rl[2] = ansi_getc();
rl[3] = ansi_getc();
rl[4] = '\0';
read_min = atoi(rl) - 4;
};
if (tcb.eof)
return NULL;
read_count = 0;
for (; bufl && read_count < read_min && !tcb.eof;) {
*(bufp++) = ansi_getc();
bufl--, read_count++;
};
if (bufl && tcb.car_control == CC_IMPLIED) {
*(bufp++) = '\n';
bufl--, read_count++;
};
if (bufl)
*bufp = '\0';
return read_count;
}
/* Read a single character from the tape buffer. */
ansi_getc()
{
int err;
try:
if (tcb.eof)
return END_OF_FILE;
if (tcb.buffer && tcb.open) {
if (tcb.char_count <= tcb.next) {
tcb.next = 0;
tcb.char_count = read_tape(tcb.buffer, tcb.blk_size);
if (tcb.char_count == 0)
goto do_eof;
};
return tcb.buffer[tcb.next++];
};
if (!tcb.buffer) {
tcb.buffer = malloc(tcb.blk_size);
tcb.next = 0;
goto try;
};
do_eof:
tcb.eof = TRUE;
tcb.open = FALSE;
err = read_labels();
if (err == END_OF_TAPE) {
next_volume();
tcb.eof = FALSE;
tcb.open = TRUE;
};
goto try;
}
/*
* ANSI_WRITE. Write a record, given a pointer and a length. We do all
* the formatting necessary. Flush the tape buffer if we run off the
* end, and start a new one.
*/
ansi_write(rec, len)
char rec[];
int len;
{
int overhead = 0;
char scratch[10];
if (!tcb.buffer) {
tcb.buffer = malloc(tcb.blk_size);
tcb.next = 0;
tcb.char_count = tcb.blk_size;
fill(tcb.buffer, tcb.blk_size, '^');
};
if (tcb.car_control == CC_IMPLIED && len > 0 && iscntrl(rec[len - 1]))
rec[--len] = '\0';
/* Check to see if we will run off the end of the block. */
overhead = (tcb.rec_format == REC_VARIABLE) ? 4 : 0;
if (len + overhead + tcb.next > tcb.blk_size) {
/* Write the old tape block. */
write_tape(tcb.buffer, tcb.blk_size);
tcb.blk_count++;
/* Check for physical end of tape. */
if (eot()) {
make_labels(END_OF_TAPE);
write_labels();
next_volume();
};
/* Initialize a new tape block. */
fill(tcb.buffer, tcb.blk_size, '^');
tcb.next = tcb.blk_offset;
};
if (tcb.rec_format == REC_VARIABLE) {
sprintf(scratch, "%04d", len + 4);
strncpy(&tcb.buffer[tcb.next], scratch, 4);
tcb.next += 4;
};
bcopy(rec, &tcb.buffer[tcb.next], len);
tcb.next += len;
return 0;
}
/*
* ANSI_CLOSE. Close out a file. We may not have read all the data in
* the file yet. If that's so, we skip over the intervening data.
* Remember that we may have to switch reels.
*/
ansi_close()
{
int err;
if (!tcb.open)
return 0;
if (tcb.rw_mode == FWRITE) {
if (tcb.next > tcb.blk_offset) {
write_tape(tcb.buffer, tcb.next);
tcb.next = tcb.blk_offset;
tcb.blk_count++;
};
tape(MTWEOF, 1);
make_labels(END_OF_FILE);
write_labels();
goto closed;
};
for (;;) {
if (!tcb.eof)
skip_past_marks(1);
err = read_labels();
if (err == END_OF_FILE)
break;
next_volume();
tcb.eof = 0;
};
scan_labels();
closed:
tcb.eof = tcb.open = FALSE;
if (tcb.buffer)
free(tcb.buffer);
tcb.buffer = 0;
tcb.char_count = tcb.next = 0;
return 0;
}
/*
* Support routines.
*/
next_volume()
{
tape(MTOFFL, 1);
fprintf(stderr, "Mount continuation tape and push return. ");
yes_or_no();
if (func == LIST || func == EXTRACT) {
mount_tape();
read_labels(); /* skip the file continuation HDRn
* records */
} else {
init_tape();
ansi_open(tcb.rw_mode);
};
}
/*
* Write a volume header record. We only write VOL1, since VAX/VMS
* does not use the VOL2 record.
*/
init_tape()
{
char scratch[90];
tcb.ebcdic = ebcdic;
tcb.ibm_format = ibm_format;
tcb.ansi_level = 3;
strncpy(tcb.tapesys, SYSID, 13);
fill(scratch, 90, ' ');
sprintf(scratch, "VOL1%-6.6s%c",
tcb.vol_name, /* volume name */
NOPROTECT /* access protection */
);
if (tcb.ibm_format)
sprintf(&scratch[41], "%-10.10s%29c",
tcb.owner, /* owner of volume set */
IGN /* ignored 3 and ansi_level */
);
else
sprintf(&scratch[37], "%-14.14s%28c%1d",
tcb.owner, /* owner of volume set */
IGN, /* ignored 3 */
tcb.ansi_level /* ansi label standard level */
);
fillnull(scratch, ' ', sizeof(vol1));
upstring(scratch, (char *) &vol1, sizeof(vol1));
write_tape((char *) &vol1, sizeof(vol1));
tcb.reel_count++;
tcb.file_count = 0;
tcb.mounted = TRUE;
if (verbose)
fprintf(stderr, "volume %-6s initialized\n", tcb.vol_name);
}
/* Read a volume header. Save the volume name for later. */
mount_tape()
{
tcb.ebcdic = ebcdic;
tcb.ibm_format = ibm_format;
read_tape((char *) &vol1, sizeof(vol1));
downstring((char *) &vol1, (char *) &vol1, sizeof(vol1));
if (strncmp(vol1.header, "vol1", 4) != 0) {
fprintf(stderr, "no VOL1 header record\n");
exit(-1);
};
tcb.mounted = TRUE;
sscanf((char *) &vol1, "vol1%6c%c",
tcb.vol_name,
&tcb.vol_access
);
if (tcb.ibm_format)
sscanf(&vol1.owner[4], "%10c", tcb.owner);
else
sscanf(vol1.owner, "%14c%*28c%1d",
tcb.owner,
&tcb.ansi_level
);
if (verbose)
fprintf(stderr, "volume %-6s mounted\n", tcb.vol_name);
}
/*
* Extract the information from a set of headers fetched by
* read_labels().
*/
scan_labels()
{
sscanf((char *) &hdr4, "%*3c4%63c", tcb.tape_filename2);
sscanf((char *) &hdr1, "%*3c1%17c%6c%4d%4d%4d%2d",
tcb.tape_filename1,