aboutsummaryrefslogtreecommitdiff
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
parent60f4843468213324cc348af9d8ec09648b6f6784 (diff)
downloadbusybox-w32-49a2e484b5bd3f6343e55bfed823d3ca6bd5d45a.tar.gz
busybox-w32-49a2e484b5bd3f6343e55bfed823d3ca6bd5d45a.tar.bz2
busybox-w32-49a2e484b5bd3f6343e55bfed823d3ca6bd5d45a.zip
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>
-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)
90 if (opts & OPT_i) { 90 if (opts & OPT_i) {
91 /* create a range of numbers */ 91 /* create a range of numbers */
92 char *dash; 92 char *dash;
93 unsigned lo, hi; 93 uintptr_t lo, hi;
94 94
95 if (argv[0]) 95 if (argv[0])
96 bb_show_usage(); 96 bb_show_usage();
@@ -100,8 +100,17 @@ int shuf_main(int argc, char **argv)
100 bb_error_msg_and_die("bad range '%s'", opt_i_str); 100 bb_error_msg_and_die("bad range '%s'", opt_i_str);
101 } 101 }
102 *dash = '\0'; 102 *dash = '\0';
103 lo = xatou(opt_i_str); 103 if (sizeof(lo) == sizeof(int)) {
104 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 }
105 *dash = '-'; 114 *dash = '-';
106 if (hi < lo) { 115 if (hi < lo) {
107 bb_error_msg_and_die("bad range '%s'", opt_i_str); 116 bb_error_msg_and_die("bad range '%s'", opt_i_str);
@@ -110,17 +119,21 @@ int shuf_main(int argc, char **argv)
110 numlines = (hi+1) - lo; 119 numlines = (hi+1) - lo;
111 lines = xmalloc(numlines * sizeof(lines[0])); 120 lines = xmalloc(numlines * sizeof(lines[0]));
112 for (i = 0; i < numlines; i++) { 121 for (i = 0; i < numlines; i++) {
113 lines[i] = (char*)(uintptr_t)lo; 122 lines[i] = (char*)lo;
114 lo++; 123 lo++;
115 } 124 }
116 } else { 125 } else {
117 /* default - read lines from stdin or the input file */ 126 /* default - read lines from stdin or the input file */
118 FILE *fp; 127 FILE *fp;
128 const char *fname = "-";
119 129
120 if (argc > 1) 130 if (argv[0]) {
121 bb_show_usage(); 131 if (argv[1])
132 bb_show_usage();
133 fname = argv[0];
134 }
122 135
123 fp = xfopen_stdin(argv[0] ? argv[0] : "-"); 136 fp = xfopen_stdin(fname);
124 lines = NULL; 137 lines = NULL;
125 numlines = 0; 138 numlines = 0;
126 for (;;) { 139 for (;;) {
@@ -150,9 +163,14 @@ int shuf_main(int argc, char **argv)
150 eol = '\0'; 163 eol = '\0';
151 164
152 for (i = numlines - outlines; i < numlines; i++) { 165 for (i = numlines - outlines; i < numlines; i++) {
153 if (opts & OPT_i) 166 if (opts & OPT_i) {
154 printf("%u%c", (unsigned)(uintptr_t)lines[i], eol); 167 if (sizeof(lines[0]) == sizeof(int))
155 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
156 printf("%s%c", lines[i], eol); 174 printf("%s%c", lines[i], eol);
157 } 175 }
158 176