Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/diff3/diff3.c
| Show First 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | |||||
| * from=to=line after point of insertion for added lines. | * from=to=line after point of insertion for added lines. | ||||
| */ | */ | ||||
| struct range { | struct range { | ||||
| int from; | int from; | ||||
| int to; | int to; | ||||
| }; | }; | ||||
| struct diff { | struct diff { | ||||
| #define DIFF_TYPE1 1 | #define DIFF_TYPE1 1 | ||||
markj: Why introduce DIFF_NONE? | |||||
Done Inline ActionsI don't want DIFF_TYPE1 to be 0 in case a bug lets an uninitialized struct diff through. Adding DIFF_NONE seemed cleaner than manually assigning values. des: I don't want `DIFF_TYPE1` to be 0 in case a bug lets an uninitialized `struct diff` through. | |||||
| #define DIFF_TYPE2 2 | #define DIFF_TYPE2 2 | ||||
| #define DIFF_TYPE3 3 | #define DIFF_TYPE3 3 | ||||
| int type; | int type; | ||||
| #if DEBUG | #if DEBUG | ||||
| char *line; | char *line; | ||||
| #endif /* DEBUG */ | #endif /* DEBUG */ | ||||
| /* Ranges as lines */ | /* Ranges as lines */ | ||||
| ▲ Show 20 Lines • Show All 460 Lines • ▼ Show 20 Lines | |||||
| static int | static int | ||||
| skip(int i, int from, const char *pr) | skip(int i, int from, const char *pr) | ||||
| { | { | ||||
| size_t j, n; | size_t j, n; | ||||
| char *line; | char *line; | ||||
| for (n = 0; cline[i] < from - 1; n += j) { | for (n = 0; cline[i] < from - 1; n += j) { | ||||
| if ((line = get_line(fp[i], &j)) == NULL) | if ((line = get_line(fp[i], &j)) == NULL) | ||||
| errx(EXIT_FAILURE, "logic error"); | errx(1, "logic error"); | ||||
| if (pr != NULL) | if (pr != NULL) | ||||
| printf("%s%s", Tflag == 1 ? "\t" : pr, line); | printf("%s%s", Tflag == 1 ? "\t" : pr, line); | ||||
| cline[i]++; | cline[i]++; | ||||
| } | } | ||||
| return ((int) n); | return ((int) n); | ||||
| } | } | ||||
| /* | /* | ||||
| Show All 14 Lines | duplicate(struct range *r1, struct range *r2) | ||||
| nchar = 0; | nchar = 0; | ||||
| for (nline = 0; nline < r1->to - r1->from; nline++) { | for (nline = 0; nline < r1->to - r1->from; nline++) { | ||||
| do { | do { | ||||
| c = getc(fp[0]); | c = getc(fp[0]); | ||||
| d = getc(fp[1]); | d = getc(fp[1]); | ||||
| if (c == -1 && d == -1) | if (c == -1 && d == -1) | ||||
| break; | break; | ||||
| if (c == -1 || d == -1) | if (c == -1 || d == -1) | ||||
| errx(EXIT_FAILURE, "logic error"); | errx(1, "logic error"); | ||||
| nchar++; | nchar++; | ||||
| if (c != d) { | if (c != d) { | ||||
| repos(nchar); | repos(nchar); | ||||
| return (0); | return (0); | ||||
| } | } | ||||
| } while (c != '\n'); | } while (c != '\n'); | ||||
| } | } | ||||
| repos(nchar); | repos(nchar); | ||||
| ▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | printrange(FILE *p, struct range *r) | ||||
| size_t len = 0; | size_t len = 0; | ||||
| int i = 1; | int i = 1; | ||||
| /* We haven't been asked to print anything */ | /* We haven't been asked to print anything */ | ||||
| if (r->from == r->to) | if (r->from == r->to) | ||||
| return; | return; | ||||
| if (r->from > r->to) | if (r->from > r->to) | ||||
| errx(EXIT_FAILURE, "invalid print range"); | errx(1, "invalid print range"); | ||||
| /* | /* | ||||
| * XXX-THJ: We read through all of the file for each range printed. | * XXX-THJ: We read through all of the file for each range printed. | ||||
| * This duplicates work and will probably impact performance on large | * This duplicates work and will probably impact performance on large | ||||
| * files with lots of ranges. | * files with lots of ranges. | ||||
| */ | */ | ||||
| fseek(p, 0L, SEEK_SET); | fseek(p, 0L, SEEK_SET); | ||||
| while (getline(&line, &len, p) > 0) { | while (getline(&line, &len, p) > 0) { | ||||
| ▲ Show 20 Lines • Show All 198 Lines • ▼ Show 20 Lines | case DIFF_TYPE3: | ||||
| printf("%s\n", divider); | printf("%s\n", divider); | ||||
| printrange(fp[2], new); | printrange(fp[2], new); | ||||
| printf("%s %s\n", newmark, f3mark); | printf("%s %s\n", newmark, f3mark); | ||||
| } | } | ||||
| break; | break; | ||||
| default: | default: | ||||
| printf("Error: Unhandled diff type - exiting\n"); | __unreachable(); | ||||
Done Inline ActionsNote that this is unreachable. Perhaps we should just assert that. des: Note that this is unreachable. Perhaps we should just assert that. | |||||
| exit(EXIT_FAILURE); | |||||
| } | } | ||||
| if (de[n].type == DIFF_TYPE2) | if (de[n].type == DIFF_TYPE2) | ||||
| r.from = new->to + de_delta[n]; | r.from = new->to + de_delta[n]; | ||||
| else | else | ||||
| r.from = old->to; | r.from = old->to; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 219 Lines • ▼ Show 20 Lines | main(int argc, char **argv) | ||||
| m = readin(fd13[0], &d13); | m = readin(fd13[0], &d13); | ||||
| n = readin(fd23[0], &d23); | n = readin(fd23[0], &d23); | ||||
| wait_and_check(pd13); | wait_and_check(pd13); | ||||
| wait_and_check(pd23); | wait_and_check(pd23); | ||||
| merge(m, n); | merge(m, n); | ||||
| return (EXIT_SUCCESS); | exit(0); | ||||
| } | } | ||||
Why introduce DIFF_NONE?