logo

utils-std

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

cmd/tee: add support for long options

Diffstat:

Mcmd/tee.c18++++++++++++++++++
Mtest-cmd/tee.sh6+++++-
2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/cmd/tee.c b/cmd/tee.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 <assert.h> /* assert() */ @@ -12,6 +13,9 @@ #include <stdlib.h> /* calloc(), free(), abort() */ #include <string.h> /* strerror() */ #include <unistd.h> /* getopt(), opt… */ +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif const char *argv0 = "tee"; @@ -31,7 +35,21 @@ main(int argc, char *argv[]) FILE **fds = {NULL}; // Shut up GCC int c; +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"append", required_argument, NULL, 'a'}, + {"ignore-interrupts", required_argument, NULL, 'i'}, + {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, "+:ai", opts, NULL)) != -1;) +#else for(int c = -1; (c = getopt_nolong(argc, argv, ":ai")) != -1;) +#endif { switch(c) { diff --git a/test-cmd/tee.sh b/test-cmd/tee.sh @@ -45,5 +45,9 @@ t --exit=1 --input='foo' enoent '/var/empty/e/no/ent' 'tee: error: Failed openin t_file --infile="${WD}/inputs/all_bytes" doubledash "${WD}/inputs/all_bytes" -- -t --exit=1 --input='foo' tripledash '---' "tee: error: Long options unsupported: '---' +if grep -q HAS_GETOPT_LONG "${WD}/../config.h"; then + skip tripledash +else + t --exit=1 --input='foo' tripledash '---' "tee: error: Long options unsupported: '---' " +fi