Files
Ian Lepore 22cda9dd45 MFC r315051, r315101, r315103, r315107, r315180, r315197, r315293, r315319,
r315590, r315649, r315726, r315743, r315746-r315747, r315779, r315985,
    r316002, r316639, r316959, r317187, r317194, r317205-r317207, r317381,
    r319489, r319847, r321076-r321079, r321227, r326822

Add the BSD-licensed diff from OpenBSD, which is optionally built and
installed when WITHOUT_GNU_DIFF is set.

r315051:
Import diff from OpenBSD

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

r315101:
Fix wrong date in diff(1)

Reported by:	rgrimes

r315103:
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

r315107:
Fix building with recent gcc

Reported by:	lwhsu, ngie

r315180:
Readd codes that creates a tmp file for diffing stdout or devices

r315197:
Do not die if cap_rights_limit reports ENOSYS

Reported by:	mmel

r315293:
Integrate contrib/netbsd-tests/usr.bin/diff/t_diff.sh in as
.../usr.bin/diff/diff_test

Some minor adjustment needed to be done for :same as it currently
has the test script hardcoded into the test, instead of using an
idiom like $(dirname $0)

Sponsored by:	Dell EMC Isilon

r315319:
diff(1): sort long options under -D example in SYNOPSYS

Sponsored by:	Dell EMC Isilon

r315590:
diff(1): add --strip-trailing-cr to last example in the SYNOPSIS

This syncs the last example in the SYNOPSIS with the other examples.

Reviewed by:	bapt
Sponsored by:	Dell EMC Isilon
Differential Revision:	D10017

r315649:
Cache tzdata when running under capsicum

PR:		217957
Reported by:	tobik@

r315726:
diff(1): fix SYNOPSIS section noting non-existent option, --no-ignore-case

`--no-ignore-case` should be `--no-ignore-file-name-case` per code for
compatibility with [g]diff(1).

Sponsored by:	Dell EMC Isilon

r315743:
Use MAX and MIN macros from sys/param.h

r315746:
Use strndup(3) instead of malloc + memcpy

r315747:
Use MIN macros from sys/param.h

r315779:
diff(1): document remaining long options

While here, try and tie together some of the short options with
their long option equivalents, where possible.

Sponsored by:	Dell EMC Isilon

r315985:
diff: Fix mtime of file1 in -u/-c header line.

PR:		218018
Reviewed by:	bapt
Differential Revision:	https://reviews.freebsd.org/D10140

r316002:
diff: Show nanoseconds in -u/-c header line.

Show nanoseconds in the -u/-c header line.

The present portability conditionals cannot handle the POSIX standard
st_mtim, so remove them and unconditionally use st_mtim.

PR:		218018
Reported by:	jbeich
Reviewed by:	bapt
Differential Revision:	https://reviews.freebsd.org/D10145

r316639:
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

r316959:
Clean up headers declaration

r317187:
Add a regression test for diff -D

r317194:
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

r317205:
Document all long options

r317206:
Update the TODO list to reflect what has been changed

r317207:
Cross reference pr(1) which diff might call with -l option

r317381:
Fix the following warning from gcc 4.2 in usr.bin/diff:

usr.bin/diff/diffreg.c: In function 'change':
usr.bin/diff/diffreg.c:1085: warning: 'i' may be used uninitialized in this function

This version of gcc is not smart enough to see that 'i' cannot actually
be used unitialized.  However, the variable is confusingly re-used, so
it is better to give it another name, and clearly initialize it before
attempting to use it.

Reviewed by:	bapt
Differential Revision: https://reviews.freebsd.org/D10484

r319489:
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

r319847:
Add some testcases for `diff --side-by-side` support

These are were created proactively, in anticipation of the support being
fully implemented sometime in the future.

The tests currently fail on ^/head@r319845, however. Expect them to fail.

PR:		219933
Tested with:	gdiff

r321076:
Don't emit "diff: diff <options> arguments" when diffing files if
-q is specified.

This improves compatibility with GNU diff.

Found by accident with `diff -Nrq /usr/tests /usr/tests.new | grep Kyuafile`.

Relnotes:	yes

r321077:
Add some tests for brief (--brief/-q) format

MFC with:	r321076

r321078:
Fix exit status with -rq when there is a file in one directory but not another,
i.e., when print_only is called.

Prior to this change, -rq was always returning 0. After this change it will
return 1 if there is a difference between two directories.

This fixes compatibility with GNU diff and unbreaks backwards compatibility
expectations.

Found when trying to extend diff_test:brief_format_test.

MFC with:	r321076, r321077

r321079:
Add tests that exercise -q, like -rq and add tests that test -q like -Nrq

MFC with:	r321076, r321077, r321078

r321227:
Use more flexible expression for replacing t_diff in
contrib/netbsd-tests/usr.bin/diff/t_diff.sh with the name of the script via
`basename $0`.

This was a change I forgot to port over from
^/head/gnu/usr.bin/diff/tests/Makefile@r272787.

r326822:
Replace homemade equivalent of tolower(3) by towlower(3)

This will help in the futur making diff -i works with multibyte

Relnotes:	Yes
2018-03-23 21:06:06 +00:00

240 lines
5.9 KiB
C

