diff --git a/usr.bin/diff3/diff3.1 b/usr.bin/diff3/diff3.1 --- a/usr.bin/diff3/diff3.1 +++ b/usr.bin/diff3/diff3.1 @@ -40,6 +40,7 @@ .Nm diff3 .Op Fl 3aEeiXx .Op Fl Fl diff-program Ar program +.Op Fl Fl strip-trailing-cr .Op Fl L | Fl Fl label Ar label1 .Op Fl L | Fl Fl label Ar label2 .Op Fl L | Fl Fl label Ar label3 @@ -125,6 +126,8 @@ instead of the default .Xr diff 1 to compare files. +.It Fl Fl strip-trailing-cr +Strip trailing carriage return on input files. .El .Pp The diff --git a/usr.bin/diff3/diff3.c b/usr.bin/diff3/diff3.c --- a/usr.bin/diff3/diff3.c +++ b/usr.bin/diff3/diff3.c @@ -264,6 +264,12 @@ if ((len = getline(&buf, &bufsize, b)) < 0) return (NULL); + if (strip_cr && len >= 2 && strcmp("\r\n", &(buf[len - 2])) == 0) { + buf[len - 2] = '\n'; + buf[len - 1] = '\0'; + len--; + } + if (n != NULL) *n = len; @@ -599,7 +605,7 @@ char *labels[] = { NULL, NULL, NULL }; const char *diffprog = DIFF_PATH; char *file1, *file2, *file3; - char *diffargv[6]; + char *diffargv[7]; int diffargc = 0; int fd13[2], fd23[2]; int pd13, pd23; @@ -657,6 +663,7 @@ break; case STRIPCR_OPT: strip_cr = 1; + diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr"); break; } } diff --git a/usr.bin/diff3/tests/1cr.txt b/usr.bin/diff3/tests/1cr.txt new file mode 100644 --- /dev/null +++ b/usr.bin/diff3/tests/1cr.txt @@ -0,0 +1,6 @@ +This is a more complete test +which multiple lines +and a few typ0s to fix +also I plan to add few lines + and to remove +other lines diff --git a/usr.bin/diff3/tests/Makefile b/usr.bin/diff3/tests/Makefile --- a/usr.bin/diff3/tests/Makefile +++ b/usr.bin/diff3/tests/Makefile @@ -6,6 +6,7 @@ ${PACKAGE}FILES+= \ 1.txt \ + 1cr.txt \ 2.txt \ 3.txt \ 1.out \ diff --git a/usr.bin/diff3/tests/diff3_test.sh b/usr.bin/diff3/tests/diff3_test.sh --- a/usr.bin/diff3/tests/diff3_test.sh +++ b/usr.bin/diff3/tests/diff3_test.sh @@ -8,6 +8,9 @@ atf_check -o file:$(atf_get_srcdir)/1.out \ diff3 $(atf_get_srcdir)/1.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt + atf_check -o file:$(atf_get_srcdir)/1.out \ + diff3 --strip-trailing-cr $(atf_get_srcdir)/1cr.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt + atf_check -o file:$(atf_get_srcdir)/2.out \ diff3 -e $(atf_get_srcdir)/1.txt $(atf_get_srcdir)/2.txt $(atf_get_srcdir)/3.txt