summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-08-29 14:39:01 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-08-29 14:39:01 +0200
commit49a2e484b5bd3f6343e55bfed823d3ca6bd5d45a (patch)
treeda7a4e7e7215aab59680a487a02982a6b7bc40b9 /coreutils
parent60f4843468213324cc348af9d8ec09648b6f6784 (diff)
downloadbusybox-49a2e484b5bd3f6343e55bfed823d3ca6bd5d45a.tar.gz
shuf: in -i RANGE, accept numbers up to width of pointers
function old new delta .rodata 108468 108474 +6 shuf_main 555 542 -13 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/shuf.c38
1 files changed, 28 insertions, 10 deletions
diff --git a/coreutils/shuf.c b/coreutils/shuf.c
index fc9635147..77f8a8ff9 100644
--- a/coreutils/shuf.c
+++ b/coreutils/shuf.c
@@ -90,7 +90,7 @@ int shuf_main(int argc, char **argv)
if (opts & OPT_i) {
/* create a range of numbers */
char *dash;
- unsigned lo, hi;
+ uintptr_t lo, hi;
if (argv[0])
bb_show_usage();
@@ -100,8 +100,17 @@ int shuf_main(int argc, char **argv)
bb_error_msg_and_die("bad range '%s'", opt_i_str);
}
*dash = '\0';
- lo = xatou(opt_i_str);
- hi = xatou(dash + 1);
+ 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);
+ }
*dash = '-';
if (hi < lo) {
bb_error_msg_and_die("bad range '%s'", opt_i_str);
@@ -110,17 +119,21 @@ 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] = (char*)(uintptr_t)lo;
+ lines[i] = (char*)lo;
lo++;
}
} else {
/* default - read lines from stdin or the input file */
FILE *fp;
+ const char *fname = "-";
- if (argc > 1)
- bb_show_usage();
+ if (argv[0]) {
+ if (argv[1])
+ bb_show_usage();
+ fname = argv[0];
+ }
- fp = xfopen_stdin(argv[0] ? argv[0] : "-");
+ fp = xfopen_stdin(fname);
lines = NULL;
numlines = 0;
for (;;) {
@@ -150,9 +163,14 @@ int shuf_main(int argc, char **argv)
eol = '\0';
for (i = numlines - outlines; i < numlines; i++) {
- if (opts & OPT_i)
- printf("%u%c", (unsigned)(uintptr_t)lines[i], eol);
- else
+ 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
printf("%s%c", lines[i], eol);
}