const char *_VERSION="\0$VER: Top Ten 1.0 (18.03.95)";

#include <stdio.h>
#include <stdlib.h>
#include <exec/ports.h>
#include <graphics/gfx.h>
#include <exec/semaphores.h>
#include <graphics/rastport.h>
#include <cnet/empty.h>
#include <cnet/cnet.h>
#include <string.h>

FILE	*fp;

short	zeile,
		spalte,
		max,
		i,
		j,
		vertauscht;
float	megs;

char	line[80];  /* 63 */

struct	daten
{
	long	upbytes,	/*	416	*/
			downbytes,	/*	424	*/
			totalcalls;	/*	404	*/
	char	handle[21];	/*	4	*/
}top[300],swapper;		/*	672	*/

main(int argc,char **argv)
{
	PreMain(argc,argv);        /*** Let it here, dute! ***/

	if (z->user1.Access<atoi(argv[2]))
		goto hell;

	/* EINLESEN DER BBS.UDATA */

	i=0;
	if (fp=fopen("sysdata:bbs.udata","r"))
	{
		fseek(fp,4,SEEK_SET);
		do
		{
			fread(&top[i].handle,20,1,fp);
			fseek(fp,380,SEEK_CUR);
			fread(&top[i].totalcalls,4,1,fp);
			fseek(fp,8,SEEK_CUR);
			fread(&top[i].upbytes,4,1,fp);
			fseek(fp,4,SEEK_CUR);
			fread(&top[i].downbytes,4,1,fp);
			i++;
		}
		while(!(fseek(fp,248,SEEK_CUR)));
		fclose(fp);
	}
	else
		goto hell;

	max=i;

	ReadFile("pfiles:top/top.ansi",0);
	PutText("[3m[9;14Hc7 No  User       Up(MB)  Calls[3CUser     Down(MB)  Callscd");

	vertauscht=1;
	while (vertauscht)
	{
		vertauscht=0;
		for (j=1;j<max;j++)
		{
			i=j-1;
			if (top[i].upbytes<top[j].upbytes)
			{
				swapper=top[i];
				top[i]=top[j];
				top[j]=swapper;
				vertauscht=1;
			}
		}
	}

	zeile=10;
	spalte=14;
	j=0;
	for(i=0;i<10;i++)
	{
		if(i==1) PutText("c5");
		j++;
		megs=top[i].upbytes/1024.0;
		sprintf(line,"[%-2.2d;%-2.2dH%2d  %-8.8s  %7.1f  %5d",zeile,spalte,j,top[i].handle,megs,top[i].totalcalls);
		PutText(line);
		zeile++;
		spalte--;
	}

	vertauscht=1;
	while (vertauscht)
	{
		vertauscht=0;
		for (j=1;j<max;j++)
		{
			i=j-1;
			if (top[i].downbytes<top[j].downbytes)
			{
				swapper=top[i];
				top[i]=top[j];
				top[j]=swapper;
				vertauscht=1;
			}
		}
	}

	zeile=10;
	spalte=45;
	j=0;
	for(i=0;i<10;i++)
	{
		if(i==0) PutText("cd");
		if(i==1) PutText("c5");
		j++;
		megs=top[i].downbytes/1024.0;
		sprintf(line,"[%-2.2d;%-2.2dH%-8.8s  %7.1f  %5d",zeile,spalte,top[i].handle,megs,top[i].totalcalls);
		PutText(line);
		zeile++;
		spalte--;
	}

	PutText("[22;1HCfPress RETURN to continueG0");

hell:
	PostMain();          /*** Must be here ***/
}
