shithub: riscv

Download patch

ref: c4c072ae97702e4e209b513b335b3bae5447b8b3
parent: 0123946b8df36a0215861b66f4fdba42e3c2dc49
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Sep 21 12:56:17 EDT 2023

diff: generate no-newline directives

when we have a file that ends without a newline,
we now generate a directive to mark it.

--- a/sys/src/cmd/diff/diffio.c
+++ b/sys/src/cmd/diff/diffio.c
@@ -22,9 +22,9 @@
 				return -1;
 			break;
 		}
+		*p++ = c;
 		if (c == '\n')
 			break;
-		*p++ = c;
 	} while (p < e);
 	*p = 0;
 	if (c != '\n' && c >= 0) {
@@ -180,12 +180,12 @@
 	d->ixnew[0] = 0;
 	for (f = t = 1; f < d->len[0]; f++) {
 		flen = readline(bf, fbuf);
-		d->ixold[f] = d->ixold[f-1] + flen + 1;		/* ftell(bf) */
+		d->ixold[f] = d->ixold[f-1] + flen;		/* ftell(bf) */
 		if (d->J[f] == 0)
 			continue;
 		do {
 			tlen = readline(bt, tbuf);
-			d->ixnew[t] = d->ixnew[t-1] + tlen + 1;	/* ftell(bt) */
+			d->ixnew[t] = d->ixnew[t-1] + tlen;	/* ftell(bt) */
 		} while (t++ < d->J[f]);
 		if (bflag) {
 			flen = squishspace(fbuf);
@@ -196,7 +196,7 @@
 	}
 	while (t < d->len[1]) {
 		tlen = readline(bt, tbuf);
-		d->ixnew[t] = d->ixnew[t-1] + tlen + 1;	/* fseek(bt) */
+		d->ixnew[t] = d->ixnew[t-1] + tlen;	/* fseek(bt) */
 		t++;
 	}
 }
@@ -213,7 +213,7 @@
 fetch(Diff *d, long *f, int a, int b, Biobuf *bp, char *s)
 {
 	char buf[MAXLINELEN];
-	int maxb;
+	int maxb, len;
 
 	if(a <= 1)
 		a = 1;
@@ -227,8 +227,12 @@
 		return;
 	Bseek(bp, f[a-1], 0);
 	while (a++ <= b) {
-		readline(bp, buf);
-		Bprint(&stdout, "%s%s\n", s, buf);
+		len = readline(bp, buf);
+		if(len == 0 || buf[len-1] != '\n'){
+			Bprint(&stdout, "%s%s\n", s, buf);
+			Bprint(&stdout, "\\ No newline at end of file\n");
+		}else
+			Bprint(&stdout, "%s%s", s, buf);
 	}
 }
 
--- a/sys/src/cmd/diff/test/diff-t6.expected
+++ b/sys/src/cmd/diff/test/diff-t6.expected
@@ -5,4 +5,5 @@
  version of the code.  With bug reports, please try to ensure that
  enough information to reproduce the problem is enclosed, and if a
 -known fix for it exists, include that as well.
+\ No newline at end of file
 +known fix for it exists, include that as well.
--- a/sys/src/cmd/diff/test/diff-t7.expected
+++ b/sys/src/cmd/diff/test/diff-t7.expected
@@ -6,3 +6,4 @@
  enough information to reproduce the problem is enclosed, and if a
 -known fix for it exists, include that as well.
 +known fix for it exists, include that as well.
+\ No newline at end of file
--- a/sys/src/cmd/diff/test/diff.rc
+++ b/sys/src/cmd/diff/test/diff.rc
@@ -3,7 +3,7 @@
 # tests with no line endings
 # are broken; fix them later
 # tests=`{seq 15}
-tests=(1 2 3 4 5 11 12 13 15)
+tests=(1 2 3 4 5 6 7 11 12 13 15)
 for(t in $tests){
 	echo ../$O.diff -u diff-t$t.1 diff-t$t.2
 	../$O.diff -u diff-t$t.1 diff-t$t.2 > diff-t$t.out