On 8/13/25 22:01, Collin Funk wrote:

+    error (0 <= cycle_count ? MAX_CYCLES + 1 : EXIT_FAILURE, errno, "%s",

This sort of thing is awkward and doesn't suffice anyway since if (say) xmalloc fails the exit status is wrong. Instead, code should use initialize_exit_failure to set the desired exit status on failure, and call 'error' with the argument 'exit_failure' on failure.

We should set MAX_CYCLES to a value lower than 124, to allow for future extension. The lower the better, to allow for more extensions.


Waaait a minute.

Come to think of it, how about setting MAX_CYCLES to 1? There's no real use for values greater than 1. This is much simpler (it avoids the abovementioned problems among other things), and POSIX allows this. Proposed patch attached.
From c4c62ff7fd9dacb4bf1bc72ae65c24d026857109 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Thu, 14 Aug 2025 09:17:51 -0700
Subject: [PATCH] tsort: add do-nothing -w option

This is for conformance to POSIX.1-2024
* src/tsort.c: Include getopt.h.
(main): Accept and ignore -w.  Do not bother altering
the usage message, as the option is useless.
* tests/misc/tsort.pl (cycle-3): New test.
---
 NEWS                |  2 ++
 doc/coreutils.texi  |  5 +++--
 src/tsort.c         | 30 +++++++++++++++++++++++++++---
 tests/misc/tsort.pl |  4 ++++
 4 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/NEWS b/NEWS
index bfde1e62d..d39d4cd20 100644
--- a/NEWS
+++ b/NEWS
@@ -84,6 +84,8 @@ GNU coreutils NEWS                                    -*- outline -*-
   the same as realpath with no options.  The corresponding long option
   is --canonicalize.
 
+  tsort now accepts and ignores -w, for compatibility with POSIX.1-2024.
+
 ** Improvements
 
   'factor' is now much faster at identifying large prime numbers,
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 4f54770ec..9038979b4 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -6280,8 +6280,9 @@ total ordering.  In the context of the call graph above, the function
 @code{parse_options} may be placed anywhere in the list as long as it
 precedes @code{main}.
 
-The only options are @option{--help} and @option{--version}.  @xref{Common
-options}.
+To conform to POSIX.1-2024, @command{tsort} accepts and ignores the
+option @option{-w}.  The only other options are @option{--help} and
+@option{--version}.  @xref{Common options}.
 
 @exitstatus
 
diff --git a/src/tsort.c b/src/tsort.c
index 2377f7082..65050db11 100644
--- a/src/tsort.c
+++ b/src/tsort.c
@@ -22,6 +22,7 @@
 
 #include <config.h>
 
+#include <getopt.h>
 #include <sys/types.h>
 
 #include "system.h"
@@ -538,9 +539,32 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE_NAME,
-                                   Version, true, usage, AUTHORS,
-                                   (char const *) nullptr);
+  while (true)
+    {
+      static struct option const long_options[] =
+        {
+          {GETOPT_HELP_OPTION_DECL},
+          {GETOPT_VERSION_OPTION_DECL},
+          {nullptr, 0, nullptr, 0}
+        };
+      int c = getopt_long (argc, argv, "w", long_options, nullptr);
+
+      if (c == -1)
+        break;
+
+      switch (c)
+        {
+        case 'w':
+          break;
+
+        case_GETOPT_HELP_CHAR;
+
+        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+
+        default:
+          usage (EXIT_FAILURE);
+        }
+    }
 
   if (1 < argc - optind)
     {
diff --git a/tests/misc/tsort.pl b/tests/misc/tsort.pl
index f1ca28a08..d2e8d2b85 100755
--- a/tests/misc/tsort.pl
+++ b/tests/misc/tsort.pl
@@ -31,6 +31,10 @@ my @Tests =
    ['cycle-2', {IN => {f => "t x\nt s\ns t\n"}}, {OUT => "s\nt\nx\n"},
     {EXIT => 1},
     {ERR => "tsort: f: input contains a loop:\ntsort: s\ntsort: t\n"} ],
+   ['cycle-3', '-w', {IN => {f => "a a\na b\na c\nc a\nb a"}},
+    {OUT => "a\nc\nb\n"}, {EXIT => 1},
+    {ERR => "tsort: f: input contains a loop:\ntsort: a\ntsort: b\n"
+     . "tsort: f: input contains a loop:\ntsort: a\ntsort: c\n"} ],
 
    ['posix-1', {IN => "a b c c d e\ng g\nf g e f\nh h\n"},
     {OUT => "a\nc\nd\nh\nb\ne\nf\ng\n"}],
-- 
2.34.1

Reply via email to