git: cfa209265a30 - stable/13 - tsort: Error out if writing to stdout failed.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Wed, 14 Jun 2023 13:26:30 UTC
The branch stable/13 has been updated by des:

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

commit cfa209265a30952ffc5c58d7d63439a6f61d3f49
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2023-05-04 17:26:59 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2023-06-14 12:48:52 +0000

    tsort: Error out if writing to stdout failed.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans, imp
    Differential Revision:  https://reviews.freebsd.org/D39959
    
    (cherry picked from commit cb46f47c7969b619d1b2547d06a75ad5b375eda9)
    
    tsort: Replace bcopy() with memcpy().
    
    Also fix an indentation error I introduced in the previous commit.
    
    Fixes:          cb46f47c7969
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D40042
    
    (cherry picked from commit b55bc49e8694d9226a82041ff23ad61a5c7a6a76)
    
    tsort: Add unit tests.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D40043
    
    (cherry picked from commit 09aee570980b7eca6e3c902a66f6db129b8c7376)
---
 etc/mtree/BSD.tests.dist          |  2 ++
 usr.bin/tsort/Makefile            |  5 +++
 usr.bin/tsort/tests/Makefile      |  6 ++++
 usr.bin/tsort/tests/tsort_test.sh | 66 +++++++++++++++++++++++++++++++++++++++
 usr.bin/tsort/tsort.c             |  6 ++--
 5 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index 1b2c94f42a6b..eb62812bfcd8 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -1096,6 +1096,8 @@
         ..
         truncate
         ..
+        tsort
+        ..
         units
         ..
         uudecode
diff --git a/usr.bin/tsort/Makefile b/usr.bin/tsort/Makefile
index b0d353e4d8f7..c0933dac2de1 100644
--- a/usr.bin/tsort/Makefile
+++ b/usr.bin/tsort/Makefile
@@ -1,6 +1,11 @@
 #	@(#)Makefile	8.1 (Berkeley) 6/9/93
 # $FreeBSD$
 
+.include <src.opts.mk>
+
 PROG=	tsort
 
+HAS_TESTS=
+SUBDIR.${MK_TESTS}=	tests
+
 .include <bsd.prog.mk>
diff --git a/usr.bin/tsort/tests/Makefile b/usr.bin/tsort/tests/Makefile
new file mode 100644
index 000000000000..ab16c2842dd0
--- /dev/null
+++ b/usr.bin/tsort/tests/Makefile
@@ -0,0 +1,6 @@
+PACKAGE=	tests
+
+ATF_TESTS_SH=	tsort_test
+BINDIR=		${TESTSDIR}
+
+.include <bsd.test.mk>
diff --git a/usr.bin/tsort/tests/tsort_test.sh b/usr.bin/tsort/tests/tsort_test.sh
new file mode 100755
index 000000000000..88d4efaf2b9f
--- /dev/null
+++ b/usr.bin/tsort/tests/tsort_test.sh
@@ -0,0 +1,66 @@
+#
+# Copyright (c) 2023 Klara, Inc.
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+
+atf_test_case basic
+basic_head()
+{
+	atf_set "descr" "Sort a basic graph"
+}
+basic_body()
+{
+	cat >input <<EOF
+A B
+A F
+B C
+B D
+D E
+EOF
+	cat >output <<EOF
+A
+F
+B
+D
+C
+E
+EOF
+	atf_check -o file:output tsort input
+	atf_check -o file:output tsort <input
+}
+
+atf_test_case cycle
+cycle_head()
+{
+	atf_set "descr" "Sort a graph with a cycle"
+}
+cycle_body()
+{
+	cat >input <<EOF
+A B
+A F
+B C
+B D
+D E
+D A
+EOF
+	cat >output<<EOF
+D
+E
+A
+F
+B
+C
+EOF
+	atf_check -e match:cycle -o file:output tsort input
+	atf_check -e match:cycle -o file:output tsort <input
+	atf_check -o file:output tsort -q input
+	atf_check -o file:output tsort -q <input
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case basic
+	atf_add_test_case cycle
+}
diff --git a/usr.bin/tsort/tsort.c b/usr.bin/tsort/tsort.c
index 58492b26999c..53c66e44c76a 100644
--- a/usr.bin/tsort/tsort.c
+++ b/usr.bin/tsort/tsort.c
@@ -183,6 +183,8 @@ main(int argc, char *argv[])
 
 	/* do the sort */
 	tsort();
+	if (ferror(stdout) != 0 || fflush(stdout) != 0)
+		err(1, "stdout");
 	exit(0);
 }
 
@@ -249,7 +251,7 @@ get_node(char *name)
 
 	switch ((*db->get)(db, &key, &data, 0)) {
 	case 0:
-		bcopy(data.data, &n, sizeof(n));
+		memcpy(&n, data.data, sizeof(n));
 		return (n);
 	case 1:
 		break;
@@ -266,7 +268,7 @@ get_node(char *name)
 	n->n_arcs = NULL;
 	n->n_refcnt = 0;
 	n->n_flags = 0;
-	bcopy(name, n->n_name, key.size);
+	memcpy(n->n_name, name, key.size);
 
 	/* Add to linked list. */
 	if ((n->n_next = graph) != NULL)