Commit graph

160 commits

Author SHA1 Message Date
Baptiste Daroussin
f5d3d35273 diff: use O_CLOEXEC on pipes
This only simplifies the code, no functional changes expected

MFC After:	1 week

(cherry picked from commit c8d40bf8ecc60cc15e3904410db62065ea681fdc)
2026-04-06 18:51:46 +02:00
Baptiste Daroussin
0c0e08be01 diff: handle read(2) failures
MFC After:	1 week

(cherry picked from commit 9b98c4b053917cad42e26596b37df72efab6dc39)
2026-04-06 18:51:46 +02:00
Baptiste Daroussin
8cc85a8751 diff: prefer posix_spawn over pdfork/execl
MFC After:		1 week
Reviewed by:		kib
Differential Revision:	https://reviews.freebsd.org/D56076

(cherry picked from commit 6d8b2ac449f34423be9d56492022421db61d920e)
2026-04-06 18:51:46 +02:00
Dag-Erling Smørgrav
d58ab2ad94 diff: Drop unnecessary casts, fix none algo
MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D55349

(cherry picked from commit d5404462f691230423dc8cc543c6fdc44f34ec31)
2026-02-26 04:02:28 +00:00
Dag-Erling Smørgrav
608d3c746d diff: Tweak recursion tests
The -r flag is not required to compare two directories; it is only
required to compare them recursively, i.e. descend into their common
subdirectories.  Adjust tests that use -r needlessly, and adjust the
dirloop test to verify that these two cases remain distinct.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D55262

(cherry picked from commit b2532432971fbd9339a9a49eca1b532978bb6d48)
2026-02-18 00:15:34 +00:00
Dag-Erling Smørgrav
23a8479c70 diff: Tweak range of -C and -U arguments
POSIX uses the terms “positive decimal integer” for -C and “non-negative
decimal integer” for -U, which translates into lower bounds of 1 for -C
and 0 for -U.

POSIX does not specify a minimum upper bound for either mode, but as of
5fc739eb5949 both our backends support context sizes up to and including
INT_MAX, so use that.

Having had the opportunity to consult the Unix System Test Suite, the
diff test cases found therein happen to precisely match these bounds.

While here, switch to using strtonum() to parse numerical arguments, and
try to be more consistent in how we report usage errors.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D55261

(cherry picked from commit 790f1d1cc5fa892ba59fd7f239b22064c8ab14c7)
2026-02-18 00:15:34 +00:00
Dag-Erling Smørgrav
f201382a55 diff: Improve directory loop detection
When we're done processing a directory, remove its entry from the tree
of visited inodes, ensuring that we only report a loop when we encounter
a descendant-to-ancestor link, not when we encounter a cousin-to-cousin
or sibling-to-sibling link.

MFC after:	1 week
Reported by:	Bakul Shah <bakul@iitbombay.org>
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D55248

(cherry picked from commit 71569594d860a59d8362770a56d806e1d31fb946)
2026-02-15 08:57:46 +00:00
Dag-Erling Smørgrav
c92f6ee0b4 diff: Bump version number
MFC after:	1 week
Sponsored by:	Klara, Inc.

(cherry picked from commit 40134060f6583486ad8ab303140fa73960d3de3d)
2026-02-10 14:24:09 +00:00
Dag-Erling Smørgrav
cbd1600235 diff: Report I/O errors in Stone algorithm
In the legacy Stone algorithm, we do a first pass over the files to
check if they're identical before we start diffing them.  That code
would correctly set the exit status if an I/O error was encountered,
but would not emit an error message.  Do so.

PR:		292198
MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	thj
Differential Revision:	https://reviews.freebsd.org/D55125

(cherry picked from commit f8c12e6e3874cdd353fb16785da6f4e7eb134cd9)
2026-02-10 14:24:09 +00:00
Dag-Erling Smørgrav
2a54bfa0ca diff: Correctly declare tests
Sponsored by:	Klara, Inc.
Fixes:		5fc739eb5949 ("diff: Fix integer overflows in Stone algorithm")
Fixes:		270492602b9b ("diff: Add test case for pagination resource leak")
Fixes:		590126789c84 ("diff: Don't compare a file or directory to itself")

(cherry picked from commit 157d6664aeb815db3b758bd3038fd1512a0f4e2c)
2026-02-10 14:24:09 +00:00
Dag-Erling Smørgrav
5fdcdf4a7e diff: Don't compare a file or directory to itself
While here, stop abusing struct dirent for something we don't even need
to store.

PR:		254455
MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	thj, kevans
Differential Revision:	https://reviews.freebsd.org/D55113

(cherry picked from commit 590126789c841d80655869bc075c8980c173dd1c)

diff: Fix build

rc must be defined first.

Fixes:		590126789c84
MFC after:	1 week
X-MFC with:	590126789c84

(cherry picked from commit ee44ab936e84bacaa49847d36aabdf280f9fecce)
2026-02-10 14:24:08 +00:00
Dag-Erling Smørgrav
e87b922655 diff: Fix pagination leak
* Drop an unnecessary variable and rename pidfd to procd.

