shuf: make -i 99999999990-100000000000 work even on 32 bits

function                                             old     new   delta
shuf_main                                            443     501     +58
.rodata                                           104238  104245      +7
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 65/0)               Total: 65 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2021-09-04 17:00:22 +02:00
parent 8aa626ffff
commit f4ba69d476

View File

@ -67,9 +67,10 @@ int shuf_main(int argc, char **argv)
{ {
unsigned opts; unsigned opts;
char *opt_i_str, *opt_n_str, *opt_o_str; char *opt_i_str, *opt_n_str, *opt_o_str;
unsigned i;
char **lines; char **lines;
unsigned long long lo = lo;
unsigned numlines, outlines; unsigned numlines, outlines;
unsigned i;
char eol; char eol;
opts = getopt32(argv, "^" opts = getopt32(argv, "^"
@ -89,8 +90,8 @@ int shuf_main(int argc, char **argv)
} else } else
if (opts & OPT_i) { if (opts & OPT_i) {
/* create a range of numbers */ /* create a range of numbers */
unsigned long long hi;
char *dash; char *dash;
uintptr_t lo, hi;
if (argv[0]) if (argv[0])
bb_show_usage(); bb_show_usage();
@ -100,27 +101,24 @@ int shuf_main(int argc, char **argv)
bb_error_msg_and_die("bad range '%s'", opt_i_str); bb_error_msg_and_die("bad range '%s'", opt_i_str);
} }
*dash = '\0'; *dash = '\0';
if (sizeof(lo) == sizeof(int)) { lo = xatoull(opt_i_str);
lo = xatou(opt_i_str); hi = xatoull(dash + 1);
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 = '-'; *dash = '-';
if (hi < lo) { if (hi < lo)
bb_error_msg_and_die("bad range '%s'", opt_i_str); bb_error_msg_and_die("bad range '%s'", opt_i_str);
hi -= lo;
if (sizeof(size_t) > sizeof(numlines)) {
if (hi >= UINT_MAX)
bb_error_msg_and_die("bad range '%s'", opt_i_str);
} else {
if (hi >= UINT_MAX / sizeof(lines[0]))
bb_error_msg_and_die("bad range '%s'", opt_i_str);
} }
numlines = (hi+1) - lo; numlines = hi + 1;
lines = xmalloc(numlines * sizeof(lines[0])); lines = xmalloc((size_t)numlines * sizeof(lines[0]));
for (i = 0; i < numlines; i++) { for (i = 0; i < numlines; i++) {
lines[i] = (char*)lo; lines[i] = (char*)(uintptr_t)i;
lo++;
} }
} else { } else {
/* default - read lines from stdin or the input file */ /* default - read lines from stdin or the input file */
@ -163,14 +161,9 @@ int shuf_main(int argc, char **argv)
eol = '\0'; eol = '\0';
for (i = numlines - outlines; i < numlines; i++) { for (i = numlines - outlines; i < numlines; i++) {
if (opts & OPT_i) { if (opts & OPT_i)
if (sizeof(lines[0]) == sizeof(int)) printf("%llu%c", lo + (uintptr_t)lines[i], eol);
printf("%u%c", (unsigned)(uintptr_t)lines[i], eol); else
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); printf("%s%c", lines[i], eol);
} }