/* bin_patch.c */ /* Based on sourcecode by Tomek Malesinski. */ /* Patchfile can be created with `cmp -l'. */ #include #include void next_patch_line(unsigned int *offset, unsigned int *old_v, unsigned int *new_v) { if (scanf(" %u %o %o", offset, old_v, new_v) != 3) *offset = -1; *offset -= 1; } int main(int argc, char *argv[]) { FILE *fp1, *fp2; unsigned int i, c; unsigned int offset, old_v, new_v; if (argc < 3) { fprintf(stderr, "Usage: %s INFILE OUTFILE < PATCHFILE\n", argv[0]); exit(EXIT_FAILURE); } fp1 = fopen(argv[1], "rb"); if (!fp1) { fprintf(stderr, "Cannot open the input file.\n"); exit(EXIT_FAILURE); } fp2 = fopen(argv[2], "wb"); if (!fp2) { fprintf(stderr, "Cannot open the output file.\n"); exit(EXIT_FAILURE); } i = 0; next_patch_line(&offset, &old_v, &new_v); while ((c = fgetc(fp1)) != EOF) { if (i == offset) { if (c != old_v) { fprintf(stderr, "Wrong input file.\n"); exit(EXIT_FAILURE); } c = new_v; next_patch_line(&offset, &old_v, &new_v); } fputc(c, fp2); i++; } return EXIT_SUCCESS; }