logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: 5e6b094dc0e7fcbf9f103540a267d2b1f35e98af
parent e586ba803d431e4b73af962f25643a24b59a4bf6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 25 Jul 2025 11:07:19 +0200

cmd/which: add support for long options

Diffstat:

Mcmd/which.c17+++++++++++++++++
1 file changed, 17 insertions(+), 0 deletions(-)

diff --git a/cmd/which.c b/cmd/which.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 "../libutils/strchrnul.h" @@ -13,6 +14,9 @@ #include <stdlib.h> // getenv #include <string.h> // strcpy, memcpy #include <unistd.h> // access, getopt +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif const char *argv0 = "which"; @@ -29,7 +33,20 @@ main(int argc, char *argv[]) return 1; } +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"all", no_argument, NULL, 'a'}, + {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, "+as", opts, NULL)) != -1;) +#else for(int c = -1; (c = getopt_nolong(argc, argv, "as")) != -1;) +#endif { switch(c) {