/* $Header: /CVSROOT/public/scylla-charybdis/md5backup/misc/findmincharcount.c,v 1.1 2005/07/21 09:34:42 tino Exp $ * * This is a little test program to count * * $Log: findmincharcount.c,v $ * Revision 1.1 2005/07/21 09:34:42 tino * added * */ #define _GNU_SOURCE 1 #include #include #include #define SHOWWIDTH 60 char bigbuf[0x100000]; int files; long long count[256], total; void trimlf(char *s) { size_t len; len = strlen(s); if (len && s[--len]=='\n') s[len]=0; } #define MINCOUNT 4 static void showmin(FILE *fd, char c) { int i, k, mini[MINCOUNT]; long long min[MINCOUNT], l; char s; for (k=MINCOUNT; --k>=0; ) min[k] = total; for (i=256; --i>=0; ) { if (count[i]=0 && p0) return 1; return 0; } static void showstat(int dosort) { unsigned char sort[256]; int i; for (i=256; --i>=0; sort[i]=i); if (dosort) qsort(sort, sizeof sort/sizeof *sort, sizeof *sort, sortstat); for (i=0; i<256; i++) { long long pos; int c; pos = SHOWWIDTH*SHOWWIDTH*SHOWWIDTH*count[sort[i]]/total; c = '-'; if (pos>SHOWWIDTH) { c = '='; pos /= SHOWWIDTH; } if (pos>SHOWWIDTH) { c = '#'; pos /= SHOWWIDTH; } printf("%10.6f %12lld %02x ", 100.*count[sort[i]]/total, count[sort[i]], sort[i]); while (--pos>=0) putchar(c); printf("\n"); } showmin(stdout, '\n'); } int main(void) { char buf[BUFSIZ]; FILE *fd; int c; total = 0; memset(count, 0, sizeof count); for (files=1; fgets(buf, sizeof buf, stdin); files++) { trimlf(buf); fd = stdin; if (strcmp(buf, "-") && (fd=fopen64(buf, "rb"))==0) { perror(buf); continue; } setvbuf(fd, bigbuf, _IOFBF, sizeof bigbuf); while ((c=getc(fd))>=0) { count[c]++; total++; } fclose(fd); showmin(stderr, '\r'); } showstat(0); showstat(1); return 0; }