join: ensure --header skips the order check with empty files
authorPádraig Brady <P@draigBrady.com>
Fri, 14 Jan 2011 08:46:21 +0000 (08:46 +0000)
committerPádraig Brady <P@draigBrady.com>
Sat, 29 Jan 2011 23:33:25 +0000 (23:33 +0000)
* src/join.c: Skip the header even if one of the files is empty.
* tests/misc/join: Add a test case.
* NEWS: Mention the fix

NEWS
src/join.c
tests/misc/join

diff --git a/NEWS b/NEWS
index 5b389f6..a042ee0 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   argument following the one containing the moved sub-tree.
   [bug introduced in coreutils-5.1.0]
 
+  join --header now skips the ordering check for the first line
+  even if the other file is empty.  [bug introduced in coreutils-8.5]
+
   rm -f no longer fails for EINVAL or EILSEQ on file systems that
   reject file names invalid for that file system.
 
index bf7e908..ab76251 100644 (file)
@@ -648,13 +648,17 @@ join (FILE *fp1, FILE *fp2)
       autocount_2 = seq2.count ? seq2.lines[0]->nfields : 0;
     }
 
-  if (join_header_lines && seq1.count && seq2.count)
+  if (join_header_lines && (seq1.count || seq2.count))
     {
-      prjoin (seq1.lines[0], seq2.lines[0]);
+      struct line const *hline1 = seq1.count ? seq1.lines[0] : &uni_blank;
+      struct line const *hline2 = seq2.count ? seq2.lines[0] : &uni_blank;
+      prjoin (hline1, hline2);
       prevline[0] = NULL;
       prevline[1] = NULL;
-      advance_seq (fp1, &seq1, true, 1);
-      advance_seq (fp2, &seq2, true, 2);
+      if (seq1.count)
+        advance_seq (fp1, &seq1, true, 1);
+      if (seq2.count)
+        advance_seq (fp2, &seq2, true, 2);
     }
 
   while (seq1.count && seq2.count)
index 3cf278b..5086ee7 100755 (executable)
@@ -239,6 +239,12 @@ my @tv = (
  [ "ID1 Name\n1 A\n2 B\n", "ID2 Color\n1 red\n"],
    "ID1 Name Color\n1 A red\n", 0],
 
+# '--header' doesn't check order of a header
+# even if there is no header in the second file
+['header-6', '--header -a1',
+ [ "ID1 Name\n1 A\n", ""],
+   "ID1 Name\n1 A\n", 0],
+
 );
 
 # Convert the above old-style test vectors to the newer