	.Title	HEADER
;	Program Header
;	Written by:	Frank Penner
;			September 1988
;	this program will count how many headers are available
;	to use, type:
;		HEADER DRA0:
;	where DRA0: is the device
	$HM2DEF				;define home block offsets
	$FH2DEF
	$FATDEF
	.PSECT	DATA,WRT,NOEXE
INFAB:	$FAB	FAC=<BIO,get>,-		;FAB WITH BLOCK I/O
		DNM=<[000000]INDEXF.SYS>,-	;FILENAME
		FNA=COMMLD+8		;device name
INRAB:	$RAB	BKT=2,-			;RAB ,READ/WRITE BUCKET ONE
		FAB=INFAB,-		;FAB FOR THIS RAB
		ROP=BIO,-		;BLOCK I/O
		UBF=BUFFER,-		;ADDRESS OF BUFFER FOR I/O
		USZ=1*512		;SIZE OF BUFFER
BUFFER:	.BLKB	70*512			;BUFFER FOR DATA
COMMLD:	.ASCID	/                                             /	;temporary 
					;device name for chan assign
PROMPTD:.ASCID	/device name:/		;prompt string
FAODESC:.ASCID	/                                                        /-
		/                                                        /
FAODESC1:.ASCID	/            /
FAODESC2:.ASCID	/            /
FAODESC3:.ASCID	/            /
FAODESC4:.ASCID	/            /
FORSTR:	.ASCID	/!10AS !6UL free headers !3UL map words /-
		/!6UL blocks !6UL RMS blocks/
FORSTR_SYM:
	.ASCID	/!6UL/
SYMBOL1:.ASCID	/FREE_HEADERS/
SYMBOL2:.ASCID	/MAP_WORDS/
SYMBOL3:.ASCID	/BLOCKS/
SYMBOL4:.ASCID	/RMS_BLOCKS/
	.PSECT	CODE,NOWRT,EXE
ERROR1:	BRW	ERROR
	.ENTRY	START,^M<>		;start of program
	PUSHAW	COMMLD			;address of word to get read byte count
	PUSHAL	PROMPTD			;address of prompt string descriptor
	PUSHAL	COMMLD			;address of descriptor to get command
	CALLS	#3,G^LIB$GET_FOREIGN	;use run time library to get command
	BLBC	R0,ERROR1		;low bit clear error
	$fab_store fab=infab,-		;pick up length of typed device name
		fns=commld		;and put it in FAB
	$OPEN	FAB=INFAB		;OPEN FILE
	BLBC	R0,ERROR1
	$CONNECT RAB=INRAB		;CONNECT FILE
	BLBC	R0,ERROR1
	$READ	RAB=INRAB		;READ BLOCK 2, home block
	BLBC	R0,ERROR1
	MOVZWL	BUFFER+HM2$W_IBMAPVBN,R11 ;VBN of header bitmap
	MOVZWL	BUFFER+HM2$W_IBMAPSIZE,R8 ;# of blocks in bitmap
	ADDL2	R11,R8			;block number where headers start
	$rab_store rab=inrab -		;read first header, file id 1,1
		bkt=R8 -
		usz=#200
	$READ	RAB=INRAB		;READ
	BLBS	R0,1$
	BRW	ERROR
1$:	MOVZBL	BUFFER+FH2$B_MAP_INUSE,R3 ;get map words in use
	MOVL	INFAB+FAB$L_ALQ,R5	;save RMS size of INDEXF.SYS file
	MOVZWL	BUFFER+FH2$W_RECATTR+FAT$W_HIBLKH ,R4 ;get # of blocks alloc hi
	ASHL	#16,R4,R4		;shift to high word
	MOVW	BUFFER+FH2$W_RECATTR+FAT$W_HIBLKL ,R4 ;get # of blocks alloc low
	SUBL3	R8,R4,R10		;subtract # of bitmap blocks from size
	ASHL	#-3,R10,R9		;divide by 8 to get bytes in bitmap
	BICL2	#^X1FF,R9		;round up to block boundary
	ADDL2	#^X200,R9		;
	$rab_store rab=inrab -		;read all the bits in the bitmap
		bkt=R11 -
		usz=R9
	$READ	RAB=INRAB		;READ
	BLBS	R0,2$
	BRW	ERROR
