logo

utils-std

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

cmd/basename: add support for long options

Diffstat:

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

diff --git a/cmd/basename.c b/cmd/basename.c @@ -4,6 +4,7 @@ #define _POSIX_C_SOURCE 200809L +#include "../config.h" #include "../libutils/getopt_nolong.h" #include <libgen.h> // basename @@ -11,6 +12,9 @@ #include <stdio.h> // puts, perror #include <string.h> // strlen, strncmp #include <unistd.h> // getopt +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif const char *argv0 = "basename"; @@ -51,7 +55,22 @@ main(int argc, char *argv[]) char *suffix = NULL; char delim = '\n'; +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"multiple", no_argument, NULL, 'a'}, + {"suffix", required_argument, NULL, 's'}, + {"zero", no_argument, NULL, 'z'}, + {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:z", opts, NULL)) != -1;) +#else for(int c = -1; (c = getopt_nolong(argc, argv, ":as:z")) != -1;) +#endif { switch(c) {