bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: wrong option name in shuf


From: Jim Meyering
Subject: Re: wrong option name in shuf
Date: Sun, 27 Jul 2008 15:48:50 +0200

Mikael Magnusson <address@hidden> wrote:
> Both the help output and the man page says --head-count, so I changed
> the occurence in the source. I checked on savannah's gitweb and the
> bug appears there too (I'm using 6.12).
>
> --- a/src/shuf.c
> +++ b/src/shuf.c
> @@ -91,7 +91,7 @@ static struct option const long_opts[] =
>  {
>    {"echo", no_argument, NULL, 'e'},
>    {"input-range", required_argument, NULL, 'i'},
> -  {"head-count", required_argument, NULL, 'n'},
> +  {"head-lines", required_argument, NULL, 'n'},
>    {"output", required_argument, NULL, 'o'},
>    {"random-source", required_argument, NULL, RANDOM_SOURCE_OPTION},
>    {"zero-terminated", no_argument, NULL, 'z'},

Thank you for the report!

However, the code accepts the right name; the documentation is what's
wrong.  That is because the --head-count option applies even in a
context where the traditional newline separator is not used: with
the --zero-terminated (-z) option.  So using "line" as part of the
option name doesn't always make sense, whereas using "count" does.

Note that because of your report, I've just discovered a case in which
-z doesn't work, and have just fixed it.  Patch below.

If you'd like to fix the documentation (you'd update NEWS, shuf.c,
and doc/coreutils.texi), please take a look at the submission
guidelines here:

  http://git.sv.gnu.org/gitweb/?p=coreutils.git;a=blob;f=HACKING;hb=HEAD

With the patch below, this works:

    $ ./shuf --zero-terminated -i 1-5 --head-count=3 |cat -A; echo
    address@hidden@1^@

>From bee58d8a0400b0303c6dce3873fdf3482de0c110 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 27 Jul 2008 15:14:37 +0200
Subject: [PATCH] shuf: honor --zero-terminated option even with 
--input-range=LO-HI

* src/shuf.c (write_permuted_output): Add EOLBYTE parameter and use
it rather than hard-coding "\n".
(main): Adjust sole caller.
* tests/misc/shuf: Add a test to exercise this bug fix.
* NEWS: Mention it.
---
 NEWS            |    2 ++
 src/shuf.c      |    7 ++++---
 tests/misc/shuf |    5 +++++
 3 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index 8753fcf..3be1ad4 100644
--- a/NEWS
+++ b/NEWS
@@ -22,6 +22,8 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   md5sum now accepts the new option, --quiet, to suppress the printing of
   'OK' messages.  sha1sum, sha224sum, sha384sum, and sha512sum accept it, too.

+  shuf honors the --zero-terminated (-z) option, even with --input-range=LO-HI
+
   sort accepts a new option, --files0-from=F, that specifies a file
   containing a null-separated list of files to sort.  This list is used
   instead of filenames passed on the command-line to avoid problems with
diff --git a/src/shuf.c b/src/shuf.c
index ca5345b..5e07d6e 100644
--- a/src/shuf.c
+++ b/src/shuf.c
@@ -214,7 +214,7 @@ read_input (FILE *in, char eolbyte, char ***pline)

 static int
 write_permuted_output (size_t n_lines, char * const *line, size_t lo_input,
-                      size_t const *permutation)
+                      size_t const *permutation, char eolbyte)
 {
   size_t i;

@@ -230,7 +230,7 @@ write_permuted_output (size_t n_lines, char * const *line, 
size_t lo_input,
     for (i = 0; i < n_lines; i++)
       {
        unsigned long int n = lo_input + permutation[i];
-       if (printf ("%lu\n", n) < 0)
+       if (printf ("%lu%c", n, eolbyte) < 0)
          return -1;
       }

@@ -400,7 +400,8 @@ main (int argc, char **argv)

   if (outfile && ! freopen (outfile, "w", stdout))
     error (EXIT_FAILURE, errno, "%s", quotearg_colon (outfile));
-  if (write_permuted_output (head_lines, line, lo_input, permutation) != 0)
+  if (write_permuted_output (head_lines, line, lo_input, permutation, eolbyte)
+      != 0)
     error (EXIT_FAILURE, errno, _("write error"));

 #ifdef lint
diff --git a/tests/misc/shuf b/tests/misc/shuf
index 9710949..83530c2 100755
--- a/tests/misc/shuf
+++ b/tests/misc/shuf
@@ -51,4 +51,9 @@ test "$t" = 'a b c d e' || { fail=1; echo "not a permutation" 
1>&2; }
 # "seq 1860" produces 8193 (8K + 1) bytes of output.
 seq 1860 | shuf > /dev/null || fail=1

+# coreutils-6.12 and earlier would output a newline terminator, not \0.
+shuf --zero-terminated -i 1-1 > out || fail=1
+printf '1\0' > exp || framework_failure
+cmp out exp || { fail=1; echo "missing NUL terminator?" 1>&2; }
+
 (exit $fail); exit $fail
--
1.6.0.rc0.46.g2ac23




reply via email to

[Prev in Thread] Current Thread [Next in Thread]