#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*#include <conio.h> */
#include <io.h>
/*#include <dos.h>*/
/*#include <ctype.h>*/


#define maxElem 600
#define maxLen 81
char list[maxElem][maxLen];
FILE *f1;
FILE *f2;

//------------------------------------------------------------------------
int compare( const void *a, const void *b)
{
    return( strcmp(a, b) );
}

//------------------------------------------------------------------------
unsigned ReadFile (char *inFile)
{
    unsigned i=0;
    FILE *fp;
    if ((fp = fopen (inFile, "r")) == NULL)
    {
	printf("unable to open input file %s\n", inFile);
	exit(1);
    }
    while ( (i<maxElem) && (fgets(list[i], maxLen, fp) != NULL) )
	i++;
    fclose(fp);
    return i;
}

//------------------------------------------------------------------------
void WriteFile (char *outFile, unsigned nItems)
{
    unsigned i = 0;
    FILE *fp;
    fp = fopen (outFile, "w");

    for (i=0; i<nItems; i++)
	fputs (list[i], fp);
    fclose(fp);
}

//------------------------------------------------------------------------
void ExitErr(char *msg)
{
    puts(msg);
    exit(1);
}


//------------------------------------------------------------------------
void fsort(char *inname,char *outname)
{
    size_t nItems;
    nItems = ReadFile(inname);
    qsort( (void *)list, (size_t)nItems, (size_t)sizeof(list[0]), compare);
    WriteFile (outname, nItems);


}

void main()
{

int l;
char *name;
char *name2;
char *part;

   if((f1=fopen("temp\\mlegend.tmp","r"))!=NULL){
      f2=fopen("temp\\sortleg.tmp","w");
      printf("\n\n\n\n\n\n\n\n\n\n        please wait... sorting");
	for(;;){
	    if(feof(f1)) break;
	    fgets(name2,81,f1);
	    if(feof(f1)) break;
	    name2[0]=toupper(name2[0]);
	    l=strlen(name2);
	    strxfrm(name,name2,l);
	    fgets(name2,81,f1);
	    if(feof(f1)) break;
	    fprintf(f2,"%-*.*s %s",28,26,name,name2);
	    if(feof(f1)) break;
	    }
	fclose(f1);
	fclose(f2);
      fsort("temp\\sortleg.tmp","text\\mlegend.txt");
      remove("temp\\sortleg.tmp");
      remove("temp\\mlegend.tmp");
    }
     
execv("start.exe",NULL);
}
