/*--- EDIT # 0076	1 Dec 1981   11:43:08	VD0:[310,105]GETHDR.C;121  */
/*--- PREVIOUS EDIT	1 Dec 1981   11:42:18	VD0:[310,105]GETHDR.C;120  */
/* routine to read in a file-header. This is done by reading the
index file in block-mode. */
/* called with the directory file open. The index-file is re-opened on
	this fp */

#include <stdio.h>
#include <hdr.h>


unsigned int index_offset = 0;	/* block number of fid=(1,1) */

gethdr (ib, fnum, fseq, dir)
struct hdr *ib;
unsigned int fnum, fseq;
FILE *dir;

{
int n;
char ixf_name [35];
char *d, *f;
long int file_pos;
/* 1st word of home block is index bitmap size */

	if (!index_offset) 
	  {/* get the device name, then the rest of the filename */
		fgetnam (dir, ixf_name);
		d = ixf_name;
		while (*d++ != ':') ;
		f = "[0,0]INDEXF.SYS";
		while (*d++ = *f++);

		if ((freopen(ixf_name, "run", dir)) == NULL) 
			{
			printf("cannot open %s\n",ixf_name);
			return (-1);	/* can't open the index-file */
			}

/* For the INDEX file, kludge up the fdb */
/* The 1st word of the home-block (2nd block of the index-file) tells
the number of blocks in the index bitmap, which is the offset for the
file-headers. i can't quite figure out how to share the definition of
"ib" betewwn the header and an array of words, so I did it this way,
knowing that it will always be a small number. You should be able to
just read 2 bytes into an integer, but virtual disks can't do that */
		dir->io_fdb[2] = dir->io_fdb[4] = 077777;
		fseek (dir,01000l, 0);	/* read the home block */
		n = fget (ib, sizeof *ib, dir);
		index_offset = 1 + ib->h_idof;
		}

	/* read the desired file-header */
	file_pos = 01000l * (index_offset + fnum);
	fseek (dir, file_pos, 0);
	n = fget (ib, sizeof *ib, dir);
	if ((ib->h_fnum == fnum) &&
	    (ib->h_fseq == fseq))	return (0);
printf ("index_offset=%d\n", index_offset);
printf ("req, got: %o,%o   %o,%o\n", fnum, fseq, ib->h_fnum, ib->h_fseq);
	return (-36);
}
