Page MenuHomeFreeBSD

D56506.diff
No OneTemporary

D56506.diff

diff --git a/usr.bin/printenv/printenv.c b/usr.bin/printenv/printenv.c
--- 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 @@
{
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
--- 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
}

File Metadata

Mime Type
text/plain
Expires
Mon, Apr 20, 12:25 AM (4 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
31811819
Default Alt Text
D56506.diff (4 KB)

Event Timeline