git: fbbf68558db0 - main - soelim: fix error detection and exit status

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 07:40:26 UTC
The branch main has been updated by bapt:

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

commit fbbf68558db0dc697198579c588ff59271cf7949
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2026-11-18 06:16:50 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2026-11-18 07:40:20 +0000

    soelim: fix error detection and exit status
---
 usr.bin/soelim/soelim.c             | 20 +++++++++++++++-----
 usr.bin/soelim/tests/soelim_test.sh | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 5 deletions(-)

diff --git a/usr.bin/soelim/soelim.c b/usr.bin/soelim/soelim.c
index 59328ba04874..51f3203c067f 100644
--- a/usr.bin/soelim/soelim.c
+++ b/usr.bin/soelim/soelim.c
@@ -79,16 +79,18 @@ soelim_fopen(const char *name)
 }
 
 static int
-soelim_file(FILE *f, int flag)
+soelim_file(FILE *f, const char *name, int flag)
 {
 	char *line = NULL;
 	char *walk, *cp;
 	size_t linecap = 0;
 	ssize_t linelen;
+	int ret;
 
 	if (f == NULL)
 		return (1);
 
+	ret = 0;
 	while ((linelen = getline(&line, &linecap, f)) > 0) {
 		if (strncmp(line, ".so", 3) != 0) {
 			printf("%s", line);
@@ -115,7 +117,7 @@ soelim_file(FILE *f, int flag)
 			printf("%s", line);
 			continue;
 		}
-		if (soelim_file(soelim_fopen(walk), flag) == 1) {
+		if (soelim_file(soelim_fopen(walk), walk, flag) == 1) {
 			free(line);
 			return (1);
 		}
@@ -123,10 +125,14 @@ soelim_file(FILE *f, int flag)
 			printf("%s", cp);
 	}
 
+	if (ferror(f)) {
+		warn("%s", name);
+		ret = 1;
+	}
 	free(line);
 	fclose(f);
 
-	return (0);
+	return (ret);
 }
 
 int
@@ -163,12 +169,16 @@ main(int argc, char **argv)
 	argv += optind;
 
 	if (argc == 0)
-		ret = soelim_file(stdin, flags);
+		ret = soelim_file(stdin, "stdin", flags);
 
 	for (i = 0; i < argc; i++)
-		ret = soelim_file(soelim_fopen(argv[i]), flags);
+		if (soelim_file(soelim_fopen(argv[i]), argv[i], flags) != 0)
+			ret = 1;
 
 	sl_free(includes, 0);
 
+	if (fflush(stdout) != 0)
+		err(EXIT_FAILURE, "stdout");
+
 	return (ret);
 }
diff --git a/usr.bin/soelim/tests/soelim_test.sh b/usr.bin/soelim/tests/soelim_test.sh
index 7fb70a1f71e9..5624a991ca35 100755
--- a/usr.bin/soelim/tests/soelim_test.sh
+++ b/usr.bin/soelim/tests/soelim_test.sh
@@ -95,8 +95,42 @@ files_body()
 
 }
 
+atf_test_case errors
+errors_head()
+{
+	atf_set "descr" "error handling"
+}
+
+errors_body()
+{
+	cp $(atf_get_srcdir)/basic .
+
+	# A failure on one file is not hidden by a later success.
+	atf_check \
+		-o ignore \
+		-e ignore \
+		-s exit:1 \
+		soelim doesnotexist $(atf_get_srcdir)/basic.in
+
+	# A read error is not reported as an empty file.
+	mkdir dir
+	atf_check \
+		-o empty \
+		-e ignore \
+		-s exit:1 \
+		soelim dir
+
+	# Write errors are not ignored.
+	atf_check \
+		-o empty \
+		-e ignore \
+		-s exit:1 \
+		sh -c 'soelim basic.in > /dev/full'
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case stdin
 	atf_add_test_case files
+	atf_add_test_case errors
 }