# 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(Padding(14),
              'cluster'/Int16un,
              Padding(16),
              'ibmapsize'/Int16un,
              Padding(478))

filehdr = Struct(Padding(6),
                 'strucLevMinor'/Int8un,
                 'strucLevMajor'/Int8un,
                 Padding(72))

identarea2 = Struct('fileName'/Bytes(20),
                    Padding(34),
                    'fileNameExt'/Bytes(66))

identarea5 = Struct(Padding(1),
                    'nameLen'/Int8un,
                    Padding(74),
                    'fileName'/Bytes(44),
                    'fileNameExt'/Bytes(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).decode('iso-8859-1').rstrip()
        print(fnm)
    if fhdr.strucLevMajor == 5:
        ia5 = identarea5.parse(frec[80:404])
        fnm = (ia5.fileName + ia5.fileNameExt).decode('iso-8859-1')[:ia5.nameLen]
        print(fnm)
    n = n + 1
print('%d files' % (n))
f.close()
