diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2024-12-16 00:32:57 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2024-12-16 00:32:57 +0100 |
commit | 0bd84c94720b5a68d97c05975220e6482c455623 (patch) | |
tree | cd41bf6152ed95ae1c17935d781211a9a38fbb4e | |
parent | ba9651b803f0eb2a8cba0205ae72c75ab773adaf (diff) | |
download | busybox-w32-0bd84c94720b5a68d97c05975220e6482c455623.tar.gz busybox-w32-0bd84c94720b5a68d97c05975220e6482c455623.tar.bz2 busybox-w32-0bd84c94720b5a68d97c05975220e6482c455623.zip |
cut: terminate cut_list[] so that we don't need "size of the array" variable
function old new delta
cut_main 1410 1404 -6
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | coreutils/cut.c | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c index 3d9f2b373..0913c4b68 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c | |||
@@ -84,19 +84,22 @@ | |||
84 | 84 | ||
85 | #define opt_REGEX (option_mask32 & OPT_REGEX) | 85 | #define opt_REGEX (option_mask32 & OPT_REGEX) |
86 | 86 | ||
87 | struct cut_list { | 87 | struct cut_range { |
88 | unsigned startpos; | 88 | unsigned startpos; |
89 | unsigned endpos; | 89 | unsigned endpos; |
90 | }; | 90 | }; |
91 | 91 | ||
92 | static int cmpfunc(const void *a, const void *b) | 92 | static int cmpfunc(const void *a, const void *b) |
93 | { | 93 | { |
94 | return (((struct cut_list *) a)->startpos - | 94 | return (((struct cut_range *) a)->startpos - |
95 | ((struct cut_list *) b)->startpos); | 95 | ((struct cut_range *) b)->startpos); |
96 | } | 96 | } |
97 | 97 | ||
98 | #define END_OF_LIST(list_elem) ((list_elem).startpos == UINT_MAX) | ||
99 | #define NOT_END_OF_LIST(list_elem) ((list_elem).startpos != UINT_MAX) | ||
100 | |||
98 | static void cut_file(FILE *file, const char *delim, const char *odelim, | 101 | static void cut_file(FILE *file, const char *delim, const char *odelim, |
99 | const struct cut_list *cut_list, unsigned nlists) | 102 | const struct cut_range *cut_list) |
100 | { | 103 | { |
101 | char *line; | 104 | char *line; |
102 | unsigned linenum = 0; /* keep these zero-based to be consistent */ | 105 | unsigned linenum = 0; /* keep these zero-based to be consistent */ |
@@ -115,7 +118,7 @@ static void cut_file(FILE *file, const char *delim, const char *odelim, | |||
115 | int need_odelim = 0; | 118 | int need_odelim = 0; |
116 | 119 | ||
117 | /* print the chars specified in each cut list */ | 120 | /* print the chars specified in each cut list */ |
118 | for (; cl_pos < nlists; cl_pos++) { | 121 | for (; NOT_END_OF_LIST(cut_list[cl_pos]); cl_pos++) { |
119 | unsigned spos; | 122 | unsigned spos; |
120 | for (spos = cut_list[cl_pos].startpos; spos < linelen;) { | 123 | for (spos = cut_list[cl_pos].startpos; spos < linelen;) { |
121 | if (!printed[spos]) { | 124 | if (!printed[spos]) { |
@@ -137,9 +140,9 @@ static void cut_file(FILE *file, const char *delim, const char *odelim, | |||
137 | } else if (!opt_REGEX && *delim == '\n') { | 140 | } else if (!opt_REGEX && *delim == '\n') { |
138 | unsigned spos = cut_list[cl_pos].startpos; | 141 | unsigned spos = cut_list[cl_pos].startpos; |
139 | 142 | ||
140 | /* get out if we have no more lists to process or if the lines | 143 | /* get out if we have no more ranges to process or if the lines |
141 | * are lower than what we're interested in */ | 144 | * are lower than what we're interested in */ |
142 | if ((linenum < spos) || (cl_pos >= nlists)) | 145 | if ((linenum < spos) || END_OF_LIST(cut_list[cl_pos])) |
143 | goto next_line; | 146 | goto next_line; |
144 | 147 | ||
145 | /* if the line we're looking for is lower than the one we were | 148 | /* if the line we're looking for is lower than the one we were |
@@ -149,8 +152,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim, | |||
149 | /* go to the next list if we're at the end of this one */ | 152 | /* go to the next list if we're at the end of this one */ |
150 | if (spos > cut_list[cl_pos].endpos) { | 153 | if (spos > cut_list[cl_pos].endpos) { |
151 | cl_pos++; | 154 | cl_pos++; |
152 | /* get out if there's no more lists to process */ | 155 | /* get out if there's no more ranges to process */ |
153 | if (cl_pos >= nlists) | 156 | if (END_OF_LIST(cut_list[cl_pos])) |
154 | goto next_line; | 157 | goto next_line; |
155 | spos = cut_list[cl_pos].startpos; | 158 | spos = cut_list[cl_pos].startpos; |
156 | /* get out if the current line is lower than the one | 159 | /* get out if the current line is lower than the one |
@@ -188,7 +191,8 @@ static void cut_file(FILE *file, const char *delim, const char *odelim, | |||
188 | /* End of current range? */ | 191 | /* End of current range? */ |
189 | if (end == linelen || dcount > cut_list[cl_pos].endpos) { | 192 | if (end == linelen || dcount > cut_list[cl_pos].endpos) { |
190 | end_of_range: | 193 | end_of_range: |
191 | if (++cl_pos >= nlists) | 194 | cl_pos++; |
195 | if (END_OF_LIST(cut_list[cl_pos])) | ||
192 | break; | 196 | break; |
193 | if (option_mask32 & OPT_NOSORT) | 197 | if (option_mask32 & OPT_NOSORT) |
194 | start = dcount = next = 0; | 198 | start = dcount = next = 0; |
@@ -284,9 +288,9 @@ static void cut_file(FILE *file, const char *delim, const char *odelim, | |||
284 | int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 288 | int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
285 | int cut_main(int argc UNUSED_PARAM, char **argv) | 289 | int cut_main(int argc UNUSED_PARAM, char **argv) |
286 | { | 290 | { |
287 | /* growable array holding a series of lists */ | 291 | /* growable array holding a series of ranges */ |
288 | struct cut_list *cut_list = NULL; | 292 | struct cut_range *cut_list = NULL; |
289 | unsigned nlists = 0; /* number of elements in above list */ | 293 | unsigned nranges = 0; /* number of elements in above list */ |
290 | char *LIST, *ltok; | 294 | char *LIST, *ltok; |
291 | const char *delim = NULL; | 295 | const char *delim = NULL; |
292 | const char *odelim = NULL; | 296 | const char *odelim = NULL; |
@@ -383,15 +387,16 @@ int cut_main(int argc UNUSED_PARAM, char **argv) | |||
383 | bb_error_msg_and_die("invalid range %s-%s", ntok, ltok ?: ntok); | 387 | bb_error_msg_and_die("invalid range %s-%s", ntok, ltok ?: ntok); |
384 | 388 | ||
385 | /* add the new range */ | 389 | /* add the new range */ |
386 | cut_list = xrealloc_vector(cut_list, 4, nlists); | 390 | cut_list = xrealloc_vector(cut_list, 4, nranges); |
387 | /* NB: s is always >= 0 */ | 391 | /* NB: s is always >= 0 */ |
388 | cut_list[nlists].startpos = s; | 392 | cut_list[nranges].startpos = s; |
389 | cut_list[nlists].endpos = e; | 393 | cut_list[nranges].endpos = e; |
390 | nlists++; | 394 | nranges++; |
391 | } | 395 | } |
396 | cut_list[nranges].startpos = UINT_MAX; /* end indicator */ | ||
392 | 397 | ||
393 | /* make sure we got some cut positions out of all that */ | 398 | /* make sure we got some cut positions out of all that */ |
394 | //if (nlists == 0) | 399 | //if (nranges == 0) |
395 | // bb_simple_error_msg_and_die("missing list of positions"); | 400 | // bb_simple_error_msg_and_die("missing list of positions"); |
396 | //^^^ this is impossible since one of -bcfF is required, | 401 | //^^^ this is impossible since one of -bcfF is required, |
397 | // they populate LIST with non-empty string and when it is parsed, | 402 | // they populate LIST with non-empty string and when it is parsed, |
@@ -401,7 +406,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv) | |||
401 | * easier on us when it comes time to print the chars / fields / lines | 406 | * easier on us when it comes time to print the chars / fields / lines |
402 | */ | 407 | */ |
403 | if (!(opt & OPT_NOSORT)) | 408 | if (!(opt & OPT_NOSORT)) |
404 | qsort(cut_list, nlists, sizeof(cut_list[0]), cmpfunc); | 409 | qsort(cut_list, nranges, sizeof(cut_list[0]), cmpfunc); |
405 | 410 | ||
406 | #if ENABLE_FEATURE_CUT_REGEX | 411 | #if ENABLE_FEATURE_CUT_REGEX |
407 | if (opt & OPT_REGEX) { | 412 | if (opt & OPT_REGEX) { |
@@ -422,7 +427,7 @@ int cut_main(int argc UNUSED_PARAM, char **argv) | |||
422 | retval = EXIT_FAILURE; | 427 | retval = EXIT_FAILURE; |
423 | continue; | 428 | continue; |
424 | } | 429 | } |
425 | cut_file(file, delim, odelim, cut_list, nlists); | 430 | cut_file(file, delim, odelim, cut_list); |
426 | fclose_if_not_stdin(file); | 431 | fclose_if_not_stdin(file); |
427 | } while (*++argv); | 432 | } while (*++argv); |
428 | 433 | ||