#include <fcntl.h>
#include <stdio.h>

void main(int argc, char *argv[]) {
	FILE *infile, *outfile;
	int type, temp;

	if( (argc<3) || ((infile=fopen(argv[1], "rb"))==NULL) ||
					((outfile=fopen(argv[2], "wb"))==NULL) )
		exit(1);

	for(;;) {
		do {
			temp=fgetc(infile);
			if(feof(infile)) {
				fclose(infile);
				fclose(outfile);
				exit(0);
			}
		} while(temp!=0x3c);
		temp=1;	/* leader byte */
		if(!(type=fgetc(infile)))	/* get block type */
			temp|=0x80;
		for(; temp; temp--)	/* write leader before */
			fputc(0x55, outfile);		/* name blocks */
		fputc(0x3c, outfile);	/* sync byte */
		fputc(type, outfile);	/* block type byte */
		temp=fputc(fgetc(infile), outfile)+1;	/* temp=block length */
		for(; temp; temp--)
			fputc(fgetc(infile), outfile);	/* write out data+checksum */
		fputc(0x55, outfile);	/* trailer byte */
		if(!type)
			for(temp=0x80; temp; temp--)	/* write leader after */
				fputc(0x55, outfile);		/* name blocks */
	}
}
