# Collection of code snippets by Arne Vajhøj 
# (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
from construct import *

home = Struct('home', Bytes('other1', 14),
                      SNInt16('cluster'),
                      Bytes('other2', 16),
                      SNInt16('ibmapsize'),
                      Bytes('other3', 478))

filehdr = Struct('filehdr', Bytes('other1', 6),
                            SNInt8('strucLevMinor'),
                            SNInt8('strucLevMajor'),
                            Bytes('other2', 72))

identarea2 = Struct('identarea2', String('fileName', 20),
                                  Bytes('other', 34),
                                  String('fileNameExt', 66))

identarea5 = Struct('identarea5', SNInt8('other1'),
                                  SNInt8('nameLen'),
                                  Bytes('other2', 74),
                                  String('fileName', 44),
                                  String('fileNameExt', 204))

f = open('/disk2/000000/indexf.sys', 'rb')
bootrec = f.read(512)
homerec = f.read(512)
homeblk = home.parse(homerec)
startfiles = 4 * homeblk.cluster + homeblk.ibmapsize
for i in range(startfiles - 2):
    skiprec = f.read(512)
n = 0
while True:
    frec = f.read(512)
    if not frec:
        break
    fhdr = filehdr.parse(frec[0:80])
    if fhdr.strucLevMajor == 2:
        ia2 = identarea2.parse(frec[80:200])
        fnm = (ia2.fileName + ia2.fileNameExt).rstrip()
        print(fnm)
    if fhdr.strucLevMajor == 5:
        ia5 = identarea5.parse(frec[80:404])
        fnm = (ia5.fileName + ia5.fileNameExt)[:ia5.nameLen]
        print(fnm)
    n = n + 1
print('%d files' % (n))
f.close()
