git: c2d93a803ace - main - env: Check the status of stdout.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Tue, 08 Oct 2024 17:02:20 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=c2d93a803acef634bd0eede6673aeea59e90c277

commit c2d93a803acef634bd0eede6673aeea59e90c277
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-10-08 17:01:46 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-10-08 17:01:46 +0000

    env: Check the status of stdout.
    
    MFC after:      3 days
    Sponsored by:   Klara, Inc.
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D47009
---
 usr.bin/env/env.c             |  2 ++
 usr.bin/env/tests/env_test.sh | 17 +++++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/usr.bin/env/env.c b/usr.bin/env/env.c
index bb83baee114f..10e333602ee8 100644
--- a/usr.bin/env/env.c
+++ b/usr.bin/env/env.c
@@ -214,6 +214,8 @@ main(int argc, char **argv)
 	}
 	for (ep = environ; *ep; ep++)
 		(void)printf("%s%c", *ep, term);
+	if (fflush(stdout) != 0)
+		err(1, "stdout");
 	exit(0);
 }
 
diff --git a/usr.bin/env/tests/env_test.sh b/usr.bin/env/tests/env_test.sh
index d49765a04f9a..2dc8f1a4c911 100644
--- a/usr.bin/env/tests/env_test.sh
+++ b/usr.bin/env/tests/env_test.sh
@@ -130,6 +130,22 @@ chdir_body()
 		  env -C "${subdir}" pwd
 }
 
+atf_test_case stdout
+stdout_head()
+{
+	atf_set descr "Failure to write to stdout"
+}
+stdout_body()
+{
+	(
+		trap "" PIPE
+		env 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 basic
@@ -140,4 +156,5 @@ atf_init_test_cases()
 	atf_add_test_case altpath
 	atf_add_test_case equal
 	atf_add_test_case chdir
+	atf_add_test_case stdout
 }