]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
comm: add the -z,--zero-terminated option
authorPádraig Brady <P@draigBrady.com>
Fri, 8 Jan 2016 15:14:01 +0000 (15:14 +0000)
committerPádraig Brady <P@draigBrady.com>
Wed, 13 Jan 2016 10:59:56 +0000 (10:59 +0000)
* doc/coreutils.texi (comm invocation): Reference option description.
* src/comm.c (main): Use readlinebuffer_delim() to support
a parameterized delimiter.
* tests/misc/comm.pl: Add test cases.
* NEWS: Mention the new feature.

NEWS
doc/coreutils.texi
src/comm.c
tests/misc/comm.pl

diff --git a/NEWS b/NEWS
index 408872b7e7a15d455b73892b0d210ce93ab4904b..27789f761cca5b00ef0db7b885d26e74d243e3f7 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -33,7 +33,7 @@ GNU coreutils NEWS                                    -*- outline -*-
 
 ** New features
 
-  cut, head, tail now have the -z,--zero-terminated option, and
+  comm, cut, head, tail now have the -z,--zero-terminated option, and
   tac --separator accepts an empty argument, to work with NUL delimited items.
 
   dd now summarizes sizes in --human-readable format too, not just --si.
index 09020275cb3c755bb9a25fb2d0914b60c3318c44..157ce0e12e749e083a141ec287e83f6eaadc67f7 100644 (file)
@@ -5130,6 +5130,8 @@ rather than the default of a single TAB character.
 
 The delimiter @var{str} may not be empty.
 
+@optZeroTerminated
+
 @end table
 
 @node ptx invocation
index 89cee88fca22bf07249e82642299dffa9e25196a..e66ac81ef2f591cc75ac2cfa585e38115c15d4ab 100644 (file)
@@ -59,6 +59,9 @@ static bool seen_unpairable;
 /* If nonzero, we have warned about disorder in that file. */
 static bool issued_disorder_warning[2];
 
+/* line delimiter.  */
+static unsigned char delim = '\n';
+
 /* If nonzero, check that the input is correctly ordered. */
 static enum
   {
@@ -86,6 +89,7 @@ static struct option const long_options[] =
   {"check-order", no_argument, NULL, CHECK_ORDER_OPTION},
   {"nocheck-order", no_argument, NULL, NOCHECK_ORDER_OPTION},
   {"output-delimiter", required_argument, NULL, OUTPUT_DELIMITER_OPTION},
+  {"zero-terminated", no_argument, NULL, 'z'},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -130,6 +134,9 @@ and column three contains lines common to both files.\n\
 "), stdout);
       fputs (_("\
   --output-delimiter=STR  separate columns with STR\n\
+"), stdout);
+      fputs (_("\
+  -z, --zero-terminated    line delimiter is NUL, not newline\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
@@ -277,7 +284,8 @@ compare_files (char **infiles)
 
       fadvise (streams[i], FADVISE_SEQUENTIAL);
 
-      thisline[i] = readlinebuffer (all_line[i][alt[i][0]], streams[i]);
+      thisline[i] = readlinebuffer_delim (all_line[i][alt[i][0]], streams[i],
+                                          delim);
       if (ferror (streams[i]))
         error (EXIT_FAILURE, errno, "%s", quotef (infiles[i]));
     }
@@ -336,7 +344,8 @@ compare_files (char **infiles)
             alt[i][1] = alt[i][0];
             alt[i][0] = (alt[i][0] + 1) & 0x03;
 
-            thisline[i] = readlinebuffer (all_line[i][alt[i][0]], streams[i]);
+            thisline[i] = readlinebuffer_delim (all_line[i][alt[i][0]],
+                                                streams[i], delim);
 
             if (thisline[i])
               check_order (all_line[i][alt[i][1]], thisline[i], i + 1);
@@ -382,7 +391,7 @@ main (int argc, char **argv)
   issued_disorder_warning[0] = issued_disorder_warning[1] = false;
   check_input_order = CHECK_ORDER_DEFAULT;
 
-  while ((c = getopt_long (argc, argv, "123", long_options, NULL)) != -1)
+  while ((c = getopt_long (argc, argv, "123z", long_options, NULL)) != -1)
     switch (c)
       {
       case '1':
@@ -397,6 +406,10 @@ main (int argc, char **argv)
         both = false;
         break;
 
+      case 'z':
+        delim = '\0';
+        break;
+
       case NOCHECK_ORDER_OPTION:
         check_input_order = CHECK_ORDER_DISABLED;
         break;
index 52d14ba5a0c205719c45365728d6275f4559b295..3232d6339a838cc34c6d65e5ad1c16ba796d50a7 100755 (executable)
@@ -28,14 +28,17 @@ my $prog = 'comm';
 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
 
 my @inputs = ({IN=>{a=>"1\n3"}}, {IN=>{b=>"2\n3"}});
+my @zinputs = ({IN=>{za=>"1\0003"}}, {IN=>{zb=>"2\0003"}});
 
 my @Tests =
   (
    # basic operation
    ['basic', @inputs, {OUT=>"1\n\t2\n\t\t3\n"} ],
+   ['zbasic', '-z', @zinputs, {OUT=>"1\0\t2\0\t\t3\0"} ],
 
    # suppress lines unique to file 1
    ['opt-1', '-1', @inputs, {OUT=>"2\n\t3\n"} ],
+   ['zopt-1', '-z', '-1', @zinputs, {OUT=>"2\0\t3\0"} ],
 
    # suppress lines unique to file 2
    ['opt-2', '-2', @inputs, {OUT=>"1\n\t3\n"} ],