DUtils

git clone git://xatko.vsos.ethz.ch/DUtils.git
Log | Files | Refs

bytehist.c (595B)


      1 #include <stdio.h>
      2 #include <unistd.h>
      3 #include <stdint.h>
      4 #include <string.h>
      5 
      6 int main(int argc, char **argv){
      7 	unsigned char buf[4092];
      8 	size_t hist[256];
      9 	size_t ret=0;
     10 	size_t i;
     11 	size_t cnt=0;
     12 	int relative=1;
     13 	if(argc>1){
     14 		if(strcmp("-r", argv[1]) == 0){
     15 			relative=0;
     16 		}
     17 	}
     18 	for(i=0; i<255; i++){
     19 		hist[i]=0;
     20 	}
     21 	while((ret=read(STDIN_FILENO, buf, sizeof(buf)))>0){
     22 		for(i=0; i<ret; i++){
     23 			hist[(size_t)(buf[i])]++;
     24 		}
     25 		cnt+=ret;
     26 	}
     27 	for(i=0; i<256; i++){
     28 		if(relative){
     29 			printf("%lu\t%lu\n", i, hist[i]);
     30 		}
     31 		else{
     32 			printf("%lu\t%e\n", i, (double)hist[i]/cnt);
     33 		}
     34 	}
     35 }