]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
shuf: exit without reading if would never output
authorPádraig Brady <P@draigBrady.com>
Wed, 27 Mar 2013 11:06:00 +0000 (11:06 +0000)
committerPádraig Brady <P@draigBrady.com>
Thu, 4 Apr 2013 01:55:55 +0000 (02:55 +0100)
* src/shuf.c (main): If -n0 specified then no data would ever be output,
so exit without reading input.
* tests/misc/shuf.sh: Augment the related test with this case.

src/shuf.c
tests/misc/shuf.sh

index bbf3a86c273b8fa6fd502ad317fdf08219e64a06..0fabb0bfeb60b67b0106acea2c0cf7922756478c 100644 (file)
@@ -463,7 +463,8 @@ main (int argc, char **argv)
           break;
 
         case 1:
-          if (! (STREQ (operand[0], "-") || freopen (operand[0], "r", stdin)))
+          if (! (STREQ (operand[0], "-") || ! head_lines
+                 || freopen (operand[0], "r", stdin)))
             error (EXIT_FAILURE, errno, "%s", operand[0]);
           break;
 
@@ -474,7 +475,8 @@ main (int argc, char **argv)
 
       fadvise (stdin, FADVISE_SEQUENTIAL);
 
-      if (head_lines != SIZE_MAX && input_size () > RESERVOIR_MIN_INPUT)
+      if (head_lines != SIZE_MAX && (! head_lines
+                                     || input_size () > RESERVOIR_MIN_INPUT))
         {
           use_reservoir_sampling = true;
           n_lines = SIZE_MAX;   /* unknown number of input lines, for now.  */
index 9c9526d4d0dc7abdfd19f5485322aa7affc2393c..171b25a1fdf69bb4ea8217597f95312c60a0500e 100755 (executable)
@@ -57,4 +57,10 @@ cmp out exp || { fail=1; echo "missing NUL terminator?" 1>&2; }
 timeout 10 shuf -i1-$SIZE_MAX -n2 >/dev/null ||
   { fail=1; echo "couldn't get a small subset" >&2; }
 
+# Ensure shuf -n0 doesn't read any input or open specified files
+touch unreadable || framework_failure_
+chmod 0 unreadable || framework_failure_
+shuf -n0 unreadable || fail=1
+shuf -n1 unreadable && fail=1
+
 Exit $fail