logo

utils-std

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

cmd/realpath: add support for long options

Diffstat:

Mcmd/realpath.c38++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+), 0 deletions(-)

diff --git a/cmd/realpath.c b/cmd/realpath.c @@ -5,6 +5,7 @@ #define _POSIX_C_SOURCE 200809L #define _XOPEN_SOURCE 700 // realpath is in XSI +#include "../config.h" #include "../libutils/fs.h" #include "../libutils/getopt_nolong.h" @@ -15,6 +16,9 @@ #include <stdlib.h> // realpath() #include <string.h> // strncmp(), strnlen, strerror #include <unistd.h> // getopt +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif static bool must_exists = false; static bool offline = false; @@ -106,7 +110,24 @@ main_realpath(int argc, char *argv[]) int offset_sep = 0; +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"canonicalize-existing", no_argument, NULL, 'e'}, + {"canonicalize-missing", no_argument, NULL, 'm'}, + {"quiet", no_argument, NULL, 'q'}, + {"strip", no_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, "+:Eemnszq", opts, NULL)) != -1;) +#else for(int c = -1; (c = getopt_nolong(argc, argv, ":Eemnszq")) != -1;) +#endif { switch(c) { @@ -173,7 +194,24 @@ main_readlink(int argc, char *argv[]) int offset_sep = 0; +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"canonicalize", no_argument, NULL, 'f'}, + {"canonicalize-existing", no_argument, NULL, 'e'}, + {"canonicalize-missing", no_argument, NULL, 'm'}, + {"no-newline", no_argument, NULL, 'n'}, + {"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, "+:femnz", opts, NULL)) != -1;) +#else for(int c = -1; (c = getopt_nolong(argc, argv, ":femnz")) != -1;) +#endif { switch(c) {