2$:	CLRL	R0			;current find bit position
	MOVAL	BUFFER,R1		;put address of bitmap in R1
	CLRL	R2			;use R2 as count of found clear bits
10$:	FFC	R0,#32,BUFFER,R0	;any clear bits?
	BEQL	10$			;Z set means no clear bits found
	INCL	R2			;found a clear bit, inc counter
	INCL	R0			;point to next bit
	CMPL	R10,R0			;have we gone far enough?
	BGTR	10$			;no so loop

	$FAO_S	CTRSTR=FORSTR_SYM,-		;format string 
		OUTBUF=FAODESC1,-	;char descript for formatted output
		OUTLEN=FAODESC1,-	;long word to hold length of output
		P1=R2			;free header count
	BLBS	R0,20$			;low bit clear error
	BRW	ERROR
20$:	PUSHAL	FAODESC1
	PUSHAL	SYMBOL1
	CALLS	#2,G^LIB$SET_SYMBOL
	BLBS	R0,21$			;low bit clear error
	BRW	ERROR
21$:	$FAO_S	CTRSTR=FORSTR_SYM,-		;format string 
		OUTBUF=FAODESC2,-	;char descript for formatted output
		OUTLEN=FAODESC2,-	;long word to hold length of output
		P1=R3			;map words in use
	BLBS	R0,22$			;low bit clear error
	BRW	ERROR
22$:	PUSHAL	FAODESC2
	PUSHAL	SYMBOL2
	CALLS	#2,G^LIB$SET_SYMBOL
	BLBS	R0,23$			;low bit clear error
	BRW	ERROR
23$:	$FAO_S	CTRSTR=FORSTR_SYM,-		;format string 
		OUTBUF=FAODESC3,-	;char descript for formatted output
		OUTLEN=FAODESC3,-	;long word to hold length of output
		P1=R4			;number of blocks allocated from header
	BLBS	R0,24$			;low bit clear error
	BRW	ERROR
24$:	PUSHAL	FAODESC3
	PUSHAL	SYMBOL3
	CALLS	#2,G^LIB$SET_SYMBOL
	BLBC	R0,ERROR		;low bit clear error
	$FAO_S	CTRSTR=FORSTR_SYM,-		;format string 
		OUTBUF=FAODESC4,-	;char descript for formatted output
		OUTLEN=FAODESC4,-	;long word to hold length of output
		P1=R5			;number of blocks allocated from RMS
	BLBC	R0,ERROR		;low bit clear error
	PUSHAL	FAODESC4
	PUSHAL	SYMBOL4
	CALLS	#2,G^LIB$SET_SYMBOL
	BLBC	R0,ERROR		;low bit clear error

	$FAO_S	CTRSTR=FORSTR,-		;format string 
		OUTBUF=FAODESC,-	;char descript for formatted output
		OUTLEN=FAODESC,-	;long word to hold length of output
		P1=#COMMLD,-		;typed device name
		P2=R2,-			;free header count
		P3=R3,-			;map words in use
		P4=R4,-			;number of blocks allocated from header
		P5=R5			;number of blocks allocated from RMS
	BLBC	R0,ERROR		;low bit clear error
	PUSHAL	FAODESC
	CALLS	#1,G^LIB$PUT_OUTPUT
	BLBC	R0,ERROR		;low bit clear error
	$CLOSE	FAB=INFAB		;CLOSE FILE
	BLBC	R0,ERROR
ERROR:	
	$EXIT_S	R0			;EXIT WITH ERROR IF ANY
	.END	START
