| | 180 | inline bool files_are_equal(const char *file1, const char *file2) { |
| | 181 | const char *error = NULL; |
| | 182 | FILE *fp1 = fopen(file1, "rb"); |
| | 183 | |
| | 184 | if (!fp1) { |
| | 185 | FlushedOutput::printf("can't open '%s'", file1); |
| | 186 | error = "i/o error"; |
| | 187 | } |
| | 188 | else { |
| | 189 | FILE *fp2 = fopen(file2, "rb"); |
| | 190 | if (!fp2) { |
| | 191 | FlushedOutput::printf("can't open '%s'", file2); |
| | 192 | error = "i/o error"; |
| | 193 | } |
| | 194 | else { |
| | 195 | const int BLOCKSIZE = 4096; |
| | 196 | char *buf1 = (char*)malloc(BLOCKSIZE); |
| | 197 | char *buf2 = (char*)malloc(BLOCKSIZE); |
| | 198 | |
| | 199 | while (!error) { |
| | 200 | int read1 = fread(buf1, 1, BLOCKSIZE, fp1); |
| | 201 | int read2 = fread(buf2, 1, BLOCKSIZE, fp2); |
| | 202 | |
| | 203 | if (read1 != read2) error = "filesizes differ"; |
| | 204 | else { |
| | 205 | if (!read1) break; // done |
| | 206 | if (memcmp(buf1, buf2, read1) != 0) error = "content differs"; |
| | 207 | } |
| | 208 | } |
| | 209 | free(buf2); |
| | 210 | free(buf1); |
| | 211 | fclose(fp2); |
| | 212 | } |
| | 213 | fclose(fp1); |
| | 214 | } |
| | 215 | |
| | 216 | if (error) FlushedOutput::printf("files_are_equal(%s, %s) fails: %s\n", file1, file2, error); |
| | 217 | return !error; |
| | 218 | } |
| | 219 | |