Commit Graph

111 Commits

Author SHA1 Message Date
Kyle Evans
54d65fdd56 diff: restyle loop a bit
This is a bit more readable, and this loop is probably unlikely to gain
any `continue` or `break`s.

Suggested by:	pstef
Differential Revision:	https://reviews.freebsd.org/D37676
2022-12-13 19:31:21 -06:00
Kyle Evans
8bf187f35b diff: fix side-by-side output with tabbed input
The previous logic conflated some things... in this block:
- j: input characters rendered so far
- nc: number of characters in the line
- col: columns rendered so far
- hw: column width ((h)ard (w)idth?)

Comparing j to hw or col to nc are naturally wrong, as col and hw are
limits on their respective counters and nc is already brought down to hw
if the input line should be truncated to start with.

Right now, we end up easily truncating lines with tabs in them as we
count each tab for $tabwidth lines in the input line, but we really
should only be accounting for them in the column count.  The problem is
most easily demonstrated by the two input files added for the tests,
the two tabbed lines lose at least a word or two even though there's
plenty of space left in the row for each side.

Reviewed by:	bapt, pstef
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D37676
2022-12-13 19:31:21 -06:00
John Baldwin
6100374ccf diff: Don't (ab)use sprintf() as a kind of strcat().
Previously print_header() used sprintf() of a buffer to itself as a
kind of string builder but without checking for overflows.  This
raised -Wformat-truncation and -Wrestrict warnings in GCC.  Instead,
just conditionally print the new timestamp fields after the initial
strftime()-formatted string.  While here, use sizeof(buf) with
strftime() rather than a magic number.

Reviewed by:	bapt
Differential Revision:	https://reviews.freebsd.org/D36814
2022-11-15 19:17:36 -08:00
John Baldwin
4e0771714d diff: Don't treat null characters like carriage returns in readhash().
The implicit fall-through in the !D_FORCEASCII case caused null
characters to be treated as carriage returns honoring the D_STRIPCR,
D_FOLDBLANKS, and D_IGNOREBLANKS flags.

Reported by:	GCC -Wimplicit-fallthrough
Reviewed by:	bapt
Fixes:	 	3cbf98e2be diff: read whole files to determine if they are ASCII text
Differential Revision:	https://reviews.freebsd.org/D36813
2022-11-15 19:16:50 -08:00
John Baldwin
3736b2dd32 diff: Fix a use after free as well as a memory leak in change().
When -B or -I are used, change() evaluates the lines in a hunk to
determine if it is a hunk that should be ignored.  It does this by
reading each candidate line into a mallocated buffer via preadline()
and then calling ignoreline().  Previously the buffer was freed as a
side effect of ignoreline_pattern() called from ignoreline().
However, if only -B was specified, then ignoreline_pattern() was not
called and the lines were leaked.  If both options were specified,
then ignoreline_pattern() was called before checking for a blank line
so that the second check was a use after free.

To fix, pull the free() out of ignoreline_pattern() and instead do it
up in change() so that is paired with preadline().

While here, simplify ignoreline() by checking for the -B and -I cases
individually without a separate clause for when both are set.  Also,
do the cheaper check (-B) first, and remove a false comment (this
function is only called if at least one of -I or -B are specified).

Reviewed by:	emaste
Reported by:	GCC 12 -Wuse-after-free
Differential Revision:	https://reviews.freebsd.org/D36822
2022-10-03 16:10:43 -07:00
Kyle Evans
efba49ee5a diff: tests: loosen up requirements for report_identical
This test cannot run without an unprivileged_user being specified
anyways, so just run as the unprivileged user.  Revoking read permisions
works just as well if you're guaranteed non-root.

Reviewed by:	pstef
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D34950
2022-04-18 23:04:13 -05:00
Tom Jones
93b7af1d85 diff: Re-add void argument to usage
Previous commit (8cf449db88)
unintentionally dropped the 'void' argument to usage, reinstate it.

Sponsored by:	Klara Inc.
2022-03-10 16:24:02 +00:00
Tom Jones
8cf449db88 diff: add support for --help and --version
Add support for --help and --version to be compatible with gnu diff.

gnu diff --help writes to stdout, do the same to be compatible

Reviewed by:	bapt, pstef, debrup, Pau Amma
Sponsored by:	Klara Inc.
Differential Revision:	https://reviews.freebsd.org/D34508
2022-03-10 16:17:37 +00:00
Tom Jones
7e64519a47 diff: set cflag when passed to diff
In b5541f456d when flags were converted to
be boolean, the setting of cflag as an integer was removed, but no
boolean set was added.

