#include #include #include #include #include #include #include #include #include #include #include #define HELPLINECOUNT 30 char *helplines[HELPLINECOUNT] = { " ", " Usage: dump -n[0-9.+BKMGTP] -x -b -s -l -ll filename", " ", " dump reads the file specified and prints out the integer values", " of the file data. The flags specify limited format options.", " ", " -n specifies a number of bytes to output where the optional uppercase", " letter specifies the units. The digit string may follow the -n", " immediately or the digits can be separated by space. The optional", " units tag must immediately follow the last digit, it cannot be a", " separate CLI argument. The number given is always used rounded to", " the next value equal to 0 modulao 16. In other words, the number", " is always rounded up to the next 16 byte boundary if not aligned.", " B = 8 bit bytes", " K = 1024 bytes", " M = 1024 * 1024 bytes", " G = 1024 * 1024 * 1024 bytes", " T = 1024 * 1024 * 1024 * 1024 bytes", " P = 1024 * 1024 * 1024 * 1024 * 1024 bytes", " ", " The other CLI switches are mutually exclusive and specify the", " bit length of the output.", " -x = HEX numbers", " -b = 8 bit bytes", " -s = 16 bit short integers", " -l = 32 bit integers", " -ll = 64 bit integers", " ", " The filename can be any readable file.", " ", }; /*#### END char *helplines[HELPLINECOUNT] = { ####*/ /* This is just here so you can just grep and cut and paste to recompile compile gcc -o dump -Wall -O3 dump.c */ #define MAXINP 65536 /***************************************************************************/ int32_t main(int32_t argc, char ** argv) { int32_t i = 0; /* primary loop counter */ int32_t readcount = 0; /* bytes read from last input file or CLI string */ int32_t total = 0; /* bytes read from last input file or CLI string */ int32_t linecount = 0; /* bytes read from last input file or CLI string */ int32_t intsize = 1; /* Size of decimal integers to print */ int32_t hexflag = 0; /* Output hexadeciman instead of decimal integers*/ int32_t blanklines = 1; /* Size of decimal integers to print */ char curargv[MAXINP]; /* for reading in CLI arguments */ int32_t curargc = 1; /* The current CLI argument we are working on */ FILE *INP = NULL; /* For reading files */ unsigned char charbuffer[MAXINP]; /* input buffer for seed data files */ unsigned short int shortbuffer[MAXINP]; /* input buffer for seed data files */ unsigned int longbuffer[MAXINP]; /* input buffer for seed data files */ unsigned long long int longlongbuffer[MAXINP]; /* input buffer for seed data files */ char line[1024]; /* used to read in the config file line by line */ uint64_t outputlimit = 0; /* optional number of numbers to output */ char limitunits = 'B'; /* units to count outputlimit in. B K M G T P */ for (i=0; i 2) { strncpy(line, &curargv[2], 80); } else { ++curargc; if (curargc > argc) { printf("Error: Specified number requested but amount not given.\n"); return(0); } strncpy(line, argv[curargc-1], 80); } i = 0; outputlimit = 0; while ((line[i] >= 48) && (line[i] <= 57) && (line[i] != 0)) { outputlimit = (outputlimit*10) + (line[i]-48); ++i; } if (line[i] == 0) { limitunits = 'B'; } else { limitunits = line[i]; } switch (limitunits) { case 'K': outputlimit *= 1024LLU; break; case 'M': outputlimit *= 1048576LLU; break; case 'G': outputlimit *= 1073741824LLU; break; case 'T': outputlimit *= 1099511627776LLU; break; case 'P': outputlimit *= 1125899906842624LLU; break; default: break; } limitunits = 'S'; /* this is now used as a flag that says Set. */ /*printf("DEBUG CLI limitunits = %c outputlimit = %d\n", limitunits, outputlimit);*/ ++curargc; continue; } /* * If the readflag is not set then anything that gets down here is not a * program control option so its either CLI data or a file to * read in for data. */ errno = 0; INP = fopen(curargv, "r"); if (errno == 0) { ++curargc; } else { printf("Error: Can't open %s for reading. Exiting now.\n", curargv); exit(1); } } total = 0; linecount = 0; switch (intsize) { case 1: if ((outputlimit < MAXINP) && (outputlimit > 0)) { readcount = fread(charbuffer, 1, outputlimit, INP); } else { readcount = fread(charbuffer, 1, MAXINP, INP); } while (readcount) { for (i=0; i= outputlimit) { return(0); } } if ((outputlimit-total) < MAXINP) { readcount = fread(charbuffer, 1, outputlimit, INP); } else { readcount = fread(charbuffer, 1, MAXINP, INP); } } break; case 2: if ((outputlimit < (MAXINP*2)) && (outputlimit > 0)) { readcount = fread(shortbuffer, 2, outputlimit/2, INP); } else { readcount = fread(shortbuffer, 2, MAXINP, INP); } while (readcount) { for (i=0; i= outputlimit) { return(0); } } if ((outputlimit-total) < MAXINP*2) { readcount = fread(shortbuffer, 2, outputlimit/2, INP); } else { readcount = fread(shortbuffer, 2, MAXINP, INP); } } break; case 4: if ((outputlimit < (MAXINP*4)) && (outputlimit > 0)) { readcount = fread(longbuffer, 4, outputlimit/4, INP); } else { readcount = fread(longbuffer, 4, MAXINP, INP); } while (readcount) { for (i=0; i= outputlimit) { return(0); } } if ((outputlimit-total) < MAXINP*4) { readcount = fread(longbuffer, 4, outputlimit/4, INP); } else { readcount = fread(longbuffer, 4, MAXINP, INP); } } break; case 8: if ((outputlimit < (MAXINP*4)) && (outputlimit > 0)) { readcount = fread(longlongbuffer, 8, outputlimit/8, INP); } else { readcount = fread(longlongbuffer, 8, MAXINP, INP); } while (readcount) { for (i=0; i= outputlimit) { return(0); } } if ((outputlimit-total) < MAXINP*8) { readcount = fread(longlongbuffer, 8, outputlimit/8, INP); } else { readcount = fread(longlongbuffer, 8, MAXINP, INP); } } break; default: break; } return(0); } /* END main */