Date: Wed, 22 Apr 2026 08:19:49 +0000 From: Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav <des@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Subject: git: abdf2a711cab - main - printenv: Clean up Message-ID: <69e884a5.25a49.26f2d7e2@gitrepo.freebsd.org>
index | next in thread | raw e-mail
The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=abdf2a711cabebc107a04fd286e441e2030827f0 commit abdf2a711cabebc107a04fd286e441e2030827f0 Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2026-04-22 08:19:39 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-04-22 08:19:39 +0000 printenv: Clean up * Error out if more than one argument is given. * Check for stdio errors. * Clean up the code. * Clean up and expand the tests. MFC after: 1 week Reviewed by: ngie Differential Revision: https://reviews.freebsd.org/D56506 --- usr.bin/printenv/printenv.c | 52 +++++++++++++----------- usr.bin/printenv/tests/printenv_test.sh | 70 ++++++++++++++++++++++++--------- 2 files changed, 82 insertions(+), 40 deletions(-) diff --git a/usr.bin/printenv/printenv.c b/usr.bin/printenv/printenv.c index 43dbdb0c1378..ba221fe70539 100644 --- a/usr.bin/printenv/printenv.c +++ b/usr.bin/printenv/printenv.c @@ -38,9 +38,15 @@ #include <string.h> #include <unistd.h> -void usage(void); extern char **environ; +static void +usage(void) +{ + (void)fprintf(stderr, "usage: printenv [name]\n"); + exit(EXIT_FAILURE); +} + /* * printenv * @@ -52,40 +58,42 @@ main(int argc, char *argv[]) { char *cp, **ep; size_t len; - int ch; + int opt; if (caph_limit_stdio() < 0 || caph_enter() < 0) err(1, "capsicum"); - while ((ch = getopt(argc, argv, "")) != -1) - switch(ch) { - case '?': + while ((opt = getopt(argc, argv, "")) != -1) { + switch (opt) { default: usage(); } + } argc -= optind; argv += optind; + if (argc > 1) + usage(); if (argc == 0) { for (ep = environ; *ep; ep++) (void)printf("%s\n", *ep); - exit(0); - } - len = strlen(*argv); - for (ep = environ; *ep; ep++) - if (!memcmp(*ep, *argv, len)) { - cp = *ep + len; - if (*cp == '=') { - (void)printf("%s\n", cp + 1); - exit(0); + } else { + len = strlen(*argv); + for (ep = environ; *ep != NULL; ep++) { + if (memcmp(*ep, *argv, len) == 0) { + cp = *ep + len; + if (*cp == '=') { + (void)printf("%s\n", cp + 1); + break; + } } } - exit(1); -} - -void -usage(void) -{ - (void)fprintf(stderr, "usage: printenv [name]\n"); - exit(1); + if (*ep == NULL) { + /* *argv not found */ + exit(EXIT_FAILURE); + } + } + if (fflush(stdout) != 0) + err(EXIT_FAILURE, "stdout"); + exit(EXIT_SUCCESS); } diff --git a/usr.bin/printenv/tests/printenv_test.sh b/usr.bin/printenv/tests/printenv_test.sh index 754ad75d3b5b..83535a555b17 100644 --- a/usr.bin/printenv/tests/printenv_test.sh +++ b/usr.bin/printenv/tests/printenv_test.sh @@ -1,6 +1,7 @@ # # SPDX-License-Identifier: BSD-2-Clause # +# Copyright (c) 2026 Dag-Erling Smørgrav <des@FreeBSD.org> # Copyright (c) 2023 The FreeBSD Foundation # # This software was developed by Yan-Hao Wang <bses30074@gmail.com> @@ -28,37 +29,70 @@ # SUCH DAMAGE # -atf_test_case base -base_head() +atf_test_case basic +basic_head() { - atf_set "descr" "Check that all reported variables exist with the reported values." + atf_set "descr" "Check that all reported variables " \ + "exist with the reported values." } -base_body() +basic_body() { - printenv | while IFS= read -r env; do - env_name=${env%%=*} - env_value=${env#*=} - expected_value=$(eval echo "\$$env_name") - atf_check_equal "${env_value}" "${expected_value}" - done + atf_check -o save:out printenv + while IFS= read -r env; do + env_name=${env%%=*} + env_value=${env#*=} + atf_check -o inline:"${env_value}" -x \ + printf "%s" "\"\$${env_name}\"" + done <out } atf_test_case add_delete_env add_delete_env_head() { - atf_set "descr" "New changes to the environment should be reflected in printenv's output" + atf_set "descr" "New changes to the environment " \ + "should be reflected in printenv's output" } add_delete_env_body() { - env_name=$(date +"%Y%m%d%H%M%S") - export "env_${env_name}=value" - atf_check -o inline:"value\n" printenv "env_${env_name}" - unset "env_${env_name}" - atf_check -s exit:1 printenv "env_${env_name}" + env_name=$(date +"env_%Y%m%d%H%M%S") + export "${env_name}=value" + atf_check -o inline:"value\n" printenv "${env_name}" + unset "${env_name}" + atf_check -s exit:1 printenv "${env_name}" +} + +atf_test_case multi +multi_head() +{ + atf_set "descr" "Error out if given multiple arguments" +} +multi_body() +{ + atf_check -s exit:1 -e match:usage printenv one two +} + +atf_test_case stdout +stdout_head() +{ + atf_set "descr" "Failure to write to stdout" +} +stdout_body() +{ + local dir=$(atf_get_srcdir) + ( + trap "" PIPE + sleep 1 + printenv 2>stderr + echo $? >result + ) | true + atf_check -o inline:"1\n" cat result + atf_check -o match:"stdout" cat stderr } atf_init_test_cases() { - atf_add_test_case base - atf_add_test_case add_delete_env + atf_add_test_case basic + atf_add_test_case add_delete_env + atf_add_test_case multi + atf_add_test_case stdout }home | help
Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69e884a5.25a49.26f2d7e2>