This effects the output format of dates, but the context type was set to
D_CONTEXT so other functionality worked.

Reviewed by:	bapt
Sponsored by:	Klara Inc.
Differential Revision:	https://reviews.freebsd.org/D34481
2022-03-09 09:32:14 +00:00
Dimitry Andric
f7d6e97e4b Fix indentation in usr.bin/diff/pr.c
In commit 6fa5bf0832 the pr(1) related code in diff was moved around,
but some part of the indentation was messed up, and one line was
duplicated. Remove the duplicated line, and fix up the indentation.

Reviewed by:	bapt
MFC after:	3 days
Differential Revision: https://reviews.freebsd.org/D34398
2022-03-04 20:46:56 +01:00
Li-Wen Hsu
adce4585ca
Revert "Temporarily skip usr.bin.diff.diff_test.functionname in CI"
This reverts commit 85eeb6ea62.

The issue has been fixed by 4be7d087c2.

PR:		262086
2022-03-02 05:37:25 +08:00
Tom Jones
4be7d087c2 diff: Use start of change when searching for function
Use the start of change when searching for a function rather than the
start of the context. In short functions if this could result in search
for the function name starting from before the function definition.

PR:		262086
Reviewed by:	bapt, mckusick, mhorne
Sponsored by:	Klara Inc.
Differential Revision:	https://reviews.freebsd.org/D34328
2022-03-01 13:27:21 +00:00
Li-Wen Hsu
85eeb6ea62
Temporarily skip usr.bin.diff.diff_test.functionname in CI
PR:		262086
Sponsored by:	The FreeBSD Foundation
2022-02-21 16:31:43 +08:00
Tom Jones
3931c072c6 diff: fix failed compare when ignoring file case
With --ignore-file-name-case we need to compare files regardless of
case. We need to propigate both names down to diffit so we can look up
the correct file when the names differ based on case, otherwise we try
to look up the file using the case from the a tree which might not be
discoverable if its case is different in the b tree.

Reviewed by:	bapt
Sponsored by:	Klara Inc.
Differential Revision:	https://reviews.freebsd.org/D34201
2022-02-18 15:51:38 +00:00
Tom Jones
cffebd1e8f Remove surplus check for which diff is being tested
Must have left this when I was testing the test invocation for
8f79bd9b85

Pointy hat:	thj
Sponsored by:	Klara Inc.
2022-02-18 15:47:46 +00:00
Tom Jones
f4be3645a1 diff: add --no-dereference flag
When diffing files and directories, don't follow symbolic links, instead
compare where the links point to.

Reviewed by:	bapt
Sponsored by:	Klara Inc.
Differential Revision:	https://reviews.freebsd.org/D34203
2022-02-18 15:17:13 +00:00
Tom Jones
8f79bd9b85 diff: Detect Objective-C methods
When searching back for function definitions, consider lines starting
with '+' and '-', this allows us to pick up Objective-C methods as well
as C style function definitions.

Reviewed by:	bapt
Sponsored by:	Klara Inc.
Differential Revision:	https://reviews.freebsd.org/D34202
2022-02-18 15:09:57 +00:00
Mariusz Zaborski
824bbb9a40 diff: consider two files with same inodes as identical
Obtained from:	OpenBSD
MFC after:	1 week
2021-10-07 17:07:00 +02:00
Piotr Pawel Stefaniak
12061d2626 diff: link with libm for sqrt()
Reported by:	Jenkins
Fixes:		bcf2e78dc4
2021-09-16 09:31:44 +02:00
Piotr Pawel Stefaniak
e51aabf8cb diff: implement option -F (--show-function-line)
With unified and context diffs, show the last line that matches the
provided pattern before the context.

Reviewed by:	bapt
Differential Revision:	https://reviews.freebsd.org/D31714
2021-09-16 01:46:44 +02:00
Cameron Katri
f38702e5a5 diff(1): Add --color support
Adds a --color flag to diff(1) that supports the same options as GNU's
diff(1). The colors are customizable with the env var DIFFCOLORS in
a format similar to grep(1)'s GREPCOLORS. An example would be 04;36:41
for additions to be underlined light blue, and deletions have a red
background.

