# 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 struct import *
from collections import namedtuple

homefmt = '14sh16sh478s'
homeclz = namedtuple('home', 'other1,cluster,other2,ibmapsize,other3')
filehdrfmt = '6sbb72s'
filehdrclz = namedtuple('filehdr', 'other1,strucLevMinor,strucLevMajor,other2')
identarea2fmt = '20s34s66s'
identarea2clz = namedtuple('identarea2', 'fileName,other,fileNameExt')
identarea5fmt = 'bb74s44s204s'
identarea5clz = namedtuple('identarea5', 'other1,nameLen,other2,fileName,fileNameExt')

f = open('/disk2/000000/indexf.sys', 'rb')
bootrec = f.read(512)
homerec = f.read(512)
homeblk = homeclz._make(unpack(homefmt, 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 = filehdrclz._make(unpack(filehdrfmt, frec[0:80]))
    if fhdr.strucLevMajor == 2:
        ia2 = identarea2clz._make(unpack(identarea2fmt, frec[80:200]))
        fnm = (ia2.fileName + ia2.fileNameExt).decode('iso-8859-1').rstrip()
        print(fnm)
    if fhdr.strucLevMajor == 5:
        ia5 = identarea5clz._make(unpack(identarea5fmt, 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()