* Rewinding stdout serves no purpose, so stop doing it.

* Don't bother freeing memory or setting the global status right
  before erroring out.

* Error out if dup(2) or dup2(2) fail.

* In the unlikely case that our pipe is equal to stdout, we need to
  record that information so we don't close it when cleaning up.

* Don't bother closing a descriptor before dup2(2)ing to it.

* Don't forget to close the the process descriptor after reaping the
  child process.

PR:		266592
MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	kevans, markj
Differential Revision:	https://reviews.freebsd.org/D55112

(cherry picked from commit c3904a7de78ca1ca15fcdf4c09f9d4be7f6fe6f5)
2026-02-10 14:24:08 +00:00
Dag-Erling Smørgrav
a0a37c2143 diff: Add test case for pagination resource leak
The pagination code leaks either processes or descriptors or both,
depending on the exact version of the code you have.  Add a test case
which exercises this leak to facilitate fixing it.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	thj, kevans
Differential Revision:	https://reviews.freebsd.org/D55111

(cherry picked from commit 270492602b9bd8b8fce4f021f055804978bf3f23)
2026-02-10 14:24:08 +00:00
Dag-Erling Smørgrav
4a30735cdd diff: Fix integer overflows in Stone algorithm
Fix integer overflows that may occur when the context window is very
large and add tests to exercise those conditions.

PR:		267032
MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	thj, kevans
Differential Revision:	https://reviews.freebsd.org/D55110

(cherry picked from commit 5fc739eb5949620da911db2f87ca8faedc549d3a)
2026-02-10 14:24:08 +00:00
Dag-Erling Smørgrav
07f25fa320 diff: Print an error message on I/O error
If an error occurs while atomizing either file, immediately print an
error message using the error code returned by the atomizer, and set
the error bit in the exit status.

PR:		292198
MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	thj, kevans
Differential Revision:	https://reviews.freebsd.org/D55109

(cherry picked from commit 55f160fb07eaee977c89bdee7bdd83f4d21f5adf)
2026-02-10 14:24:08 +00:00
Dag-Erling Smørgrav
b97396baca libdiff: Simplify truncation detection
* Adjust the comment to reflect the fact that SIGBUS can occur not only
  if the file is truncated, but also if an I/O error occurs while paging
  in any part of it.

* Instead of setting a flag, just return EIO.

* Adjust the unit test accordingly.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	thj, kevans
Differential Revision:	https://reviews.freebsd.org/D55108

(cherry picked from commit a689bfa4e25af8307709dc12f75b0e02a65abf18)
2026-02-10 14:24:07 +00:00
Baptiste Daroussin
a40fd4e65b diff: remove useless include as kevent is not used anymore
MFC After:	3 days

(cherry picked from commit c479817f30b2d2500c9663a1a07c74ba19321416)
2026-02-05 15:10:44 +01:00
Baptiste Daroussin
c47b4439da diff: fix support for -l with new diff algorithm
MFC After: 3 days
Reviewed by:	des
Differential Revision:	https://reviews.freebsd.org/D55052

(cherry picked from commit 4d73b07d02d12cdff0558d3ca6c4b3224cae831f)
2026-02-05 15:10:44 +01:00
Baptiste Daroussin
a3cb42ca63 diff: use pdwait(2) instead of homemade one
MFC After:	3 days
Reviewed by:	des
Differential Revision:	https://reviews.freebsd.org/D55053

(cherry picked from commit 0ec58e7c2e533a15eabfe8dca4a14e9ae93de4b5)
2026-02-05 15:10:44 +01:00
Dag-Erling Smørgrav
0a5b763d98 libc: Rename fscandir{,_b}() to fdscandir{,_b}().
This seems to fit the pattern better (e.g. fdopendir()).

I've added weak references to ease the transition, but since it's only
been a few days, we can remove them (and the ObsoleteFiles entries for
the manual pages) before we branch stable/15.

Fixes:		deeebfdeca
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D50980
2025-06-23 15:03:10 +02:00
Dag-Erling Smørgrav
28d9586cde diff: Fix gcc build.
Sponsored by:	Klara, Inc.
2025-06-20 17:42:40 +02:00
Dag-Erling Smørgrav
9f1e4ea64a diff: Whitespace nits.
Sponsored by:	Klara, Inc.
2025-06-20 16:09:39 +02:00
Dag-Erling Smørgrav
42092e1b66 diff: Detect loops when diffing directories.
Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D50936
2025-06-20 13:10:35 +02:00
Pau Amma
e945411697 diff: minor fixes to manual page
* Add missing dash before long option name in synopsis.
* Remove spurious paragraph break and bullet item.
* While there, fix mandoc -T lint complaints.

Reviewed by:	ziaee, des
Differential Revision:	https://reviews.freebsd.org/D48388
2025-06-06 14:05:05 +02:00
Dag-Erling Smørgrav
893839b119 diff: Fix device case.
We already fell back to Stone for FIFOs, but we actually need to fall
back to Stone for everything except regular files, because libdiff's
atomizer needs to know the size of its input in advance, and neither
FIFOs nor devices can be trusted to report their size.

