logo

utils-std

Collection of commonly available Unix tools
commit: e1fac62879872c896bdca5e6ec7bfb4146a8f7ec
parent 08efe9261e124f49e943c0a497e246bee873f08b
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 28 Jul 2024 06:16:44 +0200

cmd/wc: Add getopt_long compatibility

All options are defined since SUSv1 (1994) but of course elfutils would pull this shit.

Diffstat:

Mcmd/wc.c19+++++++++++++++++++
1 file changed, 19 insertions(+), 0 deletions(-)

diff --git a/cmd/wc.c b/cmd/wc.c @@ -18,6 +18,9 @@ #include <unistd.h> // getopt #include <wchar.h> #include <wctype.h> // iswspace +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif static char *argv0 = "wc"; @@ -168,7 +171,23 @@ main(int argc, char *argv[]) int (*wc_file)(FILE *, char *) = &wc_file_bytes; int c = -1; +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"bytes", no_argument, 0, 'c'}, + {"lines", no_argument, 0, 'l'}, + {"chars", no_argument, 0, 'm'}, + {"words", no_argument, 0, 'w'}, + {0, 0, 0, 0}, + }; + // clang-format on + + // Need + as first character to get POSIX-style option parsing + while((c = getopt_long(argc, argv, "+:clmw", opts, NULL)) != -1) +#else while((c = getopt(argc, argv, ":clmw")) != -1) +#endif { switch(c) {