diff options
author | Ron Yorston <rmy@pobox.com> | 2021-08-30 08:37:51 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2021-08-30 08:37:51 +0100 |
commit | bdb10f66af52bbd4f9d9340f2cf8ca73adbaee91 (patch) | |
tree | 306810231da90fb758cacb38ee9f8a7c4adf0d7c /coreutils | |
parent | 6a42f73dcac55a6b5dde810ad60cee7c120a2db7 (diff) | |
parent | a51d953b95a7cc6b40a6b3a5bfd95f3154acf5e2 (diff) | |
download | busybox-w32-bdb10f66af52bbd4f9d9340f2cf8ca73adbaee91.tar.gz busybox-w32-bdb10f66af52bbd4f9d9340f2cf8ca73adbaee91.tar.bz2 busybox-w32-bdb10f66af52bbd4f9d9340f2cf8ca73adbaee91.zip |
Merge branch 'busybox' into merge
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/shuf.c | 76 |
1 files changed, 49 insertions, 27 deletions
diff --git a/coreutils/shuf.c b/coreutils/shuf.c index fdbd3e9b2..77f8a8ff9 100644 --- a/coreutils/shuf.c +++ b/coreutils/shuf.c | |||
@@ -17,14 +17,14 @@ | |||
17 | //kbuild:lib-$(CONFIG_SHUF) += shuf.o | 17 | //kbuild:lib-$(CONFIG_SHUF) += shuf.o |
18 | 18 | ||
19 | //usage:#define shuf_trivial_usage | 19 | //usage:#define shuf_trivial_usage |
20 | //usage: "[-e|-i L-H] [-n NUM] [-o FILE] [-z] [FILE|ARG...]" | 20 | //usage: "[-n NUM] [-o FILE] [-z] [FILE | -e [ARG...] | -i L-H]" |
21 | //usage:#define shuf_full_usage "\n\n" | 21 | //usage:#define shuf_full_usage "\n\n" |
22 | //usage: "Randomly permute lines\n" | 22 | //usage: "Randomly permute lines\n" |
23 | //usage: "\n -e Treat ARGs as lines" | ||
24 | //usage: "\n -i L-H Treat numbers L-H as lines" | ||
25 | //usage: "\n -n NUM Output at most NUM lines" | 23 | //usage: "\n -n NUM Output at most NUM lines" |
26 | //usage: "\n -o FILE Write to FILE, not standard output" | 24 | //usage: "\n -o FILE Write to FILE, not standard output" |
27 | //usage: "\n -z End lines with zero byte, not newline" | 25 | //usage: "\n -z NUL terminated output" |
26 | //usage: "\n -e Treat ARGs as lines" | ||
27 | //usage: "\n -i L-H Treat numbers L-H as lines" | ||
28 | 28 | ||
29 | #include "libbb.h" | 29 | #include "libbb.h" |
30 | 30 | ||
@@ -39,8 +39,10 @@ | |||
39 | 39 | ||
40 | /* | 40 | /* |
41 | * Use the Fisher-Yates shuffle algorithm on an array of lines. | 41 | * Use the Fisher-Yates shuffle algorithm on an array of lines. |
42 | * If the required number of output lines is less than the total | ||
43 | * we can stop shuffling early. | ||
42 | */ | 44 | */ |
43 | static void shuffle_lines(char **lines, unsigned numlines) | 45 | static void shuffle_lines(char **lines, unsigned numlines, unsigned outlines) |
44 | { | 46 | { |
45 | unsigned i; | 47 | unsigned i; |
46 | unsigned r; | 48 | unsigned r; |
@@ -48,7 +50,7 @@ static void shuffle_lines(char **lines, unsigned numlines) | |||
48 | 50 | ||
49 | srand(monotonic_us()); | 51 | srand(monotonic_us()); |
50 | 52 | ||
51 | for (i = numlines-1; i > 0; i--) { | 53 | for (i = numlines - 1; outlines > 0; i--, outlines--) { |
52 | r = rand(); | 54 | r = rand(); |
53 | /* RAND_MAX can be as small as 32767 */ | 55 | /* RAND_MAX can be as small as 32767 */ |
54 | if (i > RAND_MAX) | 56 | if (i > RAND_MAX) |
@@ -67,7 +69,7 @@ int shuf_main(int argc, char **argv) | |||
67 | char *opt_i_str, *opt_n_str, *opt_o_str; | 69 | char *opt_i_str, *opt_n_str, *opt_o_str; |
68 | unsigned i; | 70 | unsigned i; |
69 | char **lines; | 71 | char **lines; |
70 | unsigned numlines; | 72 | unsigned numlines, outlines; |
71 | char eol; | 73 | char eol; |
72 | 74 | ||
73 | opts = getopt32(argv, "^" | 75 | opts = getopt32(argv, "^" |
@@ -88,15 +90,27 @@ int shuf_main(int argc, char **argv) | |||
88 | if (opts & OPT_i) { | 90 | if (opts & OPT_i) { |
89 | /* create a range of numbers */ | 91 | /* create a range of numbers */ |
90 | char *dash; | 92 | char *dash; |
91 | unsigned lo, hi; | 93 | uintptr_t lo, hi; |
94 | |||
95 | if (argv[0]) | ||
96 | bb_show_usage(); | ||
92 | 97 | ||
93 | dash = strchr(opt_i_str, '-'); | 98 | dash = strchr(opt_i_str, '-'); |
94 | if (!dash) { | 99 | if (!dash) { |
95 | bb_error_msg_and_die("bad range '%s'", opt_i_str); | 100 | bb_error_msg_and_die("bad range '%s'", opt_i_str); |
96 | } | 101 | } |
97 | *dash = '\0'; | 102 | *dash = '\0'; |
98 | lo = xatou(opt_i_str); | 103 | if (sizeof(lo) == sizeof(int)) { |
99 | hi = xatou(dash + 1); | 104 | lo = xatou(opt_i_str); |
105 | hi = xatou(dash + 1); | ||
106 | } else | ||
107 | if (sizeof(lo) == sizeof(long)) { | ||
108 | lo = xatoul(opt_i_str); | ||
109 | hi = xatoul(dash + 1); | ||
110 | } else { | ||
111 | lo = xatoull(opt_i_str); | ||
112 | hi = xatoull(dash + 1); | ||
113 | } | ||
100 | *dash = '-'; | 114 | *dash = '-'; |
101 | if (hi < lo) { | 115 | if (hi < lo) { |
102 | bb_error_msg_and_die("bad range '%s'", opt_i_str); | 116 | bb_error_msg_and_die("bad range '%s'", opt_i_str); |
@@ -105,17 +119,21 @@ int shuf_main(int argc, char **argv) | |||
105 | numlines = (hi+1) - lo; | 119 | numlines = (hi+1) - lo; |
106 | lines = xmalloc(numlines * sizeof(lines[0])); | 120 | lines = xmalloc(numlines * sizeof(lines[0])); |
107 | for (i = 0; i < numlines; i++) { | 121 | for (i = 0; i < numlines; i++) { |
108 | lines[i] = (char*)(uintptr_t)lo; | 122 | lines[i] = (char*)lo; |
109 | lo++; | 123 | lo++; |
110 | } | 124 | } |
111 | } else { | 125 | } else { |
112 | /* default - read lines from stdin or the input file */ | 126 | /* default - read lines from stdin or the input file */ |
113 | FILE *fp; | 127 | FILE *fp; |
128 | const char *fname = "-"; | ||
114 | 129 | ||
115 | if (argc > 1) | 130 | if (argv[0]) { |
116 | bb_show_usage(); | 131 | if (argv[1]) |
132 | bb_show_usage(); | ||
133 | fname = argv[0]; | ||
134 | } | ||
117 | 135 | ||
118 | fp = xfopen_stdin(argv[0] ? argv[0] : "-"); | 136 | fp = xfopen_stdin(fname); |
119 | lines = NULL; | 137 | lines = NULL; |
120 | numlines = 0; | 138 | numlines = 0; |
121 | for (;;) { | 139 | for (;;) { |
@@ -128,27 +146,31 @@ int shuf_main(int argc, char **argv) | |||
128 | fclose_if_not_stdin(fp); | 146 | fclose_if_not_stdin(fp); |
129 | } | 147 | } |
130 | 148 | ||
131 | if (numlines != 0) | 149 | outlines = numlines; |
132 | shuffle_lines(lines, numlines); | 150 | if (opts & OPT_n) { |
151 | outlines = xatou(opt_n_str); | ||
152 | if (outlines > numlines) | ||
153 | outlines = numlines; | ||
154 | } | ||
155 | |||
156 | shuffle_lines(lines, numlines, outlines); | ||
133 | 157 | ||
134 | if (opts & OPT_o) | 158 | if (opts & OPT_o) |
135 | xmove_fd(xopen(opt_o_str, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO); | 159 | xmove_fd(xopen(opt_o_str, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO); |
136 | 160 | ||
137 | if (opts & OPT_n) { | ||
138 | unsigned maxlines; | ||
139 | maxlines = xatou(opt_n_str); | ||
140 | if (numlines > maxlines) | ||
141 | numlines = maxlines; | ||
142 | } | ||
143 | |||
144 | eol = '\n'; | 161 | eol = '\n'; |
145 | if (opts & OPT_z) | 162 | if (opts & OPT_z) |
146 | eol = '\0'; | 163 | eol = '\0'; |
147 | 164 | ||
148 | for (i = 0; i < numlines; i++) { | 165 | for (i = numlines - outlines; i < numlines; i++) { |
149 | if (opts & OPT_i) | 166 | if (opts & OPT_i) { |
150 | printf("%u%c", (unsigned)(uintptr_t)lines[i], eol); | 167 | if (sizeof(lines[0]) == sizeof(int)) |
151 | else | 168 | printf("%u%c", (unsigned)(uintptr_t)lines[i], eol); |
169 | else if (sizeof(lines[0]) == sizeof(long)) | ||
170 | printf("%lu%c", (unsigned long)(uintptr_t)lines[i], eol); | ||
171 | else | ||
172 | printf("%llu%c", (unsigned long long)(uintptr_t)lines[i], eol); | ||
173 | } else | ||
152 | printf("%s%c", lines[i], eol); | 174 | printf("%s%c", lines[i], eol); |
153 | } | 175 | } |
154 | 176 | ||