aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2014-03-05 18:56:20 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2014-03-05 18:56:20 +0100
commit2cdcb1082c3de109eef564795feb5e8a368a88cf (patch)
tree0864105c496fe3101e1e20da8fabb70fc4c93e6a
parent30f17e9556e9a415616515db738c063159d662a8 (diff)
downloadbusybox-w32-2cdcb1082c3de109eef564795feb5e8a368a88cf.tar.gz
busybox-w32-2cdcb1082c3de109eef564795feb5e8a368a88cf.tar.bz2
busybox-w32-2cdcb1082c3de109eef564795feb5e8a368a88cf.zip
shuf: new applet
function old new delta shuf_main - 478 +478 packed_usage 29571 29719 +148 applet_names 2460 2465 +5 applet_main 1428 1432 +4 applet_nameofs 714 716 +2 ------------------------------------------------------------------------------ (add/remove: 2/0 grow/shrink: 4/0 up/down: 637/0) Total: 637 bytes Based on the code by Bartosz Golaszewski <bartekgola@gmail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/shuf.c150
1 files changed, 150 insertions, 0 deletions
diff --git a/coreutils/shuf.c b/coreutils/shuf.c
new file mode 100644
index 000000000..f213d6759
--- /dev/null
+++ b/coreutils/shuf.c
@@ -0,0 +1,150 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * shuf: Write a random permutation of the input lines to standard output.
4 *
5 * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola@gmail.com>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9
10//config:config SHUF
11//config: bool "shuf"
12//config: default y
13//config: help
14//config: Generate random permutations
15
16//kbuild:lib-$(CONFIG_SHUF) += shuf.o
17//applet:IF_SHUF(APPLET_NOEXEC(shuf, shuf, BB_DIR_USR_BIN, BB_SUID_DROP, shuf))
18
19//usage:#define shuf_trivial_usage
20//usage: "[-e|-i LO-HI] [-n NUM] [-o FILE] [-z] [FILE|ARG...]"
21//usage:#define shuf_full_usage "\n\n"
22//usage: "Write a random permutation of the input lines to standard output\n"
23//usage: "\n -e Treat each ARG as an input line"
24//usage: "\n -i L-H Treat each number L through H as an input line"
25//usage: "\n -n NUM Output at most NUM lines"
26//usage: "\n -o Write result to FILE instead of standard output"
27//usage: "\n -z End lines with zero byte, not newline"
28
29#include "libbb.h"
30
31/* This is a NOEXEC applet. Be very careful! */
32
33#define OPT_e (1 << 0)
34#define OPT_i (1 << 1)
35#define OPT_n (1 << 2)
36#define OPT_o (1 << 3)
37#define OPT_z (1 << 4)
38#define OPT_STR "ei:n:o:z"
39
40/*
41 * Use the Fisher-Yates shuffle algorithm on an array of lines.
42 */
43static void shuffle_lines(char **lines, unsigned numlines)
44{
45 unsigned i;
46 unsigned r;
47 char *tmp;
48
49 srand(monotonic_us());
50
51 for (i = numlines-1; i > 0; i--) {
52 r = rand();
53 /* RAND_MAX can be as small as 32767 */
54 if (i > RAND_MAX)
55 r ^= rand() << 15;
56 r %= i;
57 tmp = lines[i];
58 lines[i] = lines[r];
59 lines[r] = tmp;
60 }
61}
62
63int shuf_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
64int shuf_main(int argc, char **argv)
65{
66 unsigned opts;
67 char *opt_i_str, *opt_n_str, *opt_o_str;
68 unsigned i;
69 char **lines;
70 unsigned numlines;
71 char eol;
72
73 opt_complementary = "e--i:i--e"; /* mutually exclusive */
74 opts = getopt32(argv, OPT_STR, &opt_i_str, &opt_n_str, &opt_o_str);
75
76 argc -= optind;
77 argv += optind;
78
79 /* Prepare lines for shuffling - either: */
80 if (opts & OPT_e) {
81 /* make lines from command-line arguments */
82
83 numlines = argc;
84 lines = argv;
85 } else
86 if (opts & OPT_i) {
87 /* create a range of numbers */
88 char *dash;
89 unsigned lo, hi;
90
91 dash = strchr(opt_i_str, '-');
92 if (!dash) {
93 bb_error_msg_and_die("bad range '%s'", opt_i_str);
94 }
95 *dash = '\0';
96 lo = xatou(opt_i_str);
97 hi = xatou(dash + 1);
98 *dash = '-';
99 if (hi < lo) {
100 bb_error_msg_and_die("bad range '%s'", opt_i_str);
101 }
102
103 numlines = (hi+1) - lo;
104 lines = xmalloc(numlines * sizeof(lines[0]));
105 for (i = 0; i < numlines; i++) {
106 lines[i] = xstrdup(utoa(lo));
107 lo++;
108 }
109 } else {
110 /* default - read lines from stdin or the input file */
111 FILE *fp;
112
113 if (argc > 1)
114 bb_show_usage();
115
116 fp = xfopen_stdin(argv[0] ? argv[0] : "-");
117 lines = NULL;
118 numlines = 0;
119 for (;;) {
120 char *line = xmalloc_fgetline(fp);
121 if (!line)
122 break;
123 lines = xrealloc_vector(lines, 6, numlines);
124 lines[numlines++] = line;
125 }
126 fclose_if_not_stdin(fp);
127 }
128
129 shuffle_lines(lines, numlines);
130
131 if (opts & OPT_o)
132 xmove_fd(xopen(opt_o_str, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
133
134 if (opts & OPT_n) {
135 unsigned maxlines;
136 maxlines = xatou(opt_n_str);
137 if (numlines > maxlines)
138 numlines = maxlines;
139 }
140
141 eol = '\n';
142 if (opts & OPT_z)
143 eol = '\0';
144
145 for (i = 0; i < numlines; i++) {
146 printf("%s%c", lines[i], eol);
147 }
148
149 fflush_stdout_and_exit(EXIT_SUCCESS);
150}