#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define  n0  80 
main(argc,argv)
int argc;
char **argv;
{
	FILE *fp1,*fp2,*fopen();
	char line1[128],line2[128],line3[128],word[16];
	int maximum,len,account;
        int i;
	struct table {
		struct table *tbn;
		struct table *tbl;
		char hostname[16];
		char account[16];
	} *table,*tbl,*tail,*newlink,*newp;

	if(argc != 3){
		printf("Usage: %s input_file output_file\n",argv[0]);
		return(1);
	}
	if((fp1 = fopen(argv[1],"r")) == NULL){
		printf("Can't not open %s for reading\n",argv[1]);
		return(1);
	}
	if((fp2 = fopen(argv[2],"w")) == NULL){
		printf("Can't not open %s for writing\n",argv[1]);
		return(1);
	}
	tbl = NULL;
	while(fgets(line1,128,fp1) != NULL){
	        for(i=0;i<n0;i++)if(line1[i]!=line3[i])goto kkk;
	        continue;
kkk:
		if(fgets(line2,128,fp1) == NULL){
			printf("Check your source file, it is modified\n");
			printf("unsuccessful converting\n");
			fclose(fp1);
			fclose(fp2);
			return(1);
                }
/* if line2[0]=='C', line1=line2; goto kkk; */
	if(line2[0]=='C'){ strcpy(line1,line2); goto kkk; }
		if(fgets(line3,128,fp1) == NULL){
			printf("Check your source file, it is modified\n");
			printf("unsuccessful converting\n");
			fclose(fp1);
			fclose(fp2);
			return(1);
		}
		if((table = malloc(sizeof(struct table))) == NULL){
			printf("Can not allocate enough memory\n");
			printf("Unsuccessfuly converting\n");
			return(1);
		}
		table->tbl = tbl;
		table->tbn = NULL;
		if(tbl != NULL) tbl->tbn = table;
		tbl = table;
		sscanf(line1,"%s%s",word,table->hostname);
		sscanf(line3,"%s%s",word,table->account);
	}

	tail = table;
	if((newlink = malloc(sizeof(struct table))) == NULL){
		printf("Can not allocate enough memory\n");
		printf("Unsuccessfuly converting\n");
		return(1);
	}
	newp = newlink;

	while(tail != NULL){
		table = tail;
		maximum = 0;
		while(table != NULL){
			sscanf(table->account,"%d",&account);
			len = strlen(table->account);
			if(table->account[len-1] == 'M')
				account *= 1048576;
			if(table->account[len-1] == 'K')
				account *= 1024;
			if(table->account[len-1] == 'G'){
				tbl = table;
				break;
			}
			if(account >= maximum){
				maximum = account;
				tbl = table;
			}
			table = table->tbl;
		}
		if(tbl->tbl != NULL) 
			(tbl->tbl)->tbn = tbl->tbn;
		if(tbl->tbn != NULL) 
			(tbl->tbn)->tbl = tbl->tbl;
		else {
			tail = tbl->tbl;
			if(tail != NULL) tail->tbn = NULL;
		}
		newp->tbn = tbl;
		tbl->tbn = NULL;
		newp = tbl;
	}

	newp = newlink;
	while(newp->tbn != NULL){
		tbl = newp;
		newp = newp->tbn;
		if(strlen(newp->hostname) >= 8)
		fprintf(fp2,"%s\t%s\n",newp->hostname,newp->account);
		else
		fprintf(fp2,"%s\t\t%s\n",newp->hostname,newp->account);
		free(tbl);
	}
	free(tbl);
}
