#include <stdio.h>
#include <string.h>

struct command {
	char name[100];
	char desc[5000];
};

struct command list[1000];
int MAX_COMMAND_LENGTH = 100;

int main()
{
	FILE *fin;
	FILE *fout;
	char line[MAX_COMMAND_LENGTH];
	int command_count = 0;
	int current_command = 0;
	

	if(( fin = fopen( "cmddescr.txt", "r" )) == NULL )
        {
		printf( "There was an error opening input file cmddescr.txt" );
        }

	if(( fout = fopen( "assign3.out", "w" )) == NULL )
	{
		printf( "There was an error opening output file assign3.out" );	
	}

	while( fgets( line, MAX_COMMAND_LENGTH, fin ) )
	{
		if( line[0] == '*' )
		{
			strcpy( list[command_count].name, line );
			command_count++;
		}
		else if( line[0] != '\n' )
		{
			strcat( list[command_count - 1].desc, line );
		}	
	}		

	qsort( list, command_count, sizeof( struct command ), strcmp );

	for( current_command = 0; current_command < command_count + 1; current_command++ )
        {
                fprintf( fout, "%s%s\n", list[current_command].name, list[current_command].desc );
        }
	
	fclose( fin );
	fclose( fout );

	return 0;
}
