aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/diff.test5
-rw-r--r--toys/pending/diff.c50
2 files changed, 36 insertions, 19 deletions
diff --git a/tests/diff.test b/tests/diff.test
index 98477587..f78eaa66 100644
--- a/tests/diff.test
+++ b/tests/diff.test
@@ -33,3 +33,8 @@ echo foo > tree1/file
echo food > tree2/file
testing "-r" "diff -r -L tree1/file -L tree2/file tree1 tree2 |tee out" "$expected" "" ""
+
+echo -e "hello\r\nworld\r\n"> a
+echo -e "hello\nworld\n"> b
+testing "--strip-trailing-cr off" "diff -q a b" "Files a and b differ\n" "" ""
+testing "--strip-trailing-cr on" "diff -u --strip-trailing-cr a b" "" "" ""
diff --git a/toys/pending/diff.c b/toys/pending/diff.c
index d865e8df..2d13d977 100644
--- a/toys/pending/diff.c
+++ b/toys/pending/diff.c
@@ -5,7 +5,7 @@
*
* See: http://cm.bell-labs.com/cm/cs/cstr/41.pdf
-USE_DIFF(NEWTOY(diff, "<2>2(color)B(ignore-blank-lines)d(minimal)b(ignore-space-change)ut(expand-tabs)w(ignore-all-space)i(ignore-case)T(initial-tab)s(report-identical-files)q(brief)a(text)L(label)*S(starting-file):N(new-file)r(recursive)U(unified)#<0=3", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
+USE_DIFF(NEWTOY(diff, "<2>2(color)(strip-trailing-cr)B(ignore-blank-lines)d(minimal)b(ignore-space-change)ut(expand-tabs)w(ignore-all-space)i(ignore-case)T(initial-tab)s(report-identical-files)q(brief)a(text)L(label)*S(starting-file):N(new-file)r(recursive)U(unified)#<0=3", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
config DIFF
bool "diff"
@@ -13,23 +13,25 @@ config DIFF
help
usage: diff [-abBdiNqrTstw] [-L LABEL] [-S FILE] [-U LINES] FILE1 FILE2
- -a Treat all files as text
- -b Ignore changes in the amount of whitespace
- -B Ignore changes whose lines are all blank
- -d Try hard to find a smaller set of changes
- -i Ignore case differences
- -L Use LABEL instead of the filename in the unified header
- -N Treat absent files as empty
- -q Output only whether files differ
- -r Recurse
- -S Start with FILE when comparing directories
- -T Make tabs line up by prefixing a tab when necessary
- -s Report when two files are the same
- -t Expand tabs to spaces in output
- -U Output LINES lines of context
- -w Ignore all whitespace
-
- --color Colored output
+ -a Treat all files as text
+ -b Ignore changes in the amount of whitespace
+ -B Ignore changes whose lines are all blank
+ -d Try hard to find a smaller set of changes
+ -i Ignore case differences
+ -L Use LABEL instead of the filename in the unified header
+ -N Treat absent files as empty
+ -q Output only whether files differ
+ -r Recurse
+ -S Start with FILE when comparing directories
+ -T Make tabs line up by prefixing a tab when necessary
+ -s Report when two files are the same
+ -t Expand tabs to spaces in output
+ -u Unified diff
+ -U Output LINES lines of context
+ -w Ignore all whitespace
+
+ --color Colored output
+ --strip-trailing-cr Strip trailing '\r's from input lines
*/
#define FOR_diff
@@ -196,8 +198,18 @@ static int read_tok(FILE *fp, off_t *off, int tok)
tok |= empty;
while (!(tok & eol)) {
-
t = fgetc(fp);
+
+ if (FLAG(strip_trailing_cr) && t == '\r') {
+ int t2 = fgetc(fp);
+ if (t2 == '\n') {
+ t = t2;
+ if (off) (*off)++;
+ } else {
+ ungetc(t2, fp);
+ }
+ }
+
if (off && t != EOF) *off += 1;
is_space = isspace(t) || (t == EOF);
tok |= (t & (eof + eol)); //set tok eof+eol when t is eof