2014-03-05 23:26:20 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* shuf: Write a random permutation of the input lines to standard output.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 by Bartosz Golaszewski <bartekgola@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
//config:config SHUF
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "shuf (5.4 kb)"
|
2014-03-05 23:26:20 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Generate random permutations
|
2014-03-05 23:26:20 +05:30
|
|
|
|
|
|
|
//applet:IF_SHUF(APPLET_NOEXEC(shuf, shuf, BB_DIR_USR_BIN, BB_SUID_DROP, shuf))
|
|
|
|
|
2017-09-18 19:58:43 +05:30
|
|
|
//kbuild:lib-$(CONFIG_SHUF) += shuf.o
|
|
|
|
|
2014-03-05 23:26:20 +05:30
|
|
|
//usage:#define shuf_trivial_usage
|
2021-08-23 19:18:22 +05:30
|
|
|
//usage: "[-n NUM] [-o FILE] [-z] [FILE | -e [ARG...] | -i L-H]"
|
2014-03-05 23:26:20 +05:30
|
|
|
//usage:#define shuf_full_usage "\n\n"
|
2014-03-07 19:11:53 +05:30
|
|
|
//usage: "Randomly permute lines\n"
|
2014-03-05 23:26:20 +05:30
|
|
|
//usage: "\n -n NUM Output at most NUM lines"
|
2014-03-07 19:11:53 +05:30
|
|
|
//usage: "\n -o FILE Write to FILE, not standard output"
|
2021-08-07 14:11:49 +05:30
|
|
|
//usage: "\n -z NUL terminated output"
|
2021-08-23 19:18:22 +05:30
|
|
|
//usage: "\n -e Treat ARGs as lines"
|
|
|
|
//usage: "\n -i L-H Treat numbers L-H as lines"
|
2014-03-05 23:26:20 +05:30
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
|
|
|
/* This is a NOEXEC applet. Be very careful! */
|
|
|
|
|
|
|
|
#define OPT_e (1 << 0)
|
|
|
|
#define OPT_i (1 << 1)
|
|
|
|
#define OPT_n (1 << 2)
|
|
|
|
#define OPT_o (1 << 3)
|
|
|
|
#define OPT_z (1 << 4)
|
|
|
|
#define OPT_STR "ei:n:o:z"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use the Fisher-Yates shuffle algorithm on an array of lines.
|
2021-08-07 14:11:49 +05:30
|
|
|
* If the required number of output lines is less than the total
|
|
|
|
* we can stop shuffling early.
|
2014-03-05 23:26:20 +05:30
|
|
|
*/
|
2021-08-07 14:11:49 +05:30
|
|
|
static void shuffle_lines(char **lines, unsigned numlines, unsigned outlines)
|
2014-03-05 23:26:20 +05:30
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
unsigned r;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
srand(monotonic_us());
|
|
|
|
|
2021-08-23 19:18:22 +05:30
|
|
|
for (i = numlines - 1; outlines > 0; i--, outlines--) {
|
2014-03-05 23:26:20 +05:30
|
|
|
r = rand();
|
|
|
|
/* RAND_MAX can be as small as 32767 */
|
|
|
|
if (i > RAND_MAX)
|
|
|
|
r ^= rand() << 15;
|
2017-07-09 04:09:15 +05:30
|
|
|
r %= i + 1;
|
2014-03-05 23:26:20 +05:30
|
|
|
tmp = lines[i];
|
|
|
|
lines[i] = lines[r];
|
|
|
|
lines[r] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int shuf_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int shuf_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
unsigned opts;
|
|
|
|
char *opt_i_str, *opt_n_str, *opt_o_str;
|
|
|
|
unsigned i;
|
|
|
|
char **lines;
|
2021-08-07 14:11:49 +05:30
|
|
|
unsigned numlines, outlines;
|
2014-03-05 23:26:20 +05:30
|
|
|
char eol;
|
|
|
|
|
2017-08-09 01:25:02 +05:30
|
|
|
opts = getopt32(argv, "^"
|
|
|
|
OPT_STR
|
|
|
|
"\0" "e--i:i--e"/* mutually exclusive */,
|
|
|
|
&opt_i_str, &opt_n_str, &opt_o_str
|
|
|
|
);
|
2014-03-05 23:26:20 +05:30
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* Prepare lines for shuffling - either: */
|
|
|
|
if (opts & OPT_e) {
|
|
|
|
/* make lines from command-line arguments */
|
|
|
|
numlines = argc;
|
|
|
|
lines = argv;
|
|
|
|
} else
|
|
|
|
if (opts & OPT_i) {
|
|
|
|
/* create a range of numbers */
|
|
|
|
char *dash;
|
2021-08-29 18:09:01 +05:30
|
|
|
uintptr_t lo, hi;
|
2014-03-05 23:26:20 +05:30
|
|
|
|
2021-08-23 19:22:34 +05:30
|
|
|
if (argv[0])
|
|
|
|
bb_show_usage();
|
|
|
|
|
2014-03-05 23:26:20 +05:30
|
|
|
dash = strchr(opt_i_str, '-');
|
|
|
|
if (!dash) {
|
|
|
|
bb_error_msg_and_die("bad range '%s'", opt_i_str);
|
|
|
|
}
|
|
|
|
*dash = '\0';
|
2021-08-29 18:09:01 +05:30
|
|
|
if (sizeof(lo) == sizeof(int)) {
|
|
|
|
lo = xatou(opt_i_str);
|
|
|
|
hi = xatou(dash + 1);
|
|
|
|
} else
|
|
|
|
if (sizeof(lo) == sizeof(long)) {
|
|
|
|
lo = xatoul(opt_i_str);
|
|
|
|
hi = xatoul(dash + 1);
|
|
|
|
} else {
|
|
|
|
lo = xatoull(opt_i_str);
|
|
|
|
hi = xatoull(dash + 1);
|
|
|
|
}
|
2015-06-01 16:10:09 +05:30
|
|
|
*dash = '-';
|
2014-03-05 23:26:20 +05:30
|
|
|
if (hi < lo) {
|
|
|
|
bb_error_msg_and_die("bad range '%s'", opt_i_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
numlines = (hi+1) - lo;
|
|
|
|
lines = xmalloc(numlines * sizeof(lines[0]));
|
|
|
|
for (i = 0; i < numlines; i++) {
|
2021-08-29 18:09:01 +05:30
|
|
|
lines[i] = (char*)lo;
|
2014-03-05 23:26:20 +05:30
|
|
|
lo++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* default - read lines from stdin or the input file */
|
|
|
|
FILE *fp;
|
2021-08-29 18:09:01 +05:30
|
|
|
const char *fname = "-";
|
2014-03-05 23:26:20 +05:30
|
|
|
|
2021-08-29 18:09:01 +05:30
|
|
|
if (argv[0]) {
|
|
|
|
if (argv[1])
|
|
|
|
bb_show_usage();
|
|
|
|
fname = argv[0];
|
|
|
|
}
|
2014-03-05 23:26:20 +05:30
|
|
|
|
2021-08-29 18:09:01 +05:30
|
|
|
fp = xfopen_stdin(fname);
|
2014-03-05 23:26:20 +05:30
|
|
|
lines = NULL;
|
|
|
|
numlines = 0;
|
|
|
|
for (;;) {
|
|
|
|
char *line = xmalloc_fgetline(fp);
|
|
|
|
if (!line)
|
|
|
|
break;
|
|
|
|
lines = xrealloc_vector(lines, 6, numlines);
|
|
|
|
lines[numlines++] = line;
|
|
|
|
}
|
|
|
|
fclose_if_not_stdin(fp);
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:11:49 +05:30
|
|
|
outlines = numlines;
|
|
|
|
if (opts & OPT_n) {
|
|
|
|
outlines = xatou(opt_n_str);
|
|
|
|
if (outlines > numlines)
|
|
|
|
outlines = numlines;
|
|
|
|
}
|
|
|
|
|
|
|
|
shuffle_lines(lines, numlines, outlines);
|
2014-03-05 23:26:20 +05:30
|
|
|
|
|
|
|
if (opts & OPT_o)
|
|
|
|
xmove_fd(xopen(opt_o_str, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
|
|
|
|
|
|
|
|
eol = '\n';
|
|
|
|
if (opts & OPT_z)
|
|
|
|
eol = '\0';
|
|
|
|
|
2021-08-07 14:11:49 +05:30
|
|
|
for (i = numlines - outlines; i < numlines; i++) {
|
2021-08-29 18:09:01 +05:30
|
|
|
if (opts & OPT_i) {
|
|
|
|
if (sizeof(lines[0]) == sizeof(int))
|
|
|
|
printf("%u%c", (unsigned)(uintptr_t)lines[i], eol);
|
|
|
|
else if (sizeof(lines[0]) == sizeof(long))
|
|
|
|
printf("%lu%c", (unsigned long)(uintptr_t)lines[i], eol);
|
|
|
|
else
|
|
|
|
printf("%llu%c", (unsigned long long)(uintptr_t)lines[i], eol);
|
|
|
|
} else
|
2014-03-07 19:02:39 +05:30
|
|
|
printf("%s%c", lines[i], eol);
|
2014-03-05 23:26:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
|
|
|
}
|