💾 Archived View for mirrors.apple2.org.za › archive › ground.icaen.uiowa.edu › Mirrors › uni-kl › un… captured on 2024-06-20 at 12:08:27.

View Raw

More Information

-=-=-=-=-=-=-

/* File Dump 1.0 */

/* fidu extracts a file from a Apple ][ DOS 3.3 disk image */
/* The disk image should have a size of 143360 bytes */
/* and the sector order should be DOS 3.3 interleave. */

/* usage: fidu <disk-image> <file-name> */

#include <stdio.h>

#define ts(t,s) (256*(s+16*t))

main(argc,argv)
int argc;
char **argv;
{
	FILE *f,*g;
	unsigned char buf[256];
	unsigned char buf2[256];
	char name[32];
	int tr,se;
	int tsltr,tslse;
	int i,j;
	char found,filetype;
	int filesize,writesize;

	if (argc!=3)
	{
		fprintf(stderr,"usage: fidu <disk-image> <file-name>\n");
		exit(1);
	}

	name[30]=0;
	f=fopen(argv[1],"r");

	/* read volume table of contents */
	fseek(f,ts(17,0),0);
	fread(buf,1,256,f);
	tr=buf[1];
	se=buf[2];

	/* read directory */
	found=0;
	while ((tr || se) && !found)
	{
		fseek(f,ts(tr,se),0);
		fread(buf,1,256,f);
		i=0;
		while ((i<7) && !found)
		{
			for (j=0;j<30;j++)
				name[j]=buf[j+14+35*i]-128;
			if (!strncasecmp(name,argv[2],strlen(argv[2])))
			{
				tsltr=buf[11+35*i];
				tslse=buf[12+35*i];
				filetype=buf[13+35*i] & 7;
				found=1;
			}
			i++;
		}
		tr=buf[1];
		se=buf[2];
	}

	if (!found)
	{
		fprintf(stderr,"File not found.\n");
		exit(1);
	}

	/* open target file */
	g=fopen(argv[2],"w");
	filesize=0;

	/* track sector list */
	do
	{
		fseek(f,ts(tsltr,tslse),0);
		fread(buf,1,256,f);

		/* read data file */
		tr=buf[12];
		se=buf[13];
		i=0;
		do
		{
			fseek(f,ts(tr,se),0);
			fread(buf2,1,256,f);
			if (filesize)
				if (filesize>255)
					writesize=256;
				else
					writesize=filesize;
			else
				writesize=256-filetype;
			fwrite(buf2+filetype,1,writesize,g);
			if (filetype)
			{
				if (filetype==2)
					filesize=buf2[0]+256*buf2[1]+2;
				if (filetype==4)
					filesize=buf2[2]+256*buf2[3]+4;
				filetype=0;
			}
			if (filesize)
				filesize -= 256;
			i++;
			tr=buf[12+2*i];
			se=buf[13+2*i];
		}
		while ((tr || se) && (i<122));

		tsltr=buf[4];
		tslse=buf[5];
	}
	while (tsltr || tslse);
	
	fclose(f);
	fclose(g);
}