-
Notifications
You must be signed in to change notification settings - Fork 0
/
back10.c
4132 lines (3450 loc) · 82.1 KB
/
back10.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
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
/*
Copyright (c) 2016-2017, Johnny Eriksson
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"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
COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*/
/*
* Things to do:
*
* -- fix code to talk to an actual tape drive directly.
*
* -- implement a way to control overwriting of existing disk files.
*
* -- general clean up.
*/
/*
This is a re-write/merge of backup.c (various versions) and backwr.c
(creates tape images). It is based on the original version(s) of the
code, including random patches that has been floating around the net
in the numerous years.
People involved include:
Johnny Eriksson
Phil Budne
Eric Smith
...
*/
#include <ctype.h>
#include <dirent.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include "back10.h"
/*
* Data types:
*/
typedef unsigned long long w36; /* 36-bit word. */
#define C36(arg) arg##LLU /* 36-bit constant. */
struct i2s { /* int <--> string mapping. */
u_int i;
char* s;
};
typedef struct regitem { /* Make it possible to have several -R args. */
struct regitem* re_next; /* Next in list. */
regex_t re_regex; /* Actual expression. */
} regitem;
enum { /* Disk file format: */
DF_NONE, /* None given so far. */
DF_ASCII, /* Ascii. */
DF_BINARY, /* Binary. */
DF_COREDUMP, /* Core Dump. */
DF_HIGHDENS, /* High density. */
DF_INDUSTRY, /* Industry compatible. */
};
enum { /* What action we are to perform: */
ACT_NONE, /* None given so far. */
ACT_CREATE, /* Create tape (-c). */
ACT_LIST, /* List tape files (-l). */
ACT_PEEK, /* Peek at tape (-p). */
ACT_STRUCT, /* Show tape structure (-s). */
ACT_TYPE, /* Type tape contents (-t). */
ACT_EXTRACT, /* Extract files (-x). */
};
enum { /* Tape format. */
TF_NONE, /* None (placeholder). */
TF_E11, /* .e11 format. */
TF_PHY, /* Physical tape. */
TF_RAW, /* Raw data. */
TF_TAP, /* .tap format. */
TF_TPC, /* .tpc format. */
};
enum { /* Types of "objects" from tape: */
TP_BOT, /* Beginning of tape. */
TP_DATA, /* Data, length kept separate. */
TP_MARK, /* Tape mark. */
TP_EOF, /* Soft EOF (two tape marks). */
TP_GAP, /* Gap (need to find out what it is). */
TP_EOT, /* Physical EOF, i.e. EOT. */
};
enum { /* Type of data: */
DT_NONE, /* Unknown type. */
DT_ANSILBL, /* Ansi label. */
DT_BACKUP, /* Backup-10 record. */
DT_COREDUMP, /* Core dump record. */
DT_DUMPER, /* Dumper record. */
DT_FAILSAFE, /* Failsafe record. */
};
enum { /* Saveset format of data on tape: */
SF_NONE, /* None set. */
SF_BACKUP, /* Backup-10. */
SF_DUMPER, /* Dumper-20. */
SF_FAILSAFE, /* Failsafe. */
};
enum { /* Sub-types of ansi labels: */
AL_NONE, /* Unknown. */
AL_VOL, /* Volume header. */
AL_HDR, /* File header. */
AL_EOV, /* End-of-volume header. */
AL_EOF, /* End-of-file header. */
AL_UVL, /* User volume header. */
AL_UHL, /* User file header. */
AL_UTL, /* User file trailer. */
};
/* Types of DUMPER records: */
#define D_DATA 0 /* Data, file page. */
#define D_TPHD 1 /* Saveset header. */
#define D_FLHD 2 /* File header. */
#define D_FLTR 3 /* File trailer. */
#define D_TPTR 4 /* Tape trailer. */
#define D_USR 5 /* User directory info. */
#define D_CTPH 6 /* Continued saveset header. */
#define D_FILL 7 /* Filler record. */
/* Types of FAILSAFE records: (internal codes) */
enum {
F_BEGIN, /* Begin saveset. */
F_END, /* End saveset. */
F_FILE, /* Begin file. */
F_DATA, /* File data. */
};
/*
* variables:
*/
char* programname; /* What we were called as. */
int tapeaction = ACT_NONE; /* No action given yet. */
int tapeformat = TF_NONE; /* No tape format set. */
int tapeobject = TP_BOT; /* Initially at start of tape. */
int prevobject = TP_BOT; /* Previous tape object. */
int taperecnum = 0; /* Tape record number. */
int subrecnum; /* Number of current sub-record. */
int tapereclen; /* For TP_DATA records, actual length. */
int taperectype = DT_NONE; /* For TP_DATA records, data type. */
int datarectype; /* Subtype of data record, depends on type. */
int ssformat = SF_NONE; /* Saveset format. */
int argcount; /* Count of arguments to worker. */
char** arglist; /* List of arguments to worker. */
regitem* reghead = NULL; /* Head of regex list. */
regitem* regtail = NULL; /* Tail of regex list. */
int extracting = 0; /* Non-zero if we are extracting this file. */
int insaveset = 0; /* Non zero if we are inside a saveset. */
int infile = 0; /* Non-zero if we are inside a file. */
int buildtree = 0; /* (-b) Build file tree. */
int nodevice = 0; /* (-d) Omit device from file tree. */
int interchange = 0; /* (-i) Interchange flag. */
int quiet = 0; /* (-q) Quiet flag. */
int verbose = 0; /* (-v) Verbose level. */
int debug = 0; /* (-D) debugging flag. */
char* tapename = NULL; /* (-f) tape file name. */
/*
* Parameters we use when writing tapes:
*/
int apr = 4097; /* (xx) Processor serial number. */
char* bpi = "1600"; /* (xx) Density (BPI) of tape. */
char* dev = "MTA0"; /* (xx) Device name. */
char* reel = ""; /* (xx) Reel name. */
char* sysname = NULL; /* (-M) System name. */
char* ssname = NULL; /* (-S) SaveSet name. */
/*
* p_word control bits etc:
*/
u_int pw_number = 0; /* (-n) Number of words to print. */
int pw_raw = 0; /* Use raw buffer. */
int pw_hex = 0; /* Hex. */
int pw_hword = 1; /* Halfwords. */
int pw_7bit = 1; /* 7-bit bytes. */
int pw_text = 1; /* Text. */
int pw_sixbit = 1; /* Sixbit. */
int pw_instr = 0; /* Instructions. */
FILE* tapefile; /* Tape file we operate on. */
FILE* diskfile; /* Disk file we operate on. */
off_t filelength; /* Length, in octets. */
time_t filetime; /* Time last modified. */
mode_t filemode; /* Mode of file (protection). */
u_char diskbuffer[5*512]; /* Disk file buffer. */
u_int diskcount; /* Amount of data in above. */
u_int dataoffset; /* Data offset. */
int diskformat = DF_NONE; /* Disk file format. */
int bpw = 40; /* Bits/word in current format. */
int opw = 5; /* Octets/word in current format. */
u_int seq; /* Sequence number. */
int gmtoffset; /* GMT offset. */
#define DEFBLKF 4 /* Default blocking factor. */
#define MAXBLKF 96 /* Max blocking factor we handle. */
#define MAXDATA (128 * MAXBLKF) /* Max data (words) we handle. */
#define TPBUFSZ (32 + MAXDATA) /* Total tape buffer size. */
#define RAWBUFSZ (5 * TPBUFSZ) /* Total raw buffer size. */
w36 tapebuffer[TPBUFSZ]; /* Tape buffer. */
w36* bckhdr = &tapebuffer[0]; /* Start of header (backup fmt). */
w36* bckdata = &tapebuffer[32]; /* Start of data area (backup fmt). */
u_char rawbuffer[RAWBUFSZ]; /* Raw tape data. */
w36 namechecksum; /* For each file, csm of the O$NAME block. */
int dlevel = 0; /* Directory level. */
#define STRSIZE 256 /* Size of strings we collect from tape. */
char t_sysname[STRSIZE]; /* System name. */
char t_ssname[STRSIZE]; /* SaveSet name. */
/* File name elements: */
char f_device[STRSIZE]; /* Device. */
char f_ufd [STRSIZE]; /* Directory. */
char f_name [STRSIZE]; /* File name. */
char f_ext [STRSIZE]; /* Extension. */
char f_sfd[6][STRSIZE]; /* SFDs. (0 unused) */
/* File name variants: */
char iname [STRSIZE]; /* Interchange name. */
char cname [STRSIZE]; /* Canonical name. */
char oname [STRSIZE]; /* Output name. */
char tname [STRSIZE]; /* Tree name. */
w36 f_attrs [LN_AFH]; /* File attributes. */
char a_cusr [STRSIZE]; /* Creator. */
w36 f_rdtime; /* File read (access) time. */
w36 f_wrtime; /* File write time. */
struct i2s name_density[] = {
{ 1, "200" },
{ 2, "556" },
{ 3, "800" },
{ 4, "1600" },
{ 5, "6250" },
{ 0, NULL },
};
struct i2s name_ostype[] = {
{ 1, "TOPS-10" },
{ 2, "TENEX" },
{ 3, "ITS" },
{ 4, "TOPS-20" },
{ 5, "TYMCOM-X" },
{ 0, NULL },
};
struct i2s name_rectype[] = {
{ T_LABEL, "label" },
{ T_BEGIN, "begin" },
{ T_END, "end" },
{ T_FILE, "file" },
{ T_UFD, "ufd" },
{ T_EOV, "eov" },
{ T_COMM, "comment" },
{ T_CONT, "continuation" },
{ 0, NULL },
};
struct i2s name_sfformat[] = {
{ SF_BACKUP, "backup" },
{ SF_DUMPER, "dumper" },
{ SF_FAILSAFE, "failsafe" },
{ 0, NULL },
};
struct i2s name_tpformat[] = {
{ TF_E11, "e11" },
{ TF_RAW, "raw" },
{ TF_TAP, "tap" },
{ TF_TPC, "tpc" },
{ TF_TAP, "simh" },
{ 0, NULL },
};
struct i2s name_lbltype[] = {
{ AL_VOL, "VOL" },
{ AL_HDR, "HDR" },
{ AL_EOV, "EOV" },
{ AL_EOF, "EOF" },
{ AL_UVL, "UVL" },
{ AL_UHL, "UHL" },
{ AL_UTL, "UTL" },
{ 0, NULL },
};
struct i2s name_dumper_rectype[] = {
{ D_DATA, "file data" },
{ D_TPHD, "saveset" },
{ D_FLHD, "file header" },
{ D_FLTR, "file trailer" },
{ D_TPTR, "tape trailer" },
{ D_USR, "user directory" },
{ D_CTPH, "cont. saveset" },
{ D_FILL, "fill" },
{ 0, NULL },
};
struct i2s name_failsafe_rectype[] = {
{ F_BEGIN, "begin" },
{ F_END, "end" },
{ F_FILE, "file" },
{ F_DATA, "data" },
{ 0, NULL },
};
char* opcode[0700] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0... */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 20... */
0, "init", 0, 0, /* 40... */
0, 0, 0, "calli",
"open", "ttcall", 0, 0, /* 50... */
0, "rename", "in", "out",
"setsts", "stato", "getsts", "statz", /* 60... */
"inbuf", "outbuf", "input", "output",
"close", "releas", "mtape", "ugetf", /* 70... */
"useti", "useto", "lookup", "enter",
"ujen", 0, "gfad", "gfsb", /* 100... */
"jsys", "adjsp", "gfmp", "gfdv",
"dfad", "dfsb", "dfmp", "dfdv", /* 110... */
"dadd", "dsub", "dmul", "ddiv",
"dmove", "dmovn", "fix", "extend", /* 120... */
"dmovem", "dmovnm", "fixr", "fltr",
"ufa", "dfn", "fsc", "ibp", /* 130... */
"ildb", "ldb", "idbp", "dpb",
"fad", "fadl", "fadm", "fadb", /* 140... */
"fadr", "fadri", "fadrm", "fadrb",
"fsb", "fsbl", "fsbm", "fsbb", /* 150... */
"fsbr", "fsbri", "fsbrm", "fsbrb",
"fmp", "fmpl", "fmpm", "fmpb", /* 160... */
"fmpr", "fmpri", "fmprm", "fmprb",
"fdv", "fdvl", "fdvm", "fdvb", /* 170... */
"fdvr", "fdvri", "fdvrm", "fdvrb",
"move", "movei", "movem", "moves", /* 200... */
"movs", "movsi", "movsm", "movss", /* 204... */
"movn", "movni", "movnm", "movns", /* 210... */
"movm", "movmi", "movmm", "movms", /* 214... */
"imul", "imuli", "imulm", "imulb", /* 220... */
"mul", "muli", "mulm", "mulb", /* 224... */
"idiv", "idivi", "idivm", "idivb", /* 230... */
"div", "divi", "divm", "divb", /* 234... */
"ash", "rot", "lsh", "jffo", /* 240... */
"ashc", "rotc", "lshc", 0, /* 244... */
"exch", "blt", "aobjp", "aobjn", /* 250... */
"jrst", "jfcl", "xct", "map", /* 254... */
"pushj", "push", "pop", "popj", /* 260... */
"jsr", "jsp", "jsa", "jra", /* 264... */
"add", "addi", "addm", "addb", /* 270... */
"sub", "subi", "subm", "subb", /* 274... */
"cai", "cail", "caie", "caile", /* 300... */
"caia", "caige", "cain", "caig", /* 304... */
"cam", "caml", "came", "camle", /* 310... */
"cama", "camge", "camn", "camg", /* 314... */
"jump", "jumpl", "jumpe", "jumple", /* 320... */
"jumpa", "jumpge", "jumpn", "jumpg", /* 324... */
"skip", "skipl", "skipe", "skiple", /* 330... */
"skipa", "skipge", "skipn", "skipg", /* 334... */
"aoj", "aojl", "aoje", "aojle", /* 340... */
"aoja", "aojge", "aojn", "aojg", /* 344... */
"aos", "aosl", "aose", "aosle", /* 350... */
"aosa", "aosge", "aosn", "aosg", /* 354... */
"soj", "sojl", "soje", "sojle", /* 360... */
"soja", "sojge", "sojn", "sojg", /* 364... */
"sos", "sosl", "sose", "sosle", /* 370... */
"sosa", "sosge", "sosn", "sosg", /* 374... */
"setz", "setzi", "setzm", "setzb", /* 400... */
"and", "andi", "andm", "andb", /* 404... */
"andca", "andcai", "andcam", "andcab", /* 410... */
"setm", "setmi", "setmm", "setmb", /* 414... */
"andcm", "andcmi", "andcmm", "andcmb", /* 420... */
"seta", "setai", "setam", "setab", /* 424... */
"xor", "xori", "xorm", "xorb", /* 430... */
"ior", "iori", "iorm", "iorb", /* 434... */
"andcb", "andcbi", "andcbm", "andcbb", /* 440... */
"eqv", "eqvi", "eqvm", "eqvb", /* 444... */
"setca", "setcai", "setcam", "setcab", /* 450... */
"orca", "orcai", "orcam", "orcab", /* 454... */
"setcm", "setcmi", "setcmm", "setcmb", /* 460... */
"orcm", "orcmi", "orcmm", "orcmb", /* 464... */
"orcb", "orcbi", "orcbm", "orcbb", /* 470... */
"seto", "setoi", "setom", "setob", /* 474... */
"hll", "hlli", "hllm", "hlls", /* 500... */
"hrl", "hrli", "hrlm", "hrls", /* 504... */
"hllz", "hllzi", "hllzm", "hllzs", /* 510... */
"hrlz", "hrlzi", "hrlzm", "hrlzs", /* 514... */
"hllo", "hlloi", "hllom", "hllos", /* 520... */
"hrlo", "hrloi", "hrlom", "hrlos", /* 524... */
"hlle", "hllei", "hllem", "hlles", /* 530... */
"hrle", "hrlei", "hrlem", "hrles", /* 534... */
"hrr", "hrri", "hrrm", "hrrs", /* 540... */
"hlr", "hlri", "hlrm", "hlrs", /* 544... */
"hrrz", "hrrzi", "hrrzm", "hrrzs", /* 550... */
"hlrz", "hlrzi", "hlrzm", "hlrzs", /* 554... */
"hrro", "hrroi", "hrrom", "hrros", /* 560... */
"hlro", "hlroi", "hlrom", "hlros", /* 564... */
"hrre", "hrrei", "hrrem", "hrres", /* 570... */
"hlre", "hlrei", "hlrem", "hlres", /* 574... */
"trn", "tln", "trne", "tlne", /* 600... */
"trna", "tlna", "trnn", "tlnn", /* 604... */
"tdn", "tsn", "tdne", "tsne", /* 610... */
"tdna", "tsna", "tdnn", "tsnn", /* 614... */
"trz", "tlz", "trze", "tlze", /* 620... */
"trza", "tlza", "trzn", "tlzn", /* 624... */
"tdz", "tsz", "tdze", "tsze", /* 630... */
"tdza", "tsza", "tdzn", "tszn", /* 634... */
"trc", "tlc", "trce", "tlce", /* 640... */
"trca", "tlca", "trcn", "tlcn", /* 644... */
"tdc", "tsc", "tdce", "tsce", /* 650... */
"tdca", "tsca", "tdcn", "tscn", /* 654... */
"tro", "tlo", "troe", "tloe", /* 660... */
"troa", "tloa", "tron", "tlon", /* 664... */
"tdo", "tso", "tdoe", "tsoe", /* 670... */
"tdoa", "tsoa", "tdon", "tson", /* 674... */
};
/***********************************************************************/
/* General helper routines: */
/*
* Map an integer to a string:
*/
char* map_i2s(u_int i, struct i2s* x, char* dflt)
{
while (x->s != NULL) {
if (x->i == i)
return x->s;
x++;
}
return dflt;
}
/*
* Map a string to an integer:
*/
u_int map_s2i(char* s, struct i2s* x, u_int dflt)
{
while (x->s != NULL) {
if (strcmp(s, x->s) == 0)
return x->i;
x++;
}
return dflt;
}
/*
* Routines to manipulate/create 36-bit words:
*/
w36 xwd(u_int l, u_int r)
{
w36 w;
w = l;
w <<= 18;
w += r;
return w;
}
void dpb(u_int fld, u_char size, w36* w, u_char pos)
{
w36 mask;
mask = ((1 << size) - 1) << (36 - pos);
*w &= ~mask;
*w |= mask & (fld << (36 - pos));
}
u_int ldb(u_char size, w36 w, u_char pos)
{
w >>= (35 - pos);
w &= ((1 << size) - 1);
return w;
}
u_int lhalf(w36 w)
{
return w >> 18;
}
u_int rhalf(w36 w)
{
return w & 0777777;
}
/*
* Turn an ascii string into a 36-bit word, like the ASCII pseudo-op.
*/
w36 ascii(char name[])
{
int i;
char c;
w36 w;
w = 0;
c = '*';
for (i = 0; i < 5; i += 1) {
if (c != (char) 0) {
c = name[i];
}
w <<= 7;
w += c;
}
w <<= 1;
return w;
}
/*
* Turn an ascii string into a 36-bit word, like the SIXBIT pseudo-op.
*/
w36 sixbit(char name[])
{
int i;
char c;
w36 w;
w = 0;
c = '*';
for (i = 0; i < 6; i += 1) {
if (c != (char) 0) {
c = name[i];
}
if (c != (char) 0) {
if ((c >= 'a') && (c <= 'z')) {
c = c - 'a' + 'A';
}
c -= 32;
}
w <<= 6;
w += c;
}
return w;
}
/*
* time2udt() converts a unix time_t to a 36-bit tops UDT.
*/
w36 time2udt(time_t t)
{
int days, seconds;
t += gmtoffset;
days = t / 86400;
seconds = t - days * 86400;
days += 0117213;
seconds *= 2048;
seconds /= 675;
return xwd(days, seconds);
}
/*
* udt2time() converts a 36-bit tops UDT to a time_t. If the UDT
* is zero (i.e. missing), return zero. If the UDT is before 1970,
* return one. This will result in the earliest date we know.
*
* We should possibly worry about dates that overflow a time_t...
*/
time_t udt2time(w36 udt)
{
int days, seconds;
if (udt == 0)
return 0;
if (udt < xwd(1,0))
return 1;
udt -= (gmtoffset << 11) / 675;
days = lhalf(udt) - 0117213;
if (days < 0)
return 1;
seconds = (rhalf(udt) * 675) >> 11;
return days * 86400 + seconds;
}
/*
* Get current time in UDT format.
*/
w36 getnow(void)
{
return time2udt(time(NULL));
}
/*
* Replace all underscore characters in string with comma.
*/
void ununderscore(char* str)
{
for (;;) {
str = strchr(str, '_');
if (str == NULL)
return;
*str = ',';
}
}
/*
* Convert string to all lowercase.
*/
void downcase(char* str)
{
for (; *str != 0; str++) {
if (isupper(*str))
*str = tolower(*str);
}
}
/*
* Convert string to all uppercase.
*/
void upcase(char* str)
{
for (; *str != 0; str++) {
if (islower(*str))
*str = toupper(*str);
}
}
/*
* Put a sixbit word as ASCII into a string.
*/
void six2asc(char* str, w36 w, int len)
{
int i;
char c;
for (i = 0; i < len; i += 1) {
c = ldb(6, w, 5);
if (c == 0) {
*str = 0;
return;
}
*str++ = c + 32;
w <<= 6;
}
}
/*
* Construct our full file names.
*/
void buildnames(void)
{
int i, pos;
cname[0] = 0;
iname[0] = 0;
if (f_name[0] == 0) /* Got a file name at all? */
return; /* No, can't do anything. */
/* Always include the dot in the tops-style file name. */
snprintf(iname, STRSIZE, "%s.%s", f_name, f_ext);
pos = 0;
if (f_device[0] != 0)
pos += snprintf(&cname[pos], (STRSIZE - pos), "%s:", f_device);
if (f_ufd[0] != 0) {
pos += snprintf(&cname[pos], (STRSIZE - pos), "[%s", f_ufd);
for (i = 1; i <= 5; i += 1) {
if (f_sfd[i][0] != 0)
pos += snprintf(&cname[pos], (STRSIZE - pos), ",%s", f_sfd[i]);
else
i = 5;
}
pos += snprintf(&cname[pos], (STRSIZE - pos), "]");
}
pos += snprintf(&cname[pos], (STRSIZE - pos), "%s", iname);
}
/*
* Check if the current file matches any argument.
*/
int checkarg(void)
{
regitem* reg;
int i;
if (argcount == 0 && reghead == NULL)
return 1;
for (i = 0; i < argcount; i += 1) {
if (strstr(cname, arglist[i]) != NULL)
return 1;
}
for (reg = reghead; reg != NULL; reg = reg->re_next) {
if (regexec(®->re_regex, cname, 0, NULL, 0) == 0)
return 1;
}
return 0;
}
/*
* Try to interpret an A$PROT sub-field. Returns printable character.
*
* Code from Phil Budne, modified. (The code, that is).
*/
char prot(u_char fld)
{
if (fld & 0x80) /* Special? */
return '!';
switch (fld) { /* Try canned values. */
case 0x6e:
return '0'; /* change protection */
case 0x7e:
return '1'; /* rename */
case 0x2e:
return '2'; /* write */
case 0x2a:
return '3'; /* update */
case 0x26:
return '4'; /* append */
case 0x22:
return '5'; /* read */
case 0x11:
return '6'; /* execute */
case 0x10:
return '7'; /* no access */
case 0:
return '*'; /* for interchange?? */
}
/* Try field-by-field: */
switch (fld & 0x03) {
case 0:
return '7';
case 1:
return '6';
}
switch ((fld >> 2) & 0x03) {
case 0:
return '5';
case 1:
return '4';
case 2:
return '3';
}
switch ((fld >> 4) & 0x07) {
case 2:
return '2';
case 6:
return '1';
case 7:
return '0';
}
return '?';
}
/*
* Try to map a file name to a file format, i.e. "foo.tap" maps
* to TF_TAP, and so on. Return format, or TF_NONE if we fail.
*/
u_int map_ext2tf(char* arg)
{
u_int fmt;
arg = strrchr(arg, '.');
if (arg == NULL)
return TF_NONE;
arg = strdup(++arg);
if (arg == NULL)
return TF_NONE;
downcase(arg);
fmt = map_s2i(arg, name_tpformat, TF_NONE);
free(arg);
return fmt;
}
/***********************************************************************/
/*
* Printing and formatting routines:
*/
/*
* Print a tops-10 UDT in human-readable format.
*
* The following code (mis)uses, amongst other things, the fact that
* the year 1858 (base year of UDT) aligns with 1970 (base of time_t)
* with regard to leap years. We do some heavy lifting first, then
* let gmtime() handle the gruesome fine details.
*
* The leap year thing above is almost correct. The year 1900 was
* NOT a leap year, but we adjust for that and hope that no one has
* created a tops-10 file before that. The year 2100 will also not
* be a leap year, the code will need a bug fix by then.
*/
void p_udt(w36 udt)
{
u_int days, frac;
struct tm* tm;
time_t part;
static char* month[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
};
days = lhalf(udt);
frac = rhalf(udt);
/* Adjust to beginning of 1858, and make 1900 a leap year: */
days += 320 + 1;
/*
* Below, 1461 is 4*365 + 1, i.e. four years and one leap day.
*
* The fractional part is: fraction * 86400 / 2^18, modified to
* stay below 2^32 during calculation.
*/
part = ((days % 1461) * 86400) + ((frac * 675) >> 11);
/* Now part is in the range 1970..1973, inclusive. */
tm = gmtime(&part);
/* 1788 = 1900 - 1970 + 1858 */
printf("%2d-%s-%4d %02d:%02d:%02d",
tm->tm_mday, month[tm->tm_mon],
tm->tm_year + 1788 + ((days / 1461) << 2),
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
/*
* Print a word formatted as a version number.
*/
void p_vers(w36 w)
{
u_int who, ver, min, edt;
who = ldb(3, w, 2);
ver = ldb(9, w, 11);
min = ldb(6, w, 17);
edt = rhalf(w);
printf("%o", ver);
if (min != 0) {
if (min > 26) {
putchar('A' + (min/26) - 1);
min %= 26;
}
putchar('A' + min - 1);
}
printf("(%o)", edt);
if (who != 0) {
printf("-%o", who);
}
}
/*
* Print a char as itself (if printable) or as a space.
*/
void p_pchar(char c)
{
if ((c < ' ') || (c == 0177))
c = ' ';
putchar(c);
}
/*
* Print a newline.
*/
void p_nl(void)
{
printf("\n");
}
/*
* Print a word as SIXBIT.
*/
void p_sixbit(w36 word)
{
char ch;