MFC after:	1 week
Reported by:	mav
Reviewed by:	mav, allanjude
Differential Revision:	https://reviews.freebsd.org/D48181
2024-12-23 18:16:14 +01:00
Dag-Erling Smørgrav
4e859e67dd libdiff: Implement diff coloring.
This patch got accidentally left out when libdiff was imported.  The
rest of the code (command-line option etc.) was present, just not the
part that actually prints ANSI color codes.

Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D46873
2024-10-02 17:55:06 +02:00
Dag-Erling Smørgrav
9317242469 diff: Fix integer overflow.
The legacy Stone algorithm uses `int` to represent line numbers, array
indices, and array lengths.  If given inputs approaching `INT_MAX` lines,
it would overflow and attempt to allocate ridiculously large amounts of
memory.  To avoid this without penalizing non-pathological inputs,
switch a few variables to `size_t` and add checks while and immediately
after reading both inputs.

MFC after:	3 days
PR:		280371
Sponsored by:	Klara, Inc.
Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D46169
2024-07-29 16:02:29 +02:00
Dag-Erling Smørgrav
ab830de9f5 diff: Fix usage message and documentation.
The `--ignore-all-space` option was incorrectly documented as
`--ignore-all-blanks` in some (but not all) places.

MFC after:	3 days
PR:		280434
Sponsored by:	Klara, Inc.
Reviewed by:	0mp, markj
Differential Revision:	https://reviews.freebsd.org/D46160
2024-07-29 16:02:21 +02:00
Warner Losh
e9ac41698b Remove residual blank line at start of Makefile
This is a residual of the $FreeBSD$ removal.

MFC After: 3 days (though I'll just run the command on the branches)
Sponsored by: Netflix
2024-07-15 16:43:39 -06:00
Dag-Erling Smørgrav
4ce4a0fe14 diff: Document the --no-dereference option.
Also, --no-dereference should not imply --recurse.

MFC after:	3 days
Sponsored by:	Klara, Inc.
Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D45779
2024-06-30 17:55:27 +02:00
Dag-Erling Smørgrav
9c7f83bbb4 diff: Nits in tests.
Sponsored by:	Klara, Inc.
2024-05-20 16:27:50 +02:00
Dag-Erling Smørgrav
b780b6508b diff: Warn if the atomizer detected truncation.
Sponsored by:	Klara, Inc.
Reviewed by:	allanjude, markj
Differential Revision:	https://reviews.freebsd.org/D45219
2024-05-20 15:26:46 +02:00
Ed Maste
fb623aab39 diff: honour -B flag with -q
PR:		278988
Reviewed by:	bapt
Sponsored by:	The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D45220
2024-05-17 20:03:56 -04:00
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
6561c0eceb diff: Improve history section.
Reviewed by:	gbe
Differential Revision:	https://reviews.freebsd.org/D44409
2024-03-19 03:59:00 +01:00
David E. O'Brien
87740ff2ac diff(1) add FreeBSD HISTORY 2024-03-15 15:26:12 -07:00
Dag-Erling Smørgrav
312b1076c6 diff: Bump manual page date.
Sponsored by:	Klara, Inc.
2024-02-26 19:21:40 +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
Pin-Yi Kuo
4185d1f76a diff(1): fix a typo
On the manpage of diff(1), "when" is mistyped to "wen".

Event: Advanced UNIX Programming Course (Fall'23) at NTHU.
Signed-off-by: Pin-Yi Kuo <kuokuoyiyi@gapp.nthu.edu.tw>
Reviewed by: imp, zlei
Pull Request: https://github.com/freebsd/freebsd-src/pull/943
2023-12-26 10:07:55 -07:00
Warner Losh
5e3934b15a usr.bin: Automated cleanup of cdefs and other formatting
Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by:		Netflix
2023-11-26 22:24:01 -07:00
Warner Losh
bdcbfde31e usr.bin: Remove ancient SCCS tags.
Remove ancient SCCS tags from the tree, automated scripting, with two
minor fixup to keep things compiling. All the common forms in the tree
were removed with a perl script.

Sponsored by:		Netflix
2023-11-26 22:23:30 -07:00
Warner Losh
b2c76c41be Remove $FreeBSD$: one-line nroff pattern
Remove /^\.\\"\s*\$FreeBSD\$$\n/
2023-08-16 11:55:15 -06:00
Warner Losh
d0b2dbfa0e Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
2023-08-16 11:55:03 -06:00
Warner Losh
1d386b48a5 Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:42 -06:00
Warner Losh
42b388439b Remove $FreeBSD$: one-line .h pattern
Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
2023-08-16 11:54:23 -06:00
Warner Losh
b3e7694832 Remove $FreeBSD$: two-line .h pattern
Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
2023-08-16 11:54:16 -06:00
John Baldwin
e8f740fbc2 diff: Fully comment out the jackpot variable.
This fixes a set but unused warning.
2023-06-20 09:29:00 -07:00
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