From 0e46753d7c9519d7378cd3a4e0951a36ac32ffe7 Mon Sep 17 00:00:00 2001 From: Pádraig Brady Date: Fri, 8 Jan 2016 15:42:56 +0000 Subject: 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. --- src/comm.c | 34 +++++++++++++--------------------- tests/misc/comm.pl | 12 +++++++----- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/comm.c b/src/comm.c index e66ac81ef..802bf909a 100644 --- a/src/comm.c +++ b/src/comm.c @@ -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]) diff --git a/tests/misc/comm.pl b/tests/misc/comm.pl index 3232d6339..c5cd27f39 100755 --- a/tests/misc/comm.pl +++ b/tests/misc/comm.pl @@ -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, -- cgit v1.2.3-54-g00ecf