
#include <stdio.h>

typedef	struct {
	char	name[20];
	int	wave;
	long	score;	
	} Scores;

int CompareScores(Scores *A, Scores *B)
{
	if ( A->score < B->score )
		return(1);
	else if ( A->score > B->score )
		return(-1);
	else
		return(0);
}

main(int argc, char *argv[])
{
	Scores scores[20];
	FILE *scores_fp;

	if ( argc != 4 ) {
		fprintf(stderr, "Usage: %s scores1 scores2 outfile\n", argv[0]);
		exit(1);
	}

	/* Read first scores file */
	if ( (scores_fp=fopen(argv[1], "r")) == NULL ) {
		perror(argv[1]);
		exit(3);
	}
	(void) fread(&scores[0], sizeof(Scores), 10, scores_fp);
	fclose(scores_fp);

	/* Read second scores file */
	if ( (scores_fp=fopen(argv[2], "r")) == NULL ) {
		perror(argv[2]);
		exit(3);
	}
	(void) fread(&scores[10], sizeof(Scores), 10, scores_fp);
	fclose(scores_fp);

	/* Sort and merge them */
	qsort(scores, 20, sizeof(Scores), CompareScores);

	/* Write out the top ten scores */
	if ( (scores_fp=fopen(argv[3], "w")) == NULL ) {
		perror(argv[3]);
		exit(3);
	}
	(void) fwrite(&scores[0], sizeof(Scores), 10, scores_fp);
	fclose(scores_fp);

	printf("Scores merged.\n");
}

