The branch main has been updated by thj:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=8cf449db88b2c7a505bc241a78f9736b43f2ede5

commit 8cf449db88b2c7a505bc241a78f9736b43f2ede5
Author:     Tom Jones <t...@freebsd.org>
AuthorDate: 2022-03-10 16:15:39 +0000
Commit:     Tom Jones <t...@freebsd.org>
CommitDate: 2022-03-10 16:17:37 +0000

    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
---
 usr.bin/diff/diff.1 | 15 ++++++++++++++-
 usr.bin/diff/diff.c | 25 +++++++++++++++++++++----
 2 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/usr.bin/diff/diff.1 b/usr.bin/diff/diff.1
index b17ddb123fd5..8582db6c9c1c 100644
--- a/usr.bin/diff/diff.1
+++ b/usr.bin/diff/diff.1
@@ -30,7 +30,7 @@
 .\"     @(#)diff.1     8.1 (Berkeley) 6/30/93
 .\" $FreeBSD$
 .\"
-.Dd June 19, 2020
+.Dd March 10, 2022
 .Dt DIFF 1
 .Os
 .Sh NAME
@@ -207,6 +207,9 @@
 .Op Fl -width
 .Fl y | Fl -side-by-side
 .Ar file1 file2
+.Nm diff
+.Op Fl -help
+.Op Fl -version
 .Sh DESCRIPTION
 The
 .Nm
@@ -282,6 +285,8 @@ Identical output to that of the
 flag, but in reverse order.
 It cannot be digested by
 .Xr ed 1 .
+.It Fl -help
+This option prints a summary to stdout and exits with status 0.
 .It Fl n
 Produces a script similar to that of
 .Fl e ,
@@ -308,6 +313,8 @@ However, unlike with
 .Fl c ,
 all lines to be changed (added and/or removed) are present in
 a single section.
+.It Fl -version
+This option prints a version string to stdout and exits with status 0.
 .It Fl y Fl -side-by-side
 Output in two columns with a marker between them.
 The marker can be one
@@ -655,6 +662,12 @@ Differences were found.
 .It >1
 An error occurred.
 .El
+.Pp
+The
+.Fl -help
+and
+.Fl -version
+options exit with a status of 0.
 .Sh EXAMPLES
 Compare
 .Pa old_dir
diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c
index 34cdd0d846d9..ba6bc7c3a310 100644
--- a/usr.bin/diff/diff.c
+++ b/usr.bin/diff/diff.c
@@ -38,8 +38,10 @@ __FBSDID("$FreeBSD$");
 #include "diff.h"
 #include "xmalloc.h"
 
+static const char diff_version[] = "FreeBSD diff 20220309";
 bool    lflag, Nflag, Pflag, rflag, sflag, Tflag, cflag;
 bool    ignore_file_case, suppress_common, color, noderef;
+static bool help = false;
 int     diff_format, diff_context, status;
 int     tabsize = 8, width = 130;
 static int     colorflag = COLORFLAG_NEVER;
@@ -58,11 +60,13 @@ enum {
        OPT_IGN_FN_CASE,
        OPT_NO_IGN_FN_CASE,
        OPT_NORMAL,
+       OPT_HELP,
        OPT_HORIZON_LINES,
        OPT_CHANGED_GROUP_FORMAT,
        OPT_SUPPRESS_COMMON,
        OPT_COLOR,
        OPT_NO_DEREFERENCE,
+       OPT_VERSION,
 };
 
 static struct option longopts[] = {
@@ -97,6 +101,7 @@ static struct option longopts[] = {
        { "exclude-from",               required_argument,      0,      'X' },
        { "side-by-side",               no_argument,            NULL,   'y' },
        { "ignore-file-name-case",      no_argument,            NULL,   
OPT_IGN_FN_CASE },
+       { "help",                       no_argument,            NULL,   
OPT_HELP},
        { "horizon-lines",              required_argument,      NULL,   
OPT_HORIZON_LINES },
        { "no-dereference",             no_argument,            NULL,   
OPT_NO_DEREFERENCE},
        { "no-ignore-file-name-case",   no_argument,            NULL,   
OPT_NO_IGN_FN_CASE },
@@ -106,6 +111,7 @@ static struct option longopts[] = {
        { "changed-group-format",       required_argument,      NULL,   
OPT_CHANGED_GROUP_FORMAT},
        { "suppress-common-lines",      no_argument,            NULL,   
OPT_SUPPRESS_COMMON },
        { "color",                      optional_argument,      NULL,   
OPT_COLOR },
+       { "version",                    no_argument,            NULL,   
OPT_VERSION},
        { NULL,                         0,                      0,      '\0'}
 };
 
@@ -294,6 +300,10 @@ main(int argc, char **argv)
                        diff_format = D_GFORMAT;
                        group_format = optarg;
                        break;
+               case OPT_HELP:
+                       help = true;
+                       usage();
+                       break;
                case OPT_HORIZON_LINES:
                        break; /* XXX TODO for compatibility with GNU diff3 */
                case OPT_IGN_FN_CASE:
@@ -335,6 +345,9 @@ main(int argc, char **argv)
                        rflag = true;
                        noderef = true;
                        break;
+               case OPT_VERSION:
+                       printf("%s\n", diff_version);
+                       exit(0);
                default:
                        usage();
                        break;
@@ -569,9 +582,9 @@ print_status(int val, char *path1, char *path2, const char 
*entry)
 }
 
 static void
-usage(void)
+usage()
 {
-       (void)fprintf(stderr,
+       (void)fprintf(help ? stdout : stderr,
            "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] 
[--ignore-case]\n"
            "            [--no-ignore-case] [--normal] [--strip-trailing-cr] 
[--tabsize]\n"
            "            [-I pattern] [-F pattern] [-L label] file1 file2\n"
@@ -590,9 +603,13 @@ usage(void)
            "            [--ignore-blank-lines] [--ignore-case] [--minimal]\n"
            "            [--no-ignore-file-name-case] [--strip-trailing-cr]\n"
            "            [--suppress-common-lines] [--tabsize] [--text] 
[--width]\n"
-           "            -y | --side-by-side file1 file2\n");
+           "            -y | --side-by-side file1 file2\n"
+           "       diff [--help] [--version]\n");
 
-       exit(2);
+       if (help)
+               exit(0);
+       else
+               exit(2);
 }
 
 static void

Reply via email to