logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: 1505a3cf836653af01e83dafe26e952be39bccab
parent 9d4eb998f2760c0d15648150a41609ba48502b26
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 24 Jul 2025 22:23:35 +0200

cmd/cmp: add support for long options

Diffstat:

Mcmd/cmp.c20++++++++++++++++++++
1 file changed, 20 insertions(+), 0 deletions(-)

diff --git a/cmd/cmp.c b/cmd/cmp.c @@ -3,6 +3,7 @@ // SPDX-License-Identifier: MPL-2.0 #define _POSIX_C_SOURCE 200809L +#include "../config.h" #include "../libutils/getopt_nolong.h" #include <assert.h> @@ -14,6 +15,9 @@ #include <string.h> // strerror #include <sys/stat.h> // fstat #include <unistd.h> // getopt +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif static bool opt_s = false, opt_l = false; static unsigned long max_bytes = 0; @@ -133,7 +137,23 @@ main(int argc, char *argv[]) { char *endptr = NULL; +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"verbose", no_argument, NULL, 'l'}, + {"bytes", required_argument, NULL, 'n'}, + {"quiet", no_argument, NULL, 's'}, + {"silent", no_argument, NULL, 's'}, + {0, 0, 0, 0}, + }; + // clang-format on + + // Need + as first character to get POSIX-style option parsing + for(int c = -1; (c = getopt_long(argc, argv, "+:ln:s", opts, NULL)) != -1;) +#else for(int c = -1; (c = getopt_nolong(argc, argv, ":ln:s")) != -1;) +#endif { switch(c) {