/* modularip.c - untangle a modula tape after it was t2b-f'ed */
#include <stdio.h>
#include <algol68.h>

extern int $$ferr;

FILE *mfil;
FILE *rfil;

char	txtblk[512];

struct dirent
BEGIN
	char filnam[32];
	unsigned  bin[16];
END;

struct dirent dirblk[8];
struct dirent * dp;

char	rsxfil[100];
char	virgin[30];

long	length;
int	b;
int	db;
int	*ip;
char	*en;
int	fn;
char	*p;

main()
BEGIN
	IF	(!(mfil=fopenr("modula.tap",512)))
	THEN	error("$$ferr=%oo fopenr",$$ferr);
	FI;
	fn = 0;
	FOR	(db=0;	db<060;	db++)
	DO	gimme(dirblk,db);
		FOR	(dp=dirblk;	dp<dirblk+8;	dp++)
		DO	IF	(*dp->filnam)
			THEN	fn++;
				printf("%03o %32s",fn,dp->filnam);
				en = dp->bin+16;
				FOR	(ip=dp->bin;	ip<en;	ip++)
				DO	printf("%6u",*ip);
				OD;
				putchar('\n');
				strcpy(rsxfil,rindex(dp->filnam,'/')+1);
				IF	(rsxfil[4]=='_')
				THEN	rsxfil[4]='0';
				FI;
				p = strchr(rsxfil,'.');
				IF	(p)
				THEN	IF	(p>rsxfil+9)
					THEN	strcpy(rsxfil+9,p);
					FI;
				ELSE	rsxfil[9]=0;
				FI;
				sprintf(virgin,";%06o",fn+010000);
				strcat(rsxfil,virgin);
				IF	(!(rfil=fopen(rsxfil,"w")))
				THEN	error("$$ferr=%oo fopen rsxfil=\"%s\"",$$ferr,rsxfil);
				FI;
				length = dp->bin[3];	/* assume not long */
				FOR	(b=dp->bin[6];	length>0;	length-=512,b++)
				DO	gimme(txtblk,b);
					en = txtblk + (length>512?512:length);
					FOR	(p=txtblk;	p<en;	p++)
					DO	putc(*p,rfil);
					OD;
				OD;
				fclose(rfil);
			FI;
		OD;
	OD;
END

gimme(buf,bn)
char	*buf;
int	bn;
BEGIN
	IF	(frget(buf,512,mfil,(long)bn+1))
	THEN	error("$$ferr=%oo gimme",$$ferr);
	FI;
END

/* end: modularip.c */
