mirror of
git://git.sv.gnu.org/coreutils.git
synced 2026-08-04 15:49:42 +02:00
comm: support NUL --output-delimiter for consistency
* src/comm.c (main): Track the output delimiter length, so that it can be adjusted to 1 for the NUL delimiter. Also rename the global variable from "delimiter" to "col_sep" so its use is more obvious, and to distinguish from the recently added "delim" global variable. * tests/misc/comm.pl: Adjust accordingly.
This commit is contained in:
+13
-21
@@ -71,9 +71,9 @@ static enum
|
||||
} check_input_order;
|
||||
|
||||
/* Output columns will be delimited with this string, which may be set
|
||||
on the command-line with --output-delimiter=STR. The default is a
|
||||
single TAB character. */
|
||||
static char const *delimiter;
|
||||
on the command-line with --output-delimiter=STR. */
|
||||
static char const *col_sep = "\t";
|
||||
static size_t col_sep_len = 0;
|
||||
|
||||
/* For long options that have no equivalent short option, use a
|
||||
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
|
||||
@@ -174,20 +174,17 @@ writeline (struct linebuffer const *line, FILE *stream, int class)
|
||||
case 2:
|
||||
if (!only_file_2)
|
||||
return;
|
||||
/* Print a delimiter if we are printing lines from file 1. */
|
||||
if (only_file_1)
|
||||
fputs (delimiter, stream);
|
||||
fwrite (col_sep, 1, col_sep_len, stream);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
if (!both)
|
||||
return;
|
||||
/* Print a delimiter if we are printing lines from file 1. */
|
||||
if (only_file_1)
|
||||
fputs (delimiter, stream);
|
||||
/* Print a delimiter if we are printing lines from file 2. */
|
||||
fwrite (col_sep, 1, col_sep_len, stream);
|
||||
if (only_file_2)
|
||||
fputs (delimiter, stream);
|
||||
fwrite (col_sep, 1, col_sep_len, stream);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -419,14 +416,10 @@ main (int argc, char **argv)
|
||||
break;
|
||||
|
||||
case OUTPUT_DELIMITER_OPTION:
|
||||
if (delimiter && !STREQ (delimiter, optarg))
|
||||
error (EXIT_FAILURE, 0, _("multiple delimiters specified"));
|
||||
delimiter = optarg;
|
||||
if (!*delimiter)
|
||||
{
|
||||
error (EXIT_FAILURE, 0, _("empty %s not allowed"),
|
||||
quote ("--output-delimiter"));
|
||||
}
|
||||
if (col_sep_len && !STREQ (col_sep, optarg))
|
||||
error (EXIT_FAILURE, 0, _("multiple output delimiters specified"));
|
||||
col_sep = optarg;
|
||||
col_sep_len = *optarg ? strlen (optarg) : 1;
|
||||
break;
|
||||
|
||||
case_GETOPT_HELP_CHAR;
|
||||
@@ -437,6 +430,9 @@ main (int argc, char **argv)
|
||||
usage (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (! col_sep_len)
|
||||
col_sep_len = 1;
|
||||
|
||||
if (argc - optind < 2)
|
||||
{
|
||||
if (argc <= optind)
|
||||
@@ -452,10 +448,6 @@ main (int argc, char **argv)
|
||||
usage (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* The default delimiter is a TAB. */
|
||||
if (!delimiter)
|
||||
delimiter = "\t";
|
||||
|
||||
compare_files (argv + optind);
|
||||
|
||||
if (issued_disorder_warning[0] || issued_disorder_warning[1])
|
||||
|
||||
+7
-5
@@ -134,13 +134,15 @@ my @Tests =
|
||||
['delim-2char', '--output-delimiter=++', @inputs,
|
||||
{OUT=>"1\n++2\n++++3\n"} ],
|
||||
|
||||
# invalid empty delimiter
|
||||
['delim-empty', '--output-delimiter=', @inputs, {EXIT=>1},
|
||||
{ERR => "$prog: empty '--output-delimiter' not allowed\n"}],
|
||||
# NUL delimiter
|
||||
['delim-empty', '--output-delimiter=', @inputs,
|
||||
{OUT=>"1\n\0002\n\000\0003\n"} ],
|
||||
['zdelim-empty', '-z', '-z --output-delimiter=', @zinputs,
|
||||
{OUT=>"1\000\0002\000\000\0003\000"} ],
|
||||
|
||||
# invalid dual delimiter
|
||||
['delim-dual', '--output-delimiter=,', '--output-delimiter=+',
|
||||
@inputs, {EXIT=>1}, {ERR => "$prog: multiple delimiters specified\n"}],
|
||||
['delim-dual', '--output-delimiter=,', '--output-delimiter=+', @inputs,
|
||||
{EXIT=>1}, {ERR => "$prog: multiple output delimiters specified\n"}],
|
||||
|
||||
# valid dual delimiter specification
|
||||
['delim-dual2', '--output-delimiter=,', '--output-delimiter=,', @inputs,
|
||||
|
||||
Reference in New Issue
Block a user