aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2019-07-11 15:23:31 -0700
committerRob Landley <rob@landley.net>2019-07-12 13:19:10 -0500
commit36b1b7d1f8921e948b58a1c3fb709c1234ff90c7 (patch)
tree3b9a381f43a57a5fddaa627aa824de94f882f6b3
parenta7b8b772dee4f159028e3b998454d7466fbbc88f (diff)
downloadtoybox-36b1b7d1f8921e948b58a1c3fb709c1234ff90c7.tar.gz
diff: implement --strip-trailing-cr.
Used by some ART tests and also some LLVM tests. (The motivating example is the latter, but I noticed the former when looking for other users.) Bug: http://b/137298656
-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