blob: 50483a25ed7c7bb4ed11e02ee57ce32a304315b6 [file] [log] [blame]
Denys Vlasenko2cdcb102014-03-05 18:56:20 +01001/* 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 */
Denys Vlasenko2cdcb102014-03-05 18:56:20 +01009//config:config SHUF
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "shuf (5.4 kb)"
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: Generate random permutations
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010014
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010015//applet:IF_SHUF(APPLET_NOEXEC(shuf, shuf, BB_DIR_USR_BIN, BB_SUID_DROP, shuf))
16
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020017//kbuild:lib-$(CONFIG_SHUF) += shuf.o
18
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010019//usage:#define shuf_trivial_usage
Denys Vlasenkod59f5392021-08-23 15:48:22 +020020//usage: "[-n NUM] [-o FILE] [-z] [FILE | -e [ARG...] | -i L-H]"
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010021//usage:#define shuf_full_usage "\n\n"
Denys Vlasenko69f95672014-03-07 14:41:53 +010022//usage: "Randomly permute lines\n"
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010023//usage: "\n -n NUM Output at most NUM lines"
Denys Vlasenko69f95672014-03-07 14:41:53 +010024//usage: "\n -o FILE Write to FILE, not standard output"
Ron Yorston8817e282021-08-07 09:41:49 +010025//usage: "\n -z NUL terminated output"
Denys Vlasenkod59f5392021-08-23 15:48:22 +020026//usage: "\n -e Treat ARGs as lines"
27//usage: "\n -i L-H Treat numbers L-H as lines"
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010028
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.
Ron Yorston8817e282021-08-07 09:41:49 +010042 * If the required number of output lines is less than the total
43 * we can stop shuffling early.
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010044 */
Ron Yorston8817e282021-08-07 09:41:49 +010045static void shuffle_lines(char **lines, unsigned numlines, unsigned outlines)
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010046{
47 unsigned i;
48 unsigned r;
49 char *tmp;
50
51 srand(monotonic_us());
52
Denys Vlasenkod59f5392021-08-23 15:48:22 +020053 for (i = numlines - 1; outlines > 0; i--, outlines--) {
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010054 r = rand();
55 /* RAND_MAX can be as small as 32767 */
56 if (i > RAND_MAX)
57 r ^= rand() << 15;
Denys Vlasenko9de9c872017-07-09 00:39:15 +020058 r %= i + 1;
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010059 tmp = lines[i];
60 lines[i] = lines[r];
61 lines[r] = tmp;
62 }
63}
64
65int shuf_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
66int shuf_main(int argc, char **argv)
67{
68 unsigned opts;
69 char *opt_i_str, *opt_n_str, *opt_o_str;
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010070 char **lines;
Denys Vlasenkof4ba69d2021-09-04 17:00:22 +020071 unsigned long long lo = lo;
Ron Yorston8817e282021-08-07 09:41:49 +010072 unsigned numlines, outlines;
Denys Vlasenkof4ba69d2021-09-04 17:00:22 +020073 unsigned i;
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010074 char eol;
75
Denys Vlasenko22542ec2017-08-08 21:55:02 +020076 opts = getopt32(argv, "^"
77 OPT_STR
78 "\0" "e--i:i--e"/* mutually exclusive */,
79 &opt_i_str, &opt_n_str, &opt_o_str
80 );
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010081
82 argc -= optind;
83 argv += optind;
84
85 /* Prepare lines for shuffling - either: */
86 if (opts & OPT_e) {
87 /* make lines from command-line arguments */
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010088 numlines = argc;
89 lines = argv;
90 } else
91 if (opts & OPT_i) {
92 /* create a range of numbers */
Denys Vlasenkof4ba69d2021-09-04 17:00:22 +020093 unsigned long long hi;
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010094 char *dash;
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010095
Denys Vlasenko60f48432021-08-23 15:52:34 +020096 if (argv[0])
97 bb_show_usage();
98
Denys Vlasenko2cdcb102014-03-05 18:56:20 +010099 dash = strchr(opt_i_str, '-');
100 if (!dash) {
101 bb_error_msg_and_die("bad range '%s'", opt_i_str);
102 }
103 *dash = '\0';
Denys Vlasenkof4ba69d2021-09-04 17:00:22 +0200104 lo = xatoull(opt_i_str);
105 hi = xatoull(dash + 1);
Maninder Singh7db312a2015-06-01 10:40:09 +0000106 *dash = '-';
Denys Vlasenkof4ba69d2021-09-04 17:00:22 +0200107 if (hi < lo)
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100108 bb_error_msg_and_die("bad range '%s'", opt_i_str);
Denys Vlasenkof4ba69d2021-09-04 17:00:22 +0200109 hi -= lo;
110 if (sizeof(size_t) > sizeof(numlines)) {
111 if (hi >= UINT_MAX)
112 bb_error_msg_and_die("bad range '%s'", opt_i_str);
113 } else {
114 if (hi >= UINT_MAX / sizeof(lines[0]))
115 bb_error_msg_and_die("bad range '%s'", opt_i_str);
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100116 }
117
Denys Vlasenkof4ba69d2021-09-04 17:00:22 +0200118 numlines = hi + 1;
119 lines = xmalloc((size_t)numlines * sizeof(lines[0]));
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100120 for (i = 0; i < numlines; i++) {
Denys Vlasenkof4ba69d2021-09-04 17:00:22 +0200121 lines[i] = (char*)(uintptr_t)i;
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100122 }
123 } else {
124 /* default - read lines from stdin or the input file */
125 FILE *fp;
Denys Vlasenko49a2e482021-08-29 14:39:01 +0200126 const char *fname = "-";
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100127
Denys Vlasenko49a2e482021-08-29 14:39:01 +0200128 if (argv[0]) {
129 if (argv[1])
130 bb_show_usage();
131 fname = argv[0];
132 }
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100133
Denys Vlasenko49a2e482021-08-29 14:39:01 +0200134 fp = xfopen_stdin(fname);
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100135 lines = NULL;
136 numlines = 0;
137 for (;;) {
138 char *line = xmalloc_fgetline(fp);
139 if (!line)
140 break;
141 lines = xrealloc_vector(lines, 6, numlines);
142 lines[numlines++] = line;
143 }
144 fclose_if_not_stdin(fp);
145 }
146
Ron Yorston8817e282021-08-07 09:41:49 +0100147 outlines = numlines;
148 if (opts & OPT_n) {
149 outlines = xatou(opt_n_str);
150 if (outlines > numlines)
151 outlines = numlines;
152 }
153
154 shuffle_lines(lines, numlines, outlines);
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100155
156 if (opts & OPT_o)
157 xmove_fd(xopen(opt_o_str, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
158
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100159 eol = '\n';
160 if (opts & OPT_z)
161 eol = '\0';
162
Ron Yorston8817e282021-08-07 09:41:49 +0100163 for (i = numlines - outlines; i < numlines; i++) {
Denys Vlasenkof4ba69d2021-09-04 17:00:22 +0200164 if (opts & OPT_i)
165 printf("%llu%c", lo + (uintptr_t)lines[i], eol);
166 else
Denys Vlasenko102f0d02014-03-07 14:32:39 +0100167 printf("%s%c", lines[i], eol);
Denys Vlasenko2cdcb102014-03-05 18:56:20 +0100168 }
169
170 fflush_stdout_and_exit(EXIT_SUCCESS);
171}