/* $OpenBSD: diffdir.c,v 1.45 2015/10/05 20:15:00 millert Exp $ */
/*
* Copyright (c) 2003, 2010 Todd C. Miller <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Sponsored in part by the Defense Advanced Research Projects
* Agency (DARPA) and Air Force Research Laboratory, Air Force
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/stat.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fnmatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "diff.h"
static int selectfile(const struct dirent *);
static void diffit(struct dirent *, char *, size_t, char *, size_t, int);
#define d_status d_type /* we need to store status for -l */
/*
* Diff directory traversal. Will be called recursively if -r was specified.
*/
void
diffdir(char *p1, char *p2, int flags)
{
struct dirent *dent1, **dp1, **edp1, **dirp1 = NULL;
struct dirent *dent2, **dp2, **edp2, **dirp2 = NULL;
size_t dirlen1, dirlen2;
char path1[PATH_MAX], path2[PATH_MAX];
int pos;
edp1 = edp2 = NULL;
dirlen1 = strlcpy(path1, *p1 ? p1 : ".", sizeof(path1));
if (dirlen1 >= sizeof(path1) - 1) {
warnc(ENAMETOOLONG, "%s", p1);
status = 2;
return;
}
if (path1[dirlen1 - 1] != '/') {
path1[dirlen1++] = '/';
path1[dirlen1] = '\0';
}
dirlen2 = strlcpy(path2, *p2 ? p2 : ".", sizeof(path2));
if (dirlen2 >= sizeof(path2) - 1) {
warnc(ENAMETOOLONG, "%s", p2);
status = 2;
return;
}
if (path2[dirlen2 - 1] != '/') {
path2[dirlen2++] = '/';
path2[dirlen2] = '\0';
}
/*
* Get a list of entries in each directory, skipping "excluded" files
* and sorting alphabetically.
*/
pos = scandir(path1, &dirp1, selectfile, alphasort);
if (pos == -1) {
if (errno == ENOENT && (Nflag || Pflag)) {
pos = 0;
} else {
warn("%s", path1);
goto closem;
}
}
dp1 = dirp1;
edp1 = dirp1 + pos;
pos = scandir(path2, &dirp2, selectfile, alphasort);
if (pos == -1) {
if (errno == ENOENT && Nflag) {
pos = 0;
} else {
warn("%s", path2);
goto closem;
}
}
dp2 = dirp2;
edp2 = dirp2 + pos;
/*
* If we were given a starting point, find it.
*/
if (start != NULL) {
while (dp1 != edp1 && strcmp((*dp1)->d_name, start) < 0)
dp1++;
while (dp2 != edp2 && strcmp((*dp2)->d_name, start) < 0)
dp2++;
}
/*
* Iterate through the two directory lists, diffing as we go.
*/
while (dp1 != edp1 || dp2 != edp2) {
dent1 = dp1 != edp1 ? *dp1 : NULL;
dent2 = dp2 != edp2 ? *dp2 : NULL;
pos = dent1 == NULL ? 1 : dent2 == NULL ? -1 :
ignore_file_case ? strcasecmp(dent1->d_name, dent2->d_name) :
strcmp(dent1->d_name, dent2->d_name) ;
if (pos == 0) {
/* file exists in both dirs, diff it */
diffit(dent1, path1, dirlen1, path2, dirlen2, flags);
dp1++;
dp2++;
} else if (pos < 0) {
/* file only in first dir, only diff if -N */
if (Nflag)
diffit(dent1, path1, dirlen1, path2, dirlen2,
flags);
else {
print_only(path1, dirlen1, dent1->d_name);
status = 1;
}
dp1++;
} else {
/* file only in second dir, only diff if -N or -P */
if (Nflag || Pflag)
diffit(dent2, path1, dirlen1, path2, dirlen2,
flags);
else {
print_only(path2, dirlen2, dent2->d_name);
status = 1;
}
dp2++;
}
}
closem:
if (dirp1 != NULL) {
for (dp1 = dirp1; dp1 < edp1; dp1++)
free(*dp1);
free(dirp1);
}
if (dirp2 != NULL) {
for (dp2 = dirp2; dp2 < edp2; dp2++)
free(*dp2);
free(dirp2);
}
}
/*
* Do the actual diff by calling either diffreg() or diffdir().
*/
static void
diffit(struct dirent *dp, char *path1, size_t plen1, char *path2, size_t plen2,
int flags)
{
flags |= D_HEADER;
strlcpy(path1 + plen1, dp->d_name, PATH_MAX - plen1);
if (stat(path1, &stb1) != 0) {
if (!(Nflag || Pflag) || errno != ENOENT) {
warn("%s", path1);
return;
}
flags |= D_EMPTY1;
memset(&stb1, 0, sizeof(stb1));
}
strlcpy(path2 + plen2, dp->d_name, PATH_MAX - plen2);
if (stat(path2, &stb2) != 0) {
if (!Nflag || errno != ENOENT) {
warn("%s", path2);
return;
}
flags |= D_EMPTY2;
memset(&stb2, 0, sizeof(stb2));
stb2.st_mode = stb1.st_mode;
}
if (stb1.st_mode == 0)
stb1.st_mode = stb2.st_mode;
if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
if (rflag)
diffdir(path1, path2, flags);
else
printf("Common subdirectories: %s and %s\n",
path1, path2);
return;
}
if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
dp->d_status = D_SKIPPED1;
else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
dp->d_status = D_SKIPPED2;
else
dp->d_status = diffreg(path1, path2, flags, 0);
print_status(dp->d_status, path1, path2, "");
}
/*
* Returns 1 if the directory entry should be included in the
* diff, else 0. Checks the excludes list.
*/
static int
selectfile(const struct dirent *dp)
{
struct excludes *excl;
if (dp->d_fileno == 0)
return (0);
/* always skip "." and ".." */
if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
(dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
return (0);
/* check excludes list */
for (excl = excludes_list; excl != NULL; excl = excl->next)
if (fnmatch(excl->pattern, dp->d_name, FNM_PATHNAME) == 0)
return (0);
return (1);
}