Subject: bug#9236: Fwd: Join

From: Jim Meyering <[email-masked]>
Date: Thu, 04 Aug 2011 19:48:20 +0200

Eric Blake wrote:
> merge 9235 9236
> thanks
>
> On 08/03/2011 08:48 PM, David Gast wrote:
>> Oops, I hit the wrong button ...
>>
>> cat > /tmp/x <<!
>> b
>> a
>> !
>> ln /tmp/x /tmp/y
>> sort -c /tmp/x
>> join --check-order /tmp/x /tmp/y
>> # Note: The two files do not have to be the same.
>>
>> Output is
>>
>> sort: /tmp/x:2: disorder: a
>> join: file 1 is not in sorted order
>
> This sounds like a reasonable idea!  Would you like to contribute the patch?

I started looking at this, and among other things saw
a diagnostic that mentioned "file 1", which would do
much better to mention the actual file name, so embarked.
Here's a preliminary patch (not even a decent ChangeLog entry
and the join test still needs to be updated):

    $ printf '%s\n' b a c > in
    $ ./join --check-order in in
    ./join: in:2: is not sorted: a
    [Exit 1]

>From adf709ba6a8d934e8f90cafada824221e1c6eb18 Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email-masked]>
Date: Thu, 4 Aug 2011 19:31:50 +0200
Subject: [PATCH] join: FIXME: check: print both file name and line number

---
 src/join.c |   29 +++++++++++++++++++----------
 1 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/join.c b/src/join.c
index 99d918f..368d0db 100644
--- a/src/join.c
+++ b/src/join.c
@@ -89,6 +89,12 @@ struct seq
 /* The previous line read from each file. */
 static struct line *prevline[2] = {NULL, NULL};

+/* The number of lines read from each file. */
+static uintmax_t line_no[2] = {0, 0};
+
+/* The input file names.  */
+static char *g_names[2];
+
 /* This provides an extra line buffer for each file.  We need these if we
    try to read two consecutive lines into the same buffer, since we don't
    want to overwrite the previous buffer before we check order. */
@@ -386,7 +392,10 @@ check_order (const struct line *prev,
             {
               error ((check_input_order == CHECK_ORDER_ENABLED
                       ? EXIT_FAILURE : 0),
-                     0, _("file %d is not in sorted order"), whatfile);
+                     0, _("%s:%ju: is not sorted: %.*s"),
+                     g_names[whatfile], line_no[whatfile],
+                     current->buf.length-1, /* FIXME should be int */
+                     current->buf.buffer);

               /* If we get to here, the message was just a warning, but we
                  want only to issue it once. */
@@ -436,6 +445,7 @@ get_line (FILE *fp, struct line **linep, int which)
       freeline (line);
       return false;
     }
+  ++line_no[which];

   xfields (line);

@@ -980,7 +990,6 @@ main (int argc, char **argv)
   int prev_optc_status = MUST_BE_OPERAND;
   int operand_status[2];
   int joption_count[2] = { 0, 0 };
-  char *names[2];
   FILE *fp1, *fp2;
   int optc;
   int nfiles = 0;
@@ -1100,7 +1109,7 @@ main (int argc, char **argv)
           break;

         case 1:		/* Non-option argument.  */
-          add_file_name (optarg, names, operand_status, joption_count,
+          add_file_name (optarg, g_names, operand_status, joption_count,
                          &nfiles, &prev_optc_status, &optc_status);
           break;

@@ -1122,7 +1131,7 @@ main (int argc, char **argv)
   /* Process any operands after "--".  */
   prev_optc_status = MUST_BE_OPERAND;
   while (optind < argc)
-    add_file_name (argv[optind++], names, operand_status, joption_count,
+    add_file_name (argv[optind++], g_names, operand_status, joption_count,
                    &nfiles, &prev_optc_status, &optc_status);

   if (nfiles != 2)
@@ -1148,20 +1157,20 @@ main (int argc, char **argv)
   if (join_field_2 == SIZE_MAX)
     join_field_2 = 0;

-  fp1 = STREQ (names[0], "-") ? stdin : fopen (names[0], "r");
+  fp1 = STREQ (g_names[0], "-") ? stdin : fopen (g_names[0], "r");
   if (!fp1)
-    error (EXIT_FAILURE, errno, "%s", names[0]);
-  fp2 = STREQ (names[1], "-") ? stdin : fopen (names[1], "r");
+    error (EXIT_FAILURE, errno, "%s", g_names[0]);
+  fp2 = STREQ (g_names[1], "-") ? stdin : fopen (g_names[1], "r");
   if (!fp2)
-    error (EXIT_FAILURE, errno, "%s", names[1]);
+    error (EXIT_FAILURE, errno, "%s", g_names[1]);
   if (fp1 == fp2)
     error (EXIT_FAILURE, errno, _("both files cannot be standard input"));
   join (fp1, fp2);

   if (fclose (fp1) != 0)
-    error (EXIT_FAILURE, errno, "%s", names[0]);
+    error (EXIT_FAILURE, errno, "%s", g_names[0]);
   if (fclose (fp2) != 0)
-    error (EXIT_FAILURE, errno, "%s", names[1]);
+    error (EXIT_FAILURE, errno, "%s", g_names[1]);

   if (issued_disorder_warning[0] || issued_disorder_warning[1])
     exit (EXIT_FAILURE);
--
1.7.4.4