/* fid.c - given device, filenumber - report file name etc */

#include <stdio.h>
#include <cx.h>
#include <qioret.h>
#include <f11.h>
#include <nboff.h>
#include <fdoff.h>
#include <algol68.h>

extern int $$ferr;		/* the fcb io error status is here	*/
struct	home	homblk;		/* home block				*/
struct	header	hedblk;		/* file header				*/
char	indxnm[100];		/* index file print name		*/

	FILE *	indexf;		/* [0,0]indexf.sys;1			*/
	unsigned	filnum;	/* file number				*/
	unsigned	filoff;	/* (VBN of header (1,1) in indexf.sys)-1*/
	unsigned	maxfil;	/* maximum file number (permitted by	*/
				/* bitmap of index file)		*/
	FDB *	fdbp;		/* point to fdb of indexf for fcs rape	*/

char	nam[10];		/* file name				*/
char	ext[4];			/* file extension			*/

main(argc,argv)
int	argc;
char ** argv;
BEGIN
	IF	(argc<3)
	THEN	printf("usage: %s device_name: octal_file_number",argv[0]);
		exit();
	FI;
	strcpy(indxnm,argv[1]);
	strcat(indxnm,"[0,0]indexf.sys");
	IF	(! (indexf=fopenr(indxnm,512)) )
	THEN	error("$$ferr=%oo fopenr: %s",$$ferr,indxnm);
	FI;
	/* now fake end-of-file to fcs: so we can read home block */
	fdbp = indexf -> io_fdb;
	fdbp -> f_fatt . f_hibk = fdbp -> f_fatt . f_efbk = 42;
	IF	(frget(&homblk,512,indexf,2L))
	THEN	error("$$ferr=%oo home",$$ferr);
	FI;
	filoff = 2 + homblk . h_ibsz;
	maxfil = homblk . h_fmax;
	fdbp -> f_fatt . f_hibk = fdbp -> f_fatt . f_efbk = maxfil + 1;
	sscanf(argv[2],"%o",&filnum);
	IF	(frget(&hedblk,512,indexf,(long)(filnum+filoff)))
	THEN	error("$$ferr=%oo header read",$$ferr);
	ELSE	r50toa(nam,&hedblk.i_fnam,3);
		nam[9] = EOS;
		r50toa(ext,&hedblk.i_ftyp,1);
		ext[3] = EOS;
		printf("[%o,%o]%s.%s;%o\\%d. (%o:%o)\n"
			,hedblk.h_proj&0xFF,hedblk.h_prog&0xFF,nam,ext
			,hedblk.i_fver,hedblk.i_rvno,hedblk.h_fnum
			,hedblk.h_fseq);

	FI;
END
/* end: bum.c */
