logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 99cd19f1b1b4a8020294d0e13c5a8ded4eb1736d
parent d1d68799f1823ca77c36aa801d846dea46d476a9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 12 Apr 2022 07:53:12 +0200

bin/env: Add -u flag

Diffstat:

Mbin/env.c7+++++--
Mtest-bin/env10+++++++++-
2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/bin/env.c b/bin/env.c @@ -32,7 +32,7 @@ int export() void usage() { - fprintf(stderr, "env [-i] [key=value ...] [command [args]]\n"); + fprintf(stderr, "env [-i] [-u key] [key=value ...] [command [args]]\n"); } int @@ -42,13 +42,16 @@ main(int argc, char *argv[]) bool flag_i = false; /* flawfinder: ignore. Old implementations of getopt should fix themselves */ - while((c = getopt(argc, argv, ":i")) != -1) + while((c = getopt(argc, argv, ":iu:")) != -1) { switch(c) { case 'i': flag_i = true; break; + case 'u': + unsetenv(optarg); + break; case ':': fprintf(stderr, "Error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/test-bin/env b/test-bin/env @@ -8,7 +8,7 @@ noargs_body() { atf_test_case badarg badarg_body() { - atf_check -s not-exit:0 -e "inline:Error: Unrecognised option: '-f'\nenv [-i] [key=value ...] [command [args]]\n" ../bin/env -f + atf_check -s not-exit:0 -e "inline:Error: Unrecognised option: '-f'\nenv [-i] [-u key] [key=value ...] [command [args]]\n" ../bin/env -f } atf_test_case iflag @@ -21,6 +21,13 @@ iflag_body() { atf_check -o "not-inline:FOO=BAR\n" ../bin/env FOO=BAR } +atf_test_case uflag +uflag_body() { + [ "${LD_PRELOAD}" = "libsandbox.so" ] && atf_expect_fail "sandbox (gentoo) interferes with the environment" + + atf_check -o "inline:FOO=BAR\n" ../bin/env -i FOO=BAR BAR=FOO ../bin/env -u BAR +} + atf_test_case devfull devfull_body() { [ "$(uname -s)" = "NetBSD" ] && atf_expect_fail "NetBSD ignoring write errors for puts()" @@ -44,6 +51,7 @@ atf_init_test_cases() { atf_add_test_case noargs atf_add_test_case badarg atf_add_test_case iflag + atf_add_test_case uflag atf_add_test_case devfull atf_add_test_case noutil atf_add_test_case false