logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: adf2daed82463c9c46aa7ce4ee1123dbe4b53ddd
parent 0e24d6f2175ad96c38899ae8910e877bd1527b1f
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 25 Jul 2025 04:41:29 +0200

cmd/tr: add support for long options

Diffstat:

Mcmd/tr.c21++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/cmd/tr.c b/cmd/tr.c @@ -32,6 +32,7 @@ #define _DEFAULT_SOURCE +#include "../config.h" #include "../lib/tr_str.h" #include "../libutils/err.h" #include "../libutils/getopt_nolong.h" @@ -42,6 +43,9 @@ #include <strings.h> // bzero #include <sys/types.h> #include <unistd.h> +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif int delete[NCHARS], squeeze[NCHARS]; int translate[NCHARS] = { @@ -78,7 +82,22 @@ main(int argc, char *argv[]) int cflag, dflag, sflag; cflag = dflag = sflag = 0; - for(int c = -1; (c = getopt_nolong(argc, argv, "Ccds")) != -1;) +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"complement", no_argument, NULL, 'c'}, + {"delete", no_argument, NULL, 'd'}, + {"squeeze-repeats", 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, "+:Ccds", opts, NULL)) != -1;) +#else + for(int c = -1; (c = getopt_nolong(argc, argv, ":Ccds")) != -1;) +#endif { switch(c) {