compare_result compare(FILE *fp1, FILE *fp2, int brief) { // NOTE!! // This code is meant to be simple so I take shortcuts like // exiting without cleaning up. Sorry. char buf1[39], buf2[8]; // these sizes are totally arbitrary scanstate ssrec, *ss=&ssrec; int n, cont; scanstate_init(ss, buf1, sizeof(buf1)); readfp_attach(ss, fp1); compare_attach(ss); do { n = fread(buf2, 1, sizeof(buf2), fp2); if(n <= 0) { if(ferror(fp2)) { fprintf(stderr, "Error reading second file.\n"); exit(17); } if(feof(fp2)) { break; } } cont = compare_continue(ss, buf2, n); if(cont < 0) { fprintf(stderr, "Error reading first file.\n"); exit(17); } } while(cont == 0); return brief ? compare_check(ss) : compare_check_newlines(ss); }
enum matchval end_output_section(struct test *test, scanstate *cmpscan, const char *name) { compare_result cmp = compare_check_newlines(cmpscan); int suppress_trailing_newline = cmpscan_suppress_newline; if(cmp == cmp_ptr_has_extra_nl) { // actual is missing a single newline as compared to expected. if(!suppress_trailing_newline) { // user hasn't marked section needing suppression so warn. warn_section_newline(test, name); return match_no; } // it's met all the requirements. We have a match. return match_yes; } if(cmp == cmp_ptr_has_more_nls && suppress_trailing_newline) { fprintf(stderr, "WARNING: %s is marked -n but it ends with multiple newlines!\n" " Please remove all but one newline from %s around line %d.\n", name, convert_testfile_name(test->testfile), test->testscanner.line); return match_no; } if(cmp == cmp_full_match) { if(suppress_trailing_newline) { if(cmpscan->line == 0) { // don't want to print the warning if it's an empty section // because, while it's weird, it's technically correct. return match_yes; } // section was marked -n but a newline was present. No match. return match_no; } // full match in a normal section (not marked -n) return match_yes; } return match_no; }