diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2021-09-04 17:00:22 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-09-04 17:00:22 +0200 |
commit | f4ba69d47698c5357c32e21eb9122a5031b9b080 (patch) | |
tree | c7397cfac0a9618560fd96285b2075549cb83323 /coreutils | |
parent | 8aa626ffffbe7f7dfa6db8a37a0f841ce777085d (diff) | |
download | busybox-w32-f4ba69d47698c5357c32e21eb9122a5031b9b080.tar.gz busybox-w32-f4ba69d47698c5357c32e21eb9122a5031b9b080.tar.bz2 busybox-w32-f4ba69d47698c5357c32e21eb9122a5031b9b080.zip |
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>
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/shuf.c | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/coreutils/shuf.c b/coreutils/shuf.c index 77f8a8ff9..50483a25e 100644 --- a/coreutils/shuf.c +++ b/coreutils/shuf.c | |||
@@ -67,9 +67,10 @@ int shuf_main(int argc, char **argv) | |||
67 | { | 67 | { |
68 | unsigned opts; | 68 | unsigned opts; |
69 | char *opt_i_str, *opt_n_str, *opt_o_str; | 69 | char *opt_i_str, *opt_n_str, *opt_o_str; |
70 | unsigned i; | ||
71 | char **lines; | 70 | char **lines; |
71 | unsigned long long lo = lo; | ||
72 | unsigned numlines, outlines; | 72 | unsigned numlines, outlines; |
73 | unsigned i; | ||
73 | char eol; | 74 | char eol; |
74 | 75 | ||
75 | opts = getopt32(argv, "^" | 76 | opts = getopt32(argv, "^" |
@@ -89,8 +90,8 @@ int shuf_main(int argc, char **argv) | |||
89 | } else | 90 | } else |
90 | if (opts & OPT_i) { | 91 | if (opts & OPT_i) { |
91 | /* create a range of numbers */ | 92 | /* create a range of numbers */ |
93 | unsigned long long hi; | ||
92 | char *dash; | 94 | char *dash; |
93 | uintptr_t lo, hi; | ||
94 | 95 | ||
95 | if (argv[0]) | 96 | if (argv[0]) |
96 | bb_show_usage(); | 97 | bb_show_usage(); |
@@ -100,27 +101,24 @@ int shuf_main(int argc, char **argv) | |||
100 | bb_error_msg_and_die("bad range '%s'", opt_i_str); | 101 | bb_error_msg_and_die("bad range '%s'", opt_i_str); |
101 | } | 102 | } |
102 | *dash = '\0'; | 103 | *dash = '\0'; |
103 | if (sizeof(lo) == sizeof(int)) { | 104 | lo = xatoull(opt_i_str); |
104 | lo = xatou(opt_i_str); | 105 | hi = xatoull(dash + 1); |
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 | } | ||
114 | *dash = '-'; | 106 | *dash = '-'; |
115 | if (hi < lo) { | 107 | if (hi < lo) |
116 | bb_error_msg_and_die("bad range '%s'", opt_i_str); | 108 | bb_error_msg_and_die("bad range '%s'", opt_i_str); |
109 | 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); | ||
117 | } | 116 | } |
118 | 117 | ||
119 | numlines = (hi+1) - lo; | 118 | numlines = hi + 1; |
120 | lines = xmalloc(numlines * sizeof(lines[0])); | 119 | lines = xmalloc((size_t)numlines * sizeof(lines[0])); |
121 | for (i = 0; i < numlines; i++) { | 120 | for (i = 0; i < numlines; i++) { |
122 | lines[i] = (char*)lo; | 121 | lines[i] = (char*)(uintptr_t)i; |
123 | lo++; | ||
124 | } | 122 | } |
125 | } else { | 123 | } else { |
126 | /* default - read lines from stdin or the input file */ | 124 | /* default - read lines from stdin or the input file */ |
@@ -163,14 +161,9 @@ int shuf_main(int argc, char **argv) | |||
163 | eol = '\0'; | 161 | eol = '\0'; |
164 | 162 | ||
165 | for (i = numlines - outlines; i < numlines; i++) { | 163 | for (i = numlines - outlines; i < numlines; i++) { |
166 | if (opts & OPT_i) { | 164 | if (opts & OPT_i) |
167 | if (sizeof(lines[0]) == sizeof(int)) | 165 | printf("%llu%c", lo + (uintptr_t)lines[i], eol); |
168 | printf("%u%c", (unsigned)(uintptr_t)lines[i], eol); | 166 | else |
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 | ||
174 | printf("%s%c", lines[i], eol); | 167 | printf("%s%c", lines[i], eol); |
175 | } | 168 | } |
176 | 169 | ||