bc: convert to "G trick" - this returns bc to zero bss increase
[oweals/busybox.git] / coreutils / shuf.c
index e0c2fbf1f9dfa4f1a776836b1666443de20b23e7..fdbd3e9b2ebca4f72d3900c3af306eae7a4fd63d 100644 (file)
@@ -6,24 +6,24 @@
  *
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
-
 //config:config SHUF
-//config:      bool "shuf"
+//config:      bool "shuf (5.4 kb)"
 //config:      default y
 //config:      help
-//config:        Generate random permutations
+//config:      Generate random permutations
 
-//kbuild:lib-$(CONFIG_SHUF) += shuf.o
 //applet:IF_SHUF(APPLET_NOEXEC(shuf, shuf, BB_DIR_USR_BIN, BB_SUID_DROP, shuf))
 
+//kbuild:lib-$(CONFIG_SHUF) += shuf.o
+
 //usage:#define shuf_trivial_usage
 //usage:       "[-e|-i L-H] [-n NUM] [-o FILE] [-z] [FILE|ARG...]"
 //usage:#define shuf_full_usage "\n\n"
-//usage:       "Write a random permutation of the input lines to standard output\n"
-//usage:     "\n       -e      Treat each ARG as an input line"
-//usage:     "\n       -i L-H  Treat numbers L-H as an input line"
+//usage:       "Randomly permute lines\n"
+//usage:     "\n       -e      Treat ARGs as lines"
+//usage:     "\n       -i L-H  Treat numbers L-H as lines"
 //usage:     "\n       -n NUM  Output at most NUM lines"
-//usage:     "\n       -o FILE Write to FILE instead of standard output"
+//usage:     "\n       -o FILE Write to FILE, not standard output"
 //usage:     "\n       -z      End lines with zero byte, not newline"
 
 #include "libbb.h"
@@ -53,7 +53,7 @@ static void shuffle_lines(char **lines, unsigned numlines)
                /* RAND_MAX can be as small as 32767 */
                if (i > RAND_MAX)
                        r ^= rand() << 15;
-               r %= i;
+               r %= i + 1;
                tmp = lines[i];
                lines[i] = lines[r];
                lines[r] = tmp;
@@ -70,8 +70,11 @@ int shuf_main(int argc, char **argv)
        unsigned numlines;
        char eol;
 
-       opt_complementary = "e--i:i--e"; /* mutually exclusive */
-       opts = getopt32(argv, OPT_STR, &opt_i_str, &opt_n_str, &opt_o_str);
+       opts = getopt32(argv, "^"
+                       OPT_STR
+                       "\0" "e--i:i--e"/* mutually exclusive */,
+                       &opt_i_str, &opt_n_str, &opt_o_str
+       );
 
        argc -= optind;
        argv += optind;
@@ -79,7 +82,6 @@ int shuf_main(int argc, char **argv)
        /* Prepare lines for shuffling - either: */
        if (opts & OPT_e) {
                /* make lines from command-line arguments */
-
                numlines = argc;
                lines = argv;
        } else
@@ -95,7 +97,7 @@ int shuf_main(int argc, char **argv)
                *dash = '\0';
                lo = xatou(opt_i_str);
                hi = xatou(dash + 1);
-               *dash = '-';
+               *dash = '-';
                if (hi < lo) {
                        bb_error_msg_and_die("bad range '%s'", opt_i_str);
                }
@@ -103,7 +105,7 @@ int shuf_main(int argc, char **argv)
                numlines = (hi+1) - lo;
                lines = xmalloc(numlines * sizeof(lines[0]));
                for (i = 0; i < numlines; i++) {
-                       lines[i] = xstrdup(utoa(lo));
+                       lines[i] = (char*)(uintptr_t)lo;
                        lo++;
                }
        } else {
@@ -144,7 +146,10 @@ int shuf_main(int argc, char **argv)
                eol = '\0';
 
        for (i = 0; i < numlines; i++) {
-               printf("%s%c", lines[i], eol);
+               if (opts & OPT_i)
+                       printf("%u%c", (unsigned)(uintptr_t)lines[i], eol);
+               else
+                       printf("%s%c", lines[i], eol);
        }
 
        fflush_stdout_and_exit(EXIT_SUCCESS);