]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
comm: support NUL --output-delimiter for consistency
authorPádraig Brady <P@draigBrady.com>
Fri, 8 Jan 2016 15:42:56 +0000 (15:42 +0000)
committerPádraig Brady <P@draigBrady.com>
Wed, 13 Jan 2016 10:59:56 +0000 (10:59 +0000)
* 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
tests/misc/comm.pl

index e66ac81ef2f591cc75ac2cfa585e38115c15d4ab..802bf909adbca9a40b6fcad8303b046aecc2edc3 100644 (file)
@@ -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])
index 3232d6339a838cc34c6d65e5ad1c16ba796d50a7..c5cd27f39940a93675b6dc5f319877404f6cbae4 100755 (executable)
@@ -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,