Script started on Thu Feb 13 13:45:18 2003 [root@Fizzgig /root]# cd C [root@Fizzgig C]# cat makefile bytecount: bytecount.o sortedprint.o gcc bytecount.o sortedprint.o -o bytecount bytecount.o: bytecount.c bytecount.h gcc -c bytecount.c sortedprint.o: sortedprint.c bytecount.h gcc -c sortedprint.c [root@Fizzgig C]# cat bytecount.h int bytes[256][2]; /* possible bytes with count [0] and byte value [1]*/ /* in bytecount.c */ void usage(); void loadBytesArray(); /* in sortedprint.c */ void sortBytesArray(); void printBytesArray(); [root@Fizzgig C]# cat bytecount.c /*bytecount.c */ /*Given a file, prints to the screen the sorted frquency */ /* of each byte in that file. */ /* */ /*This module deals primarily with file I/O. */ /* */ /*Author: Zach Tomaszewski */ /*Date: 13 February */ #include "bytecount.h" #include //only module dealing with i/o FILE *fileInp; short verbose = 0; /* print processing info? true(1) or false(0) */ int main(int argc, char *argv[]) { int i; /* temp loop counter */ if (argc > 3 || (argc == 2 && strcmp(argv[1], "-h") == 0) ){ /*too many arguments or request for help */ usage(); }else if (argc > 2) { /* two arguments given */ if (strcmp(argv[1], "-v") == 0) { fileInp = fopen(argv[2], "rb"); verbose = 1; }else { usage(); } }else if (argc > 1) { /* one argument given */ fileInp = fopen(argv[1], "rb"); }else { /* no args; use stdin */ fileInp = stdin; } if (fileInp==NULL) { puts("Error: Could not open given input file."); exit(1); } loadBytesArray(); sortBytesArray(); printBytesArray(); close(fileInp); } /*Using the file in *fileInp, loads bytes[][] with a frequency count. */ void loadBytesArray(){ int ch; /*read-in char */ int i; /*loop counter */ /*init bytes[] */ i = 0; while (i < 256) { bytes[i][0] = 0; /* for frequency */ bytes[i][1] = i; /* for byte value */ i++; } /*load array with file contents */ ch = getc(fileInp); while (!feof(fileInp) ){ if (verbose) { putchar(ch); } bytes[ch][0]++; ch = getc(fileInp); } /*show unsorted array for debugging*/ if (verbose) { printf("\nUnsorted:"); printBytesArray(); } } /* Gives a usage message for this program */ void usage() { puts(""); puts("Bytecount returns the frequency of each byte in a given file."); puts("Enter the name of a single file to process as a command line argument"); puts("or else pipe/redirect data through standard input."); puts("\nUsage: bytecount [-hv] [filename] \n"); puts("-v\t verbose. Gives debugging information such as unsorted input"); puts("-h\t help. Prints this help message."); puts(""); exit(0); } [root@Fizzgig C]# cat sortedprint.c /*sortedprint.c */ /*Part of bytecount, this module deals primarily with */ /* --sorting the bytes array based on count frequency */ /* --printing the final count of the sorted bytes array */ /* */ /*Author: Zach Tomaszewski */ /*Date: 13 February */ #include "bytecount.h" /*Sort the bytes array based on the count of each byte */ void sortBytesArray(){ int s, temp, temp2, highest; /* for sorting */ register int i; /*sort by finding next highest frequecy... */ for (s = 0; s < 256; s++){ temp = 0; temp2 = 0; highest = 0; i = 0; for (i=s; i < 256; i++){ if (bytes[i][0] > bytes[highest][0]){ highest = i; } } /*... and inserting it in order at the beginning */ if (bytes[s][0] < bytes[highest][0]) { temp = bytes[s][0]; temp2 = bytes[s][1]; bytes[s][0] = bytes[highest][0]; bytes[s][1] = bytes[highest][1]; bytes[highest][0] = temp; bytes[highest][1] = temp2; } } } /* Prints the bytes array to the screen. */ /* Only prints character if they have a count > 0 */ void printBytesArray(){ int s, i; //loop counters long totalBytes = 0; i = 0; for (s = 0; s < 256; s++){ if (bytes[s][0] > 0){ if (i++ % 4 == 0) { puts(""); } printf("%3d(", bytes[s][1]); if (bytes[s][1] >= 32 && bytes[s][1] <= 126){ //printable printf("%c", bytes[s][1]); }else{ //unprintable printf(" "); } printf(") %d \t", bytes[s][0]); totalBytes += bytes[s][0]; } } printf("\nTotal bytes: %d\n\n", totalBytes); } [root@Fizzgig C]# make gcc -c bytecount.c gcc -c sortedprint.c gcc bytecount.o sortedprint.o -o bytecount [root@Fizzgig C]# bytecount se.txt 101(e) 16 32( ) 13 114(r) 8 116(t) 6 99(c) 5 100(d) 5 115(s) 5 44(,) 3 111(o) 3 117(u) 3 105(i) 2 40(() 1 41()) 1 46(.) 1 68(D) 1 70(F) 1 80(P) 1 83(S) 1 98(b) 1 104(h) 1 110(n) 1 118(v) 1 120(x) 1 Total bytes: 81 [root@Fizzgig C]# bytecount poly.txt 32( ) 53 49(1) 14 51(3) 8 50(2) 6 53(5) 5 10( ) 2 48(0) 2 55(7) 2 57(9) 2 52(4) 1 Total bytes: 95 [root@Fizzgig C]# bytecount rand.bin 17( ) 2 55(7) 2 159( ) 2 210( ) 2 7( ) 1 0( ) 1 26( ) 1 38(&) 1 41()) 1 47(/) 1 50(2) 1 53(5) 1 71(G) 1 77(M) 1 82(R) 1 87(W) 1 92(\) 1 102(f) 1 110(n) 1 111(o) 1 120(x) 1 127( ) 1 135( ) 1 147( ) 1 151( ) 1 153( ) 1 161( ) 1 162( ) 1 172( ) 1 177( ) 1 179( ) 1 181( ) 1 193( ) 1 207( ) 1 208( ) 1 209( ) 1 4( ) 1 218( ) 1 222( ) 1 224( ) 1 229( ) 1 230( ) 1 232( ) 1 233( ) 1 234( ) 1 255( ) 1 Total bytes: 50 [root@Fizzgig C]# bytecount msdev.exe 255( ) 153329 0( ) 53295 103(g) 3208 119(w) 2744 141( ) 2194 97(a) 1553 15( ) 1523 142( ) 1419 117(u) 1394 205( ) 1339 240( ) 1288 87(W) 1252 136( ) 1179 104(h) 1143 85(U) 1097 128( ) 1097 236( ) 1059 247( ) 1034 140( ) 1010 239( ) 992 7( ) 967 153( ) 960 130( ) 921 5( ) 903 96(`) 798 112(p) 726 61(=) 709 235( ) 702 182( ) 694 102(f) 682 135( ) 653 120(x) 633 1( ) 608 38(&) 581 101(e) 554 204( ) 553 20( ) 550 143( ) 548 3( ) 546 244( ) 530 16( ) 523 56(8) 518 68(D) 517 32( ) 517 77(M) 496 21( ) 483 248( ) 481 100(d) 477 111(o) 454 245( ) 445 105(i) 429 147( ) 420 241( ) 419 64(@) 413 110(n) 406 116(t) 401 127( ) 398 8( ) 382 4( ) 377 242( ) 369 19( ) 349 11( ) 349 51(3) 346 237( ) 341 188( ) 335 18( ) 320 114(r) 317 17( ) 316 67(C) 313 2( ) 313 115(s) 303 109(m) 293 187( ) 292 84(T) 283 146( ) 273 69(E) 261 108(l) 247 80(P) 228 13( ) 226 192( ) 207 215( ) 203 170( ) 194 44(,) 193 88(X) 190 174( ) 182 125(}) 169 86(V) 160 133( ) 157 63(?) 155 81(Q) 154 151( ) 152 22( ) 152 30( ) 150 57(9) 150 9( ) 148 129( ) 144 62(>) 143 234( ) 138 71(G) 132 65(A) 132 83(S) 128 212( ) 123 99(c) 121 98(b) 121 181( ) 118 121(y) 116 31( ) 116 175( ) 113 173( ) 105 113(q) 103 48(0) 100 176( ) 94 10( ) 93 37(%) 92 246( ) 90 79(O) 88 6( ) 87 82(R) 83 23( ) 82 12( ) 81 25( ) 78 40(() 77 76(L) 76 53(5) 75 216( ) 72 27( ) 67 46(.) 67 254( ) 66 60(<) 63 52(4) 60 118(v) 60 148( ) 59 26( ) 59 224( ) 57 232( ) 57 95(_) 55 66(B) 55 160( ) 54 183( ) 54 144( ) 50 34(") 48 14( ) 47 89(Y) 46 107(k) 43 28( ) 42 72(H) 41 73(I) 41 225( ) 41 250( ) 40 78(N) 38 42(*) 38 36($) 36 55(7) 36 206( ) 36 33(!) 35 24( ) 35 137( ) 34 196( ) 33 54(6) 33 70(F) 33 221( ) 32 222( ) 32 43(+) 32 29( ) 31 194( ) 31 162( ) 30 106(j) 30 131( ) 29 149( ) 28 252( ) 28 177( ) 27 49(1) 27 152( ) 27 168( ) 27 41()) 27 35(#) 25 253( ) 25 139( ) 24 208( ) 24 145( ) 24 74(J) 23 126(~) 23 161( ) 22 134( ) 21 45(-) 20 195( ) 19 200( ) 19 93(]) 19 223( ) 19 178( ) 18 122(z) 18 214( ) 17 50(2) 17 92(\) 16 238( ) 16 191( ) 16 203( ) 15 184( ) 15 231( ) 15 39(') 15 220( ) 14 189( ) 14 132( ) 14 251( ) 14 150( ) 13 58(:) 13 193( ) 13 198( ) 12 227( ) 12 47(/) 12 75(K) 12 199( ) 11 213( ) 10 157( ) 10 230( ) 9 164( ) 9 94(^) 9 179( ) 8 90(Z) 8 156( ) 8 159( ) 8 123({) 8 124(|) 8 169( ) 8 211( ) 8 228( ) 7 172( ) 7 233( ) 7 180( ) 7 243( ) 7 218( ) 7 138( ) 7 190( ) 6 166( ) 6 59(;) 6 165( ) 6 217( ) 6 202( ) 5 158( ) 5 219( ) 4 210( ) 4 209( ) 4 154( ) 3 207( ) 3 163( ) 3 186( ) 3 249( ) 3 185( ) 3 226( ) 2 201( ) 2 229( ) 1 167( ) 1 Total bytes: 270406 [root@Fizzgig C]# exit Script done on Thu Feb 13 13:50:03 2003