Commit graph

31 commits

Author SHA1 Message Date
Dag-Erling Smørgrav eea5f8d408 diff: Sort headers.
MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D45078
2024-05-03 17:03:50 +02:00
Martin Tournoij 9744821765 diff: use getline() instead of fgetln()
This replaces fgetln() with getline(). The main reason for this is
portability, making things easier for people who want to compile these
tools on non-FreeBSD systems.

I appreciate that's probably not the top concern for FreeBSD base tools,
but fgetln() is impossible to port to most platforms, as concurrent
access is essentially impossible to implement fully correct without the
line buffer on the FILE struct. Other than this, many generic FreeBSD
tools compile fairly cleanly on Linux with a few small changes.

Most uses of fgetln() pre-date getline() support (added in 2009 with
69099ba2ec), and there's been some previous patches (ee3ca711a8
8c98e6b1a7 1a2a4fc8ce) for other tools.

Obtained from:	https://github.com/dcantrell/bsdutils and
              	https://github.com/chimera-linux/chimerautils
Signed-off-by: Martin Tournoij <martin@arp242.net>
Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/893
2024-04-19 15:52:21 -06:00
Dag-Erling Smørgrav d9a9f23d0b diff: Integrate libdiff from OpenBSD GoT.
This adds support for two new diff algorithms, Myers diff and Patience
diff.

These algorithms perform a different form of search compared to the
classic Stone algorithm and support escapes when worst case scenarios
are encountered.

Add the -A flag to allow selection of the algorithm, but default to
using the new Myers diff implementation.

The libdiff implementation currently only supports a subset of input and
output options supported by diff.  When these options are used, but the
algorithm is not selected, automatically fallback to the classic Stone
algorithm until support for these modes can be added.

Based on work originally done by thj@ with contributions from kevans@.

Sponsored by:	Klara, Inc.
Reviewed by:	thj
Differential Revision:	https://reviews.freebsd.org/D44302
2024-03-27 11:03:33 +01:00
Dag-Erling Smørgrav 53de23f4d1 diff: Fix --expand-tabs and --side-by-side.
* Overhaul column width and padding calculation.
* Rewrite print_space() so it is now a) correct and b) understandable.
* Rewrite tab expansion in fetch() for the same reason.

This brings us in line with GNU diff for all cases I could think of.

Sponsored by:	Klara, Inc.
Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D44014
2024-02-26 19:08:06 +01:00
Warner Losh 1d386b48a5 Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:42 -06: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
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
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 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
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
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 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
Kyle Evans 67c907653e diff(1): don't reject specifying the same format multiple times
This may happen, for instance, if one happens to have an alias of diff to
diff -up and attempts to specify the amount of context on top of that.

Aliases like this may cause other problems, but if they're really not ever
generating non-unified diffs then we should at least not break that
use-case.

In addition, we'll now pick up a format mismatch if -p is specified with
!contextual && !unified && !unset.

Fix up a small trailing whitespace nit in the tests while we're here, and
add tests to make sure that we can double up all the formatting options.

Reported by:	jbeich
MFC after:	3 days
2020-04-28 14:33:33 +00:00
Kyle Evans db9cccfbb1 diff(1): reject conflicting formatting options
This matches GNU diff(1) behavior and, more importantly, eliminates any
source of confusion if multiple formatting options are specified.

Note that the committed diff differs slightly from the submitted: I've
modified it so that we initialize diff_format to something that isn't an
accepted format option so that we can also reject --normal -c and -c
--normal, which would've otherwise been accepted because the default was
--normal. After option parsing we default it to D_NORMAL if it's still
unset.

PR:		243975
Submitted by:	fehmi noyan isi
MFC after:	1 week
2020-04-20 16:14:44 +00:00
Kyle Evans 43fbd65451 diff: fix segfault with --tabsize and no/malformed argument
--tabsize was previously listed as optional_argument, but didn't account for
the optionality of it in the argument handling. This is irrelevant -- the
manpage doesn't indicate that the argument is optional, and indeed there's
no clear interpretation of omitting the argument because there's no other
side effect of --tabsize.

