logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git
commit: e1bdccc8c91f5c8d3badc971907e28848353685c
parent ed86b3a7785e5ffdb522229e68bdf683b4c672c6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri,  6 Dec 2024 17:38:19 +0100

cmd/env: put wordexp behind #ifdef

Diffstat:

Mcmd/env.c19++++++++++++++++---
Mconfigure3+++
Mtest-cmd/env.sh6+++++-
3 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/cmd/env.c b/cmd/env.c @@ -12,7 +12,9 @@ #include <stdlib.h> // putenv #include <string.h> // strchr, strerror #include <unistd.h> // getopt, opt* -#include <wordexp.h> // wordexp +#ifdef HAS_WORDEXP +#include <wordexp.h> +#endif #ifdef HAS_GETOPT_LONG #include <getopt.h> #endif @@ -47,8 +49,13 @@ int main(int argc, char *argv[]) { bool flag_i = false; +#ifdef HAS_WORDEXP bool flag_S = false; wordexp_t opt_S; +#define OPTARGS ":iu:S:" +#else +#define OPTARGS ":iu:" +#endif int c = -1; #ifdef HAS_GETOPT_LONG @@ -57,15 +64,17 @@ main(int argc, char *argv[]) static struct option opts[] = { {"ignore-environment", no_argument, 0, 'i'}, {"unset", required_argument, 0, 'u'}, +#ifdef HAS_WORDEXP {"split-string", required_argument, 0, 'S'}, +#endif {0, 0, 0, 0}, }; // clang-format on // Need + as first character to get POSIX-style option parsing - while((c = getopt_long(argc, argv, "+:iu:S:", opts, NULL)) != -1) + while((c = getopt_long(argc, argv, "+" OPTARGS, opts, NULL)) != -1) #else - while((c = getopt(argc, argv, ":iu:S:")) != -1) + while((c = getopt(argc, argv, OPTARGS)) != -1) #endif { switch(c) @@ -76,6 +85,7 @@ main(int argc, char *argv[]) case 'u': unsetenv(optarg); break; +#ifdef HAS_WORDEXP case 'S': flag_S = true; int ret = wordexp(optarg, &opt_S, WRDE_NOCMD); @@ -103,6 +113,7 @@ main(int argc, char *argv[]) return 1; } break; +#endif case ':': fprintf(stderr, "env: error: Missing operand for option: '-%c'\n", optopt); usage(); @@ -147,6 +158,7 @@ main(int argc, char *argv[]) } } +#ifdef HAS_WORDEXP if(flag_S) { errno = 0; @@ -159,6 +171,7 @@ main(int argc, char *argv[]) } assert(false); } +#endif if(argc < 1) { diff --git a/configure b/configure @@ -235,6 +235,9 @@ check_conftest configure.d/getopt_long.c && CFLAGS="${CFLAGS} -DHAS_GETOPT_LONG" check_conftest configure.d/syncfs.c && CFLAGS="${CFLAGS} -DHAS_SYNCFS" +# As seen on Haiku beta5 +check_header wordexp.h && CFLAGS="${CFLAGS} -DHAS_WORDEXP" + echo ## Configuration write diff --git a/test-cmd/env.sh b/test-cmd/env.sh @@ -21,10 +21,14 @@ t --exit=127 enoent '/var/empty/e/no/ent' "env: error: Failed executing '/var/em t posix_me_harder "-i FOO=BAR sh -c echo" ' ' -t_args opt_S 'foo +if grep -q HAS_WORDEXP "${WD}/config.mk"; then + t_args opt_S 'foo bar baz ' "-S printf '%s\n' foo bar baz" +else + skip opt_S 'Lacks <wordexp.h>' +fi if ! gentoo_sandbox then