>From 1ebde7af7d1e122bcb2d0935d0b37af21156f357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draig=20Brady?= Date: Thu, 27 Jan 2011 07:17:16 +0000 Subject: [PATCH] join: don't report disorder against an empty file This allows one to use join as a field extractor like: join -a1 -o 1.3,1.1 - /dev/null * src/join.c (join): Don't flag unpairable lines when one of the files is empty. * tests/misc/join: Add a new test for empty input, and adjust a previous test that was only checking against empty input. * doc/coreutils.texi (join invocation): Document the change. * NEWS: Likewise. --- NEWS | 6 ++++++ doc/coreutils.texi | 18 +++++++++++++----- src/join.c | 8 +++++--- tests/misc/join | 6 +++++- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/NEWS b/NEWS index 9ccad63..422bbe6 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,12 @@ GNU coreutils NEWS -*- outline -*- rm -f no longer fails for EINVAL or EILSEQ on file systems that reject file names invalid for that file system. +** Changes in behavior + + join no longer reports disorder when one of the files is empty. + This allows one to use join as a field extractor like: + join -a1 -o 1.3,1.1 - /dev/null + * Noteworthy changes in release 8.9 (2011-01-04) [stable] diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 85d5201..c2a7580 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -4761,11 +4761,17 @@ If there is an error it exits with nonzero status. @macro checkOrderOption{cmd} If the @option{--check-order} option is given, unsorted inputs will cause a fatal error message. If the option @option{--nocheck-order} -is given, unsorted inputs will never cause an error message. If -neither of these options is given, wrongly sorted inputs are diagnosed -only if an input file is found to contain unpairable lines. If an -input file is diagnosed as being unsorted, the @command{\cmd\} command -will exit with a nonzero status (and the output should not be used). +is given, unsorted inputs will never cause an error message. If neither +of these options is given, wrongly sorted inputs are diagnosed +only if an input file is found to contain unpairable +@ifset JOIN_COMMAND +lines, and when both input files are non empty. +@end ifset +@ifclear JOIN_COMMAND +lines. +@end ifclear +If an input file is diagnosed as being unsorted, the @command{\cmd\} +command will exit with a nonzero status (and the output should not be used). Forcing @command{\cmd\} to process wrongly sorted input files containing unpairable lines by specifying @option{--nocheck-order} is @@ -5646,7 +5652,9 @@ c c1 c2 b b1 b2 @end example +@set JOIN_COMMAND @checkOrderOption{join} +@clear JOIN_COMMAND The defaults are: @itemize diff --git a/src/join.c b/src/join.c index afda5a1..6e10f61 100644 --- a/src/join.c +++ b/src/join.c @@ -711,7 +711,7 @@ join (FILE *fp1, FILE *fp2) seq2.count = 0; } - /* If the user did not specify --check-order, then we read the + /* If the user did not specify --nocheck-order, then we read the tail ends of both inputs to verify that they are in order. We skip the rest of the tail once we have issued a warning for that file, unless we actually need to print the unpairable lines. */ @@ -726,7 +726,8 @@ join (FILE *fp1, FILE *fp2) { if (print_unpairables_1) prjoin (seq1.lines[0], &uni_blank); - seen_unpairable = true; + if (seq2.count) + seen_unpairable = true; while (get_line (fp1, &line, 1)) { if (print_unpairables_1) @@ -740,7 +741,8 @@ join (FILE *fp1, FILE *fp2) { if (print_unpairables_2) prjoin (&uni_blank, seq2.lines[0]); - seen_unpairable = true; + if (seq1.count) + seen_unpairable = true; while (get_line (fp2, &line, 2)) { if (print_unpairables_2) diff --git a/tests/misc/join b/tests/misc/join index 3696a03..3ce267c 100755 --- a/tests/misc/join +++ b/tests/misc/join @@ -189,7 +189,11 @@ my @tv = ( # Before 6.10.143, this would mistakenly fail with the diagnostic: # join: File 1 is not in sorted order -['chkodr-7', '-12', ["2 a\n1 b\n", ""], "", 0], +['chkodr-7', '-12', ["2 a\n1 b\n", "2 c\n1 d"], "", 0], + +# After 8.9, join doesn't report disorder by default +# when comparing against an empty input file. +['chkodr-8', '', ["2 a\n1 b\n", ""], "", 0], # Test '--header' feature ['header-1', '--header', -- 1.7.3.4