/*

(C) Copyright 1988 Diomidis D. Spinellis. All rights reserved.

This profiler is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves
any particular purpose or works at all, unless he says so in writing.
Refer to the profiler General Public License for full details.

Everyone is granted permission to copy, modify and redistribute
the profiler, but only under the conditions described in the
profiler General Public License.  A copy of this license is
supposed to have been given to you along with the profiler so you can
know your rights and responsibilities.  It should be in a file named
COPYING.  Among other things, the copyright notice and this notice
must be preserved on all copies.

In other words, go ahead and share the profiler, but don't try to stop
anyone else from sharing it farther.  Help stamp out software
hoarding!  

*/

/*
 * Print an analysis of the profiler results.
 *
 * (C) Copyright 1988 Diomidis D. Spinellis. All rights reserved.
 *
 * The code contained herein is not the best. It is given as a substitute
 * for the ten line awk script that has the same functionality.
 *
 * $Header: PROFPRT.C^v 1.1 88/11/20 17:36:12 dds Rel $
 *
 * $Log:	PROFPRT.C^v $
 * Revision 1.1  88/11/20  17:36:12  dds
 * Initial revision
 * 
 *
 */

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>


/* Profiler output maximum line length */
#define LINELEN 129

/* Linker output maximum symbol length */
#define STRLEN	65

char *Argv0 ;

static char rcsid[] = "$Header: PROFPRT.C^v 1.1 88/11/20 17:36:12 dds Rel $" ;

static void usage( void ) ;

static void
usage()
{
	fprintf(stderr, "Usage : %s [-h] [file]", Argv0 ) ;
	exit( 1 ) ;
}

void
main(int argc, char *argv[] )
{
	FILE *f ;
	long t, total = 0, max = -1 ;
	static char line[LINELEN], str[STRLEN] ;
	char *fname = "prof.out" ;
	int histo = 0, fname_given = 0 ;
	register i ;

	Argv0 = argv[0] ;

	while( argc > 1 )
		switch( *argv[1] ){
		case '-' :
			if( argv[1][1] == 'h' ){
				histo++ ;
				argc-- ;
				argv++ ;
			} else
				usage() ;
			break ;
		default :
			if( fname_given )
				usage() ;
			else {
				fname_given++ ;
				fname = argv[1] ;
				argc-- ;
				argv++ ;
			}
			break ;
		}

	if( ( f = fopen( fname, "r" ) ) == NULL ){
		perror( fname ) ;
		exit( 1 ) ;
	}
	for( i = 1 ; fgets(line, LINELEN, f ) ; i++ ){
		if( sscanf(line, " %*s %ld ", &t ) != 1 ){
			fprintf(stderr, "%s : Error in reading %s(%d)\n", Argv0, fname, i ) ;
			exit( 1 ) ;
		}
		total += t ;
		if( t > max )
			max = t ;
	}
	if( total == 0 ){
		fprintf(stderr, "%s : No hits found\n", Argv0 ) ;
		exit( 1 ) ;
	}
	(void)rewind( f ) ;
	while( fgets(line, LINELEN, f ) ){
		(void)sscanf(line, " %s %ld ", str, &t ) ;
		if( t ){
			printf("%-20s %5.2lf%% ", *str == '_' ? str+1 : str, (double)t / (double)total * 100.0 ) ;
			if( histo )
				for( i = 0 ; i < (int)((double)t / (double)max * 50 ) ; i++ )
					putchar( '*' ) ;
			putchar( '\n' ) ;
		}
	}
	exit( 0 ) ;
}