Differential Revision:	https://reviews.freebsd.org/D30545
2021-09-16 01:46:44 +02:00
Piotr Pawel Stefaniak
7760b85414 diff: decrease indent level
An upcoming change will add more code in the loop.
2021-09-16 01:46:44 +02:00
Piotr Pawel Stefaniak
2171b2cbe0 diff: avoid applying offsets to null pointer
This was the only instance of undefined behavior I could find so far.
2021-09-16 01:46:44 +02:00
Piotr Pawel Stefaniak
bcf2e78dc4 diff: replace isqrt() with sqrt()
Remove cruft and use a system-provided and maintained function instead.
2021-09-16 01:46:43 +02:00
Piotr Pawel Stefaniak
e43df07e37 diff: move functions around and reduce their visibility
Most of them become static. There will be more such functions added in
upcoming commits, so they would be inconsistent with existing code.
Improve the existing code instead of reinforcing the unwanted pattern.
2021-09-16 01:36:41 +02:00
Piotr Pawel Stefaniak
b5541f456d diff: convert boolean flag variables to bool
There will be more boolean flags added in upcoming commits and they
would have to be stored in ints in order to be consistent with existing
code. Change the existing code to use the bool type.
2021-09-16 01:36:41 +02:00
Piotr Pawel Stefaniak
0358202111 diff: improve code style
Reflow comments, strip trailing space, improve wrapping of lines.
2021-09-16 01:36:41 +02:00
Piotr Pawel Stefaniak
3cbf98e2be diff: read whole files to determine if they are ASCII text
Before this change, only the first BUFSIZE bytes were checked.

Reviewed by:	bapt (previous version)
Differential Revision:	https://reviews.freebsd.org/D31639
2021-08-23 16:09:05 +02:00
Piotr Pawel Stefaniak
af2f016431 diff: don't output carriage returns that were stripped on input
--strip-trailing-cr worked as intended for comparison between files,
but the characters were still present in final output.
2021-08-23 07:04:28 +02:00
Alex Richardson
c78f449d85 usr.bin/diff: fix UBSan error in readhash
UBSan complains about the `sum = sum * 127 + chrtran(t);` line below since
that can overflow an `int`. Use `unsigned int` instead to ensure that
overflow is well-defined.

Reviewed By:	imp
MFC after:	1 week
Differential Revision: https://reviews.freebsd.org/D31075
2021-07-06 12:16:40 +01:00
Mark Johnston
6fa0b2893f diff tests: Require root for report_identical
This test switches users and either hangs or fails when run as an
unprivileged user.

MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
2021-06-21 11:07:03 -04:00
Olivier Cochard
b67df8d7c2 diff: Use unprivileged_user with report_identical test
Approved by:	bapt
Sponsored by:	Netflix
Differential Revision:	https://reviews.freebsd.org/D28466
2021-02-03 17:18:59 +01:00
Baptiste Daroussin
c69047ca75 Revert "diff: eliminate a useless lseek"
This changes breaks when one of the files is stdin

This reverts commit fa977a3b2b.

Reported by:	olivier
2021-02-02 10:08:25 +01:00
Baptiste Daroussin
7a57c9cb5a diff: eleminitate useless macros
The diff_output was not bringing any values but was obfuscating
the code.
2021-01-27 12:29:33 +01:00
Baptiste Daroussin
e43239f514 diff: simplify the hash functions
Instead of 3 different complex case they have all been folded into a
simple on based on switch
2021-01-27 12:28:26 +01:00
Baptiste Daroussin
e52546a3a7 diff: fix typo in a comment 2021-01-27 12:18:46 +01:00
Baptiste Daroussin
931ad51808 diff: remove stalled entries in headers 2021-01-27 12:18:45 +01:00
Baptiste Daroussin
15abb23286 diff: eliminate space at end of line
No functionnal changes
2021-01-27 12:18:45 +01:00
Baptiste Daroussin
fa977a3b2b diff: eliminate a useless lseek
fdopen with the "r" already position the stream at the beginning
of the file.
2021-01-27 12:18:45 +01:00
Baptiste Daroussin
c440e7870a diff: add a test case about the non regular file support 2021-01-27 12:18:45 +01:00
Jamie Landeg-Jones
fefb3c46a8 diff: fix incorrectly displaying files as duplicates
When diff hits certain access errors, function diffreg() shows the error
message, and then returns to the calling function, which calls
print_status() with the return value.

