32 using namespace basis;
43 #define LOG(to_print) program_wide_logger::get().log(to_print, ALWAYS_PRINT)
48 Usage:\n\t%s [-q] [-t|-b] filename [filename]\n\n\
49 This program generates a checksum for each file that is entered on the\n\
50 command line. The checksum is (hopefully) an architecture independent\n\
51 number that is a very compressed representation of the file gestalt.\n\
52 If one compares two copies of a file, then the checksums should be identical.\n\
53 This is a useful test of whether a file copy or a program download is\n\
54 successful in making an identical version of the file. In particular, if the\n\
55 file is made slightly bigger or smaller, or if an item in the file is changed,\n\
56 then the checksums of the two versions should be different numbers.\n\n\
57 The -q flag specifies a quieter print-out, without any headers.\n\
58 The -b flag is used if the files are to be compared as binary files, and this\n\
59 is also the default. The -t flag is used if the files are to be compared as\n\
65 #define HIGHEST_CHECK 32714
72 char file_open_mode[10];
73 if (open_as_a_text_file) strcpy(file_open_mode,
"rt");
74 else strcpy(file_open_mode,
"rb");
75 FILE *opened_file = fopen(file_name.
s(), file_open_mode);
79 if (!opened_file)
return common::NOT_FOUND;
80 int characters_read = 0;
81 int current_checksum_value = 0;
83 while (!feof(opened_file)) {
84 characters_read = int(fread(buffer_chunk,
sizeof(
char),
buffer_size,
90 if (characters_read <= 0) {
91 if (characters_read < 0) current_checksum_value = -1;
92 else if (current_checksum_value == 0) current_checksum_value = -1;
95 current_checksum_value = (current_checksum_value
96 + checksums::bizarre_checksum((
abyte *)buffer_chunk, characters_read))
99 LOG(
a_sprintf(
"current checksum=%d", current_checksum_value));
103 return int(current_checksum_value);
111 char file_open_mode[10];
112 if (open_as_a_text_file) strcpy(file_open_mode,
"rt");
113 else strcpy(file_open_mode,
"rb");
114 FILE *opened_file = fopen(file_name.
s(), file_open_mode);
118 if (!opened_file)
return common::NOT_FOUND;
119 int characters_read = 0;
120 int current_checksum_value = 0;
122 while (!feof(opened_file)) {
123 characters_read = int(fread(buffer_chunk,
sizeof(
char),
buffer_size,
129 if (characters_read <= 0) {
130 if (characters_read < 0) current_checksum_value = -1;
131 else if (current_checksum_value == 0) current_checksum_value = -1;
134 current_checksum_value = checksums::rolling_fletcher_checksum
135 ((
uint16)current_checksum_value, (
abyte *)buffer_chunk,
138 LOG(
a_sprintf(
"current checksum=%d", current_checksum_value));
142 return current_checksum_value;
145 int main(
int argc,
char *argv[])
148 bool open_file_as_text =
false;
150 bool show_header =
true;
156 if (cmds.
find(
'b', index)) open_file_as_text =
false;
158 if (cmds.
find(
't', index)) open_file_as_text =
true;
160 if (cmds.
find(
'q', index)) show_header =
false;
165 bool printed_header =
false;
167 for (
int entry = 0; entry < cmds.
entries(); entry++) {
170 if (!printed_header) {
171 printed_header =
true;
173 printf(
"%s\n", (
astring(
"[ checker running at ") + time_stamp::notarize(
true) +
"]").s());
174 printf(
"bizarro fletcher filename\n");
175 printf(
"======= ======== ========\n");
179 int checksum_of_file =
do_checksum(name, open_file_as_text);
181 if (checksum_of_file >= 0) {
182 printf(
"%s",
a_sprintf(
" %05d 0x%04x %s\n", checksum_of_file,
183 fletcher_chksum, name.
s()).
s());
185 printf(
"%s",
a_sprintf(
"%s is inaccessible.\n", name.
s()).
s());
int main(int argc, char *argv[])
int do_checksum(const astring &file_name, int open_as_a_text_file)
int do_fletcher_checksum(const astring &file_name, int open_as_a_text_file)
int print_instructions(bool good, const astring &program_name)
int entries() const
Returns the number of fields found on the command line.
bool find(char option_character, int &index, bool case_sense=true) const
Returns true if the "option_character" is found in the parameters.
const command_parameter & get(int field) const
Returns the parameter at the "field" specified.
const basis::astring & text() const
observes the string contents.
parameter_types type() const
observes the type of the parameter.
a_sprintf is a specialization of astring that provides printf style support.
Provides a dynamically resizable ASCII character string.
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
unsigned char abyte
A fairly important unit which is seldom defined...
A logger that sends to the console screen using the standard output device.
A dynamic container class that holds any kind of object via pointers.