>From 99ed9353c43fc8682f99a70d8a0b97d75f78f3f8 Mon Sep 17 00:00:00 2001
From: "Geyslan G. Bem" <geys...@gmail.com>
Date: Sat, 6 Jul 2019 01:04:40 -0300
Subject: [PATCH] cat: open any file name after "--"

* src/cat.c (main): Treat "-" after "--" as file, not as stdin.
---
 src/cat.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/cat.c b/src/cat.c
index d8c444aaf..be3dc2def 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -522,6 +522,9 @@ main (int argc, char **argv)
   /* Index in argv to processed argument.  */
   int argind;

+  /* Limit index of non-literal args */
+  int limind = argc;
+
   /* Device number of the output (file or whatever).  */
   dev_t out_dev;

@@ -571,6 +574,16 @@ main (int argc, char **argv)
      close_stdout does nothing.  */
   atexit (close_stdout);

+  /* Get limit of non-literal args before possible permutes by getopt*() */
+  for (int i = 0; i < argc; ++i)
+    {
+      if (!strcmp (argv[i], "--"))
+        {
+          limind = i;
+          break;
+        }
+    }
+
   /* Parse command line options.  */

   while ((c = getopt_long (argc, argv, "benstuvAET", long_options, NULL))
@@ -660,7 +673,7 @@ main (int argc, char **argv)
       if (argind < argc)
         infile = argv[argind];

-      if (STREQ (infile, "-"))
+      if ((argind <= limind) && (STREQ (infile, "-")))
         {
           have_read_stdin = true;
           input_desc = STDIN_FILENO;
@@ -752,7 +765,7 @@ main (int argc, char **argv)
       free (inbuf);

     contin:
-      if (!STREQ (infile, "-") && close (input_desc) < 0)
+      if ((input_desc != STDIN_FILENO) && (close (input_desc) < 0))
         {
           error (0, errno, "%s", quotef (infile));
           ok = false;
-- 
2.22.0

Reply via email to