However, in these cases, the return value isn't changed from the initial
default value of D_SAME.

Normally, print_status() with a value of D_SAME does nothing, so this
works out ok, however, if the "-s" flag is set, a message is displayed
showing identicality:

case D_SAME:
                if (sflag)
                        printf("Files %s%s and %s%s are identical\n",                                                                                                       path1, entry, path2, entry);
                break;

This then produces such results as:

% diff  -s /COPYRIGHT /var/run/rpcbind.sock
diff: /var/run/rpcbind.sock: Operation not supported
Files /COPYRIGHT and /var/run/rpcbind.sock are identical

% diff  -s /COPYRIGHT /etc/master.passwd
diff: /etc/master.passwd: Permission denied
Files /COPYRIGHT and /etc/master.passwd are identical

Create a D_ERROR status which is returned in such cases, and
print_status() then deals with that status seperately from D_SAME

PR:		252614
MFC after:	1 week
2021-01-25 20:38:18 +01:00
Baptiste Daroussin
13860e71eb diff: add a test case for failed -s option 2021-01-25 20:37:58 +01:00
Ed Maste
12a8d3027d diff: honour flags with -q
Previously -q (just print a line when files differ) ignored flags like
-w (ignore whitespace).  Avoid the D_BRIEF short-circuit when flags are
in effect.

PR:		252515
Reported by:	Scott Aitken
Reviewed by:	kevans
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D28064
2021-01-09 13:34:06 -05:00
Kyle Evans
b0d30b27f0 diff: don't force the format to 'context' with -p immediately
Instead, leave the fomat as unspecified (if it hasn't been) and use the
-p flag as a hint to 'context' if no other formatting option is specified.

This fixes `diff -purw`, used frequently by emaste, and matches the behavior
of its GNU counterpart.

PR:		250015
Reviewed by:	emaste
MFC after:	1 week
2020-10-27 12:27:26 +00:00
Baptiste Daroussin
fb0d1c6980 diff: always properly kill pr(1)
When diff is invoked with -l it will spawn the pr(1) program.
In some circumpstances the pr(1) was not properly killed when diff program
exits.

Submitted by:	Bret Ketchum
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D26232
2020-09-01 14:52:39 +00:00
Kyle Evans
ac05de1788 diff: fix side_by_side after r363679
It's currently unclear to me how this could have worked previously; \n here
is not a literal newline but actual '\' 'n', and was getting passed to the
underlying regex engine as such. regex(3) does not translate this to a
newline, and this became an error because we don't really allow escaping
of arbitrary ordinary characters anymore.

Run the pattern strings through printf to make sure we're dealing with real
newlines before passing them through to atf_check, which ultimately feeds
them directly to regcomp(3).

This fix is different than that will be needed for sed, in that this is the
proper way to inject newlines into search strings as long as regex(3)
won't combine \ + n as folks might expect.

Reported by:	Jenkins via lwhsu
MFC after:	1 week
2020-07-30 13:33:45 +00:00
Fernando Apesteguía
204fa3b066 diff(1): Add EXAMPLES section
Add examples for -r, -u, -N, -x, -q and -I

Approved by:	0mp@
Differential Revision:	https://reviews.freebsd.org/D25345
2020-06-19 06:48:16 +00:00
Baptiste Daroussin
8ccef19395 Document long version of -b option
PR:		234195
Submitted by:	Fehmi Noyan Isi <fnoyanisi@yahoo.com>
Reported by:	Andras Farkas <deepbluemistake@gmail.com>
MFC after:	3 days
2020-06-01 09:15:15 +00:00
Baptiste Daroussin
2816b9998c diff: restore compatibility with GNU diff regarding -N option
When -N is used the missing files are treated as empty.

PR:		233402
Submitted by:	Fehmi Noyan Isi <fnoyanisi@yahoo.com>
Reported by:	Roman Neuhauser <roman@sigpipe.cz>
MFC after:	3 days
Differential Revision:	D25081
2020-06-01 09:09:36 +00:00
Baptiste Daroussin
f096ed981f Restore compatibility with GNU diff regarding --label
Various options to "diff(1)" show filenames, and traditionally make use of the
"--label" parameter, if set.

Restore this behaviour in BSD diff.

While here add a regression test

PR:		244533
Submitted by:	Jamie Landeg-Jones <jamie@catflap.org>
MFC after:	3 days
2020-06-01 09:01:13 +00:00