aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2026-02-28 18:15:23 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2026-02-28 18:15:23 +0000
commitc2dee7786bf32cb66cedec226e42e79e06457c51 (patch)
tree073ad14308db6c04daaf75c3904aff2501c97176
parent05ca4837a3e9a413aabcf005abb14fff35088476 (diff)
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
-rw-r--r--usr.bin/diff3/diff3.c48
1 files changed, 19 insertions, 29 deletions
diff --git a/usr.bin/diff3/diff3.c b/usr.bin/diff3/diff3.c
index 9a01951e60ab..c33e3275d006 100644
--- a/usr.bin/diff3/diff3.c
+++ b/usr.bin/diff3/diff3.c
@@ -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);
}