diff3: Code cleanup

* Order includes.

* Turn the diff type into an enum.

* Turn an unreachable error message into an assertion.

* Remove unused debugging code.

* Remove some dead assignments.

* Consistently use 1 instead of EXIT_FAILURE.

* Turn a return from main() into an exit().

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	bapt
Differential Revision:	https://reviews.freebsd.org/D55517
This commit is contained in:
Dag-Erling Smørgrav
2026-02-28 19:15:23 +01:00
parent 05ca4837a3
commit c2dee7786b
+19 -29
View File
@@ -62,19 +62,20 @@
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/capsicum.h>
#include <sys/procdesc.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <capsicum_helpers.h>
#include <ctype.h>
#include <err.h>
#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
@@ -87,14 +88,15 @@ struct range {
int to;
};
enum difftype {
DIFF_NONE,
DIFF_TYPE1,
DIFF_TYPE2,
DIFF_TYPE3,
};
struct diff {
#define DIFF_TYPE1 1
#define DIFF_TYPE2 2
#define DIFF_TYPE3 3
int type;
#if DEBUG
char *line;
#endif /* DEBUG */
enum difftype type;
/* Ranges as lines */
struct range old;
@@ -137,7 +139,7 @@ static const char *newmark = ">>>>>>>";
static const char *divider = "=======";
static bool duplicate(struct range *, struct range *);
static int edit(struct diff *, bool, int, int);
static int edit(struct diff *, bool, int, enum difftype);
static char *getchange(FILE *);
static char *get_line(FILE *, size_t *);
static int readin(int fd, struct diff **);
@@ -256,9 +258,6 @@ readin(int fd, struct diff **dd)
for (i = 0; (p = getchange(f)) != NULL; i++) {
if ((size_t)i >= szchanges - 1)
increase();
#if DEBUG
(*dd)[i].line = strdup(p);
#endif /* DEBUG */
a = b = strtoi(p, &p);
if (*p == ',')
@@ -565,7 +564,7 @@ skip(int i, int from, const char *pr)
for (n = 0; cline[i] < from - 1; n += j) {
if ((line = get_line(fp[i], &j)) == NULL)
errx(EXIT_FAILURE, "logic error");
errx(1, "logic error");
if (pr != NULL)
printf("%s%s", Tflag == 1 ? "\t" : pr, line);
cline[i]++;
@@ -596,7 +595,7 @@ duplicate(struct range *r1, struct range *r2)
if (c == -1 && d == -1)
break;
if (c == -1 || d == -1)
errx(EXIT_FAILURE, "logic error");
errx(1, "logic error");
nchar++;
if (c != d) {
repos(nchar);
@@ -621,7 +620,7 @@ repos(int nchar)
* collect an editing script for later regurgitation
*/
static int
edit(struct diff *diff, bool dup, int j, int difftype)
edit(struct diff *diff, bool dup, int j, enum difftype difftype)
{
if (!(eflag == EFLAG_UNMERGED ||
(!dup && eflag == EFLAG_OVERLAP ) ||
@@ -634,10 +633,6 @@ edit(struct diff *diff, bool dup, int j, int difftype)
overlapcnt++;
de[j].type = difftype;
#if DEBUG
de[j].line = strdup(diff->line);
#endif /* DEBUG */
de[j].old.from = diff->old.from;
de[j].old.to = diff->old.to;
de[j].new.from = diff->new.from;
@@ -657,7 +652,7 @@ printrange(FILE *p, struct range *r)
return;
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.
@@ -872,8 +867,7 @@ mergescript(int i, int f1f3delta)
}
break;
default:
printf("Error: Unhandled diff type - exiting\n");
exit(EXIT_FAILURE);
__assert_unreachable();
}
if (de[n].type == DIFF_TYPE2)
@@ -886,11 +880,7 @@ mergescript(int i, int f1f3delta)
* Print from the final range to the end of 'myfile'. Any deletions or
* additions to this file should have been handled by now.
*/
new = &de[n-1].new;
old = &de[n-1].old;
r.from -= f1f3delta;
r.to = INT_MAX;
printrange(fp[2], &r);
exit(overlapcnt > 0);
@@ -1109,5 +1099,5 @@ main(int argc, char **argv)
merge(m, n);
return (EXIT_SUCCESS);
exit(0);
}