#include <stdio.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>

#define MAXSIZE 8192

/*
 * Date frequence statics tool
 * 
 * Author: zhouer@zhouer.tfcis.org
 * Create: 2004/02/24
 * Update: --
 *
 * Todo list
 * Implement -q: quiet & -d: decimal
 */

main( int argc, char *argv[] ) {

	int i, nfile, size, fd, totalsize, needclose;
	int count[256];
	unsigned char buf[MAXSIZE];

	for( nfile = 1; nfile < argc; nfile++ ) {

		totalsize = 0;
		bzero( count, sizeof(count) );
		bzero( buf, sizeof(buf) );

		if ( *(argv[nfile] + 0) == '-' ) {
			fd = dup(STDIN_FILENO);
			needclose = 0;
		} else {
			fd = open( argv[nfile],  O_RDONLY);
			needclose = 1;
		}

		if( fd == -1 ) {
			/* can't opening file, read next file */
			fprintf(stderr, "error opening %s!\n", argv[nfile]);
			break;
		}

		while( ( size = read( fd, buf, MAXSIZE) ) != 0 ) {
			for( i = 0; i < size; i++) {
				count[buf[i]]++;
			}
			totalsize += size;
		}

		printf("# %s statics\n", argv[nfile]);
		printf("# total size: %d\n", totalsize);

		for(i = 0; i < 256; i++) {
			printf("0x%02X %d\n", i, count[i]);
		}

		printf("\n");

		if( needclose ) {
			close(fd);
		}
	}
}

