From 1a92368aedd6229e298c6d191f7faaaea17b5489 Mon Sep 17 00:00:00 2001
From: mrdudz <mrdudz@users.noreply.github.com>
Date: Wed, 19 Aug 2020 14:50:12 +0200
Subject: [PATCH] rename bdiff.c to isequal.c, make it handle different
 line-endings as equal

---
 test/asm/Makefile  |  4 ++--
 test/bdiff.c       | 28 ------------------------
 test/dasm/Makefile |  4 ++--
 test/isequal.c     | 54 ++++++++++++++++++++++++++++++++++++++++++++++
 test/misc/Makefile |  4 ++--
 test/ref/Makefile  |  4 ++--
 6 files changed, 62 insertions(+), 36 deletions(-)
 delete mode 100644 test/bdiff.c
 create mode 100644 test/isequal.c

diff --git a/test/asm/Makefile b/test/asm/Makefile
index a0825574c..94a925376 100644
--- a/test/asm/Makefile
+++ b/test/asm/Makefile
@@ -24,7 +24,7 @@ CL65 := $(if $(wildcard ../../bin/cl65*),../../bin/cl65,cl65)
 
 WORKDIR = ../../testwrk/asm
 
-DIFF = $(WORKDIR)/bdiff$(EXE)
+DIFF = $(WORKDIR)/isequal$(EXE)
 
 CC = gcc
 CFLAGS = -O2
@@ -44,7 +44,7 @@ all: $(OPCODE_BINS) $(CPUDETECT_BINS)
 $(WORKDIR):
 	$(call MKDIR,$(WORKDIR))
 
-$(DIFF): ../bdiff.c | $(WORKDIR)
+$(DIFF): ../isequal.c | $(WORKDIR)
 	$(CC) $(CFLAGS) -o $@ $<
 
 define OPCODE_template
diff --git a/test/bdiff.c b/test/bdiff.c
deleted file mode 100644
index 797ba4302..000000000
--- a/test/bdiff.c
+++ /dev/null
@@ -1,28 +0,0 @@
-
-// minimal tool to compare two binaries
-
-#include <stdlib.h>
-#include <stdio.h>
-
-int main(int argc, char *argv[])
-{
-    FILE *f1, *f2;
-    if (argc < 3) {
-        return EXIT_FAILURE;
-    }
-    f1 = fopen(argv[1], "rb");
-    f2 = fopen(argv[2], "rb");
-    if ((f1 == NULL) || (f2 == NULL)) {
-        return EXIT_FAILURE;
-    }
-    for(;;) {
-        if (feof(f1) && feof(f2)) {
-            return EXIT_SUCCESS;
-        } else if (feof(f1) || feof(f2)) {
-            return EXIT_FAILURE;
-        }
-        if (fgetc(f1) != fgetc(f2)) {
-            return EXIT_FAILURE;
-        }
-    }
-}
diff --git a/test/dasm/Makefile b/test/dasm/Makefile
index d70711491..faa6b7fa0 100644
--- a/test/dasm/Makefile
+++ b/test/dasm/Makefile
@@ -25,7 +25,7 @@ DA65 := $(if $(wildcard ../../bin/da65*),../../bin/da65,da65)
 
 WORKDIR = ../../testwrk/dasm
 
-DIFF = $(WORKDIR)/bdiff$(EXE)
+DIFF = $(WORKDIR)/isequal$(EXE)
 
 CC = gcc
 CFLAGS = -O2
@@ -44,7 +44,7 @@ all: $(BINS)
 $(WORKDIR):
 	$(call MKDIR,$(WORKDIR))
 
-$(DIFF): ../bdiff.c | $(WORKDIR)
+$(DIFF): ../isequal.c | $(WORKDIR)
 	$(CC) $(CFLAGS) -o $@ $<
 
 define DISASS_template
diff --git a/test/isequal.c b/test/isequal.c
new file mode 100644
index 000000000..b3806c7e4
--- /dev/null
+++ b/test/isequal.c
@@ -0,0 +1,54 @@
+
+// minimal tool to compare two text files
+
+#include <stdlib.h>
+#include <stdio.h>
+
+/* get the next character from FILE and convert commonly used line-endings all
+   into the same value (0x0a, as used on *nix systems)
+
+   recognized values/pairs:
+
+   0x0a (LF)                Linux, macOS
+   0x0d, 0x0a (CR, LF)      Windows, MSDOS, OS/2
+   0x0d (CR)                classic MacOS
+*/
+
+int getnext(FILE *f)
+{
+    int c = fgetc(f);
+    if (c == 0x0d) {
+        if (!feof(f)) {
+            int n = fgetc(f);
+            if (n != 0x0a) {
+                ungetc(n, f);
+            }
+            clearerr(f); /* clears EOF when we did not push back */
+        }
+        return 0x0a;
+    }
+    return c;
+}
+
+int main(int argc, char *argv[])
+{
+    FILE *f1, *f2;
+    if (argc < 3) {
+        return EXIT_FAILURE;
+    }
+    f1 = fopen(argv[1], "rb");
+    f2 = fopen(argv[2], "rb");
+    if ((f1 == NULL) || (f2 == NULL)) {
+        return EXIT_FAILURE;
+    }
+    for(;;) {
+        if (feof(f1) && feof(f2)) {
+            return EXIT_SUCCESS;
+        } else if (feof(f1) || feof(f2)) {
+            return EXIT_FAILURE;
+        }
+        if (getnext(f1) != getnext(f2)) {
+            return EXIT_FAILURE;
+        }
+    }
+}
diff --git a/test/misc/Makefile b/test/misc/Makefile
index 1d98e2d62..55c19704e 100644
--- a/test/misc/Makefile
+++ b/test/misc/Makefile
@@ -37,7 +37,7 @@ WORKDIR = ..$S..$Stestwrk$Smisc
 
 OPTIONS = g O Os Osi Osir Osr Oi Oir Or
 
-DIFF = $(WORKDIR)$Sbdiff$(EXE)
+DIFF = $(WORKDIR)$Sisequal$(EXE)
 
 CC = gcc
 CFLAGS = -O2
@@ -58,7 +58,7 @@ all: $(TESTS)
 $(WORKDIR):
 	$(call MKDIR,$(WORKDIR))
 
-$(DIFF): ../bdiff.c | $(WORKDIR)
+$(DIFF): ../isequal.c | $(WORKDIR)
 	$(CC) $(CFLAGS) -o $@ $<
 
 define PRG_template
diff --git a/test/ref/Makefile b/test/ref/Makefile
index a94f65dd8..f88821f64 100644
--- a/test/ref/Makefile
+++ b/test/ref/Makefile
@@ -35,7 +35,7 @@ WORKDIR = ..$S..$Stestwrk$Sref
 
 OPTIONS = g O Os Osi Osir Osr Oi Oir Or
 
-DIFF = $(WORKDIR)$Sbdiff$(EXE)
+DIFF = $(WORKDIR)$Sisequal$(EXE)
 
 CC = gcc
 CFLAGS = -O2 -Wall -W -Wextra -funsigned-char -fwrapv -fno-strict-overflow
@@ -57,7 +57,7 @@ $(WORKDIR)/%.ref: %.c | $(WORKDIR)
 	$(CC) $(CFLAGS) -o $(WORKDIR)/$*.host $< $(NULLERR)
 	$(WORKDIR)$S$*.host > $@
 
-$(DIFF): ../bdiff.c | $(WORKDIR)
+$(DIFF): ../isequal.c | $(WORKDIR)
 	$(CC) $(CFLAGS) -o $@ $<
 
 # "yaccdbg.c" includes "yacc.c".