The "malformed" argument part of the header on this message is simply
referring to usage like this:

% diff --tabsize 4 A B

With an optional_argument, the argument must be attached to the parameter
directly (e.g. --tabsize=4), so the argument is effectively NULL with the
above invocation as if no argument had been passed.

PR:		243974
Submitted by:	fehmi noyan isi <fnoyanisi yahoo com> (diff.c portion)
MFC after:	3 days
2020-02-13 20:23:55 +00:00
Baptiste Daroussin 64793e748c diff: implement -y (--side-by-side) along with -W and --suppress-common-lines
PR:		219933
Submitted by:	fehmi noyan isi <fnoyanisi@yahoo.com>
MFC after:	3 weeks
2020-02-07 10:17:13 +00:00
Baptiste Daroussin 12d9c0dc55 When system calls indicate an error they return -1, not some arbitrary
value < 0.  errno is only updated in this case.

Obtained from:	OpenBSD
MFC after:	3 days
2020-01-14 08:22:28 +00:00
Kyle Evans e68edb8cf0 diff(1): Implement -B/--ignore-blank-lines
As noted by cem in r338035, coccinelle invokes diff(1) with the -B flag.
This was not previously implemented here, so one was forced to create a link
for GNU diff to /usr/local/bin/diff

Implement the -B flag and add some primitive tests for it. It is implemented
in the same fashion that -I is implemented; each chunk's lines are scanned,
and if a non-blank line is encountered then the chunk will be output.
Otherwise, it's skipped.

MFC after:	2 weeks
2018-08-19 03:57:20 +00:00
John Baldwin ea67c7a015 Add -H as an alias for --speed-large-file to match GNU diff.
This is undocumented to match GNU diff where -H is also undocumented.
Some existing software (such as kompare) uses this option by default.

Reviewed by:	emaste, rpokala
Differential Revision:	https://reviews.freebsd.org/D11022
2017-06-02 03:25:59 +00:00
Baptiste Daroussin fddcb7b82f Implement a basic --changed-group-format
etcupdate(8) requires that option, while GNU diff supports many more variation
of that options, their behaviour beside the simple verion implemented here are
quite inconsistent as such I do not plan to implement those.

The only special keyword supported by this implementation are: %< and %>
%= is not implemented as the documentation of GNU diff says: common lines, but
it actually when tested print the changes from the first file
2017-04-20 14:22:33 +00:00
Baptiste Daroussin 7752043c9d Clean up headers declaration 2017-04-15 13:57:35 +00:00
Baptiste Daroussin a077708d35 add a stub --speed-large-files for compatibility with GNU diff
There is no intention to implement it, but lots of scripts/tools using
diff(1) passes GNU diff option
2017-04-08 08:46:02 +00:00
Baptiste Daroussin 5c8e56f615 Use strndup(3) instead of malloc + memcpy 2017-03-22 21:52:22 +00:00
Baptiste Daroussin 901a475b7a Implement a stub --horizon-lines=NUM for compatibility with GNU diff3
some options of GNU diff3 would call diff with --horizon-lines, rcs is depending
on that.

Reported by:	antoine
2017-03-12 03:49:05 +00:00
Baptiste Daroussin 3bbe3f672e Import diff from OpenBSD and remove GNU diff
Some of the modifications from the previous summer of code has been integrated
Modification for compatibility with GNU diff output has been added

Main difference with OpenBSD:
Implement multiple GNU diff options:
* --ignore-file-name-case
* --no-ignore-file-name-case
* --normal
* --tabsize
* --strip-trailing-cr
Make diff -p compatible with GNU diff
Implement diff -l
Make diff -r compatible with GNU diff

Capsicumize diffing 2 regular files
Add a simple test suite

Approved by:	AsiaBSDcon devsummit
Obtained from:	OpenBSD, GSoC
Relnotes:	yes
2017-03-11 05:01:29 +00:00