diff options
-rw-r--r-- | coreutils/cut.c | 19 | ||||
-rwxr-xr-x | testsuite/cut.tests | 5 |
2 files changed, 12 insertions, 12 deletions
diff --git a/coreutils/cut.c b/coreutils/cut.c index b7fe11126..f68bbbad5 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c | |||
@@ -278,29 +278,26 @@ int cut_main(int argc UNUSED_PARAM, char **argv) | |||
278 | if (!ntok[0]) { | 278 | if (!ntok[0]) { |
279 | s = 0; | 279 | s = 0; |
280 | } else { | 280 | } else { |
281 | s = xatoi_positive(ntok); | ||
282 | /* account for the fact that arrays are zero based, while | 281 | /* account for the fact that arrays are zero based, while |
283 | * the user expects the first char on the line to be char #1 */ | 282 | * the user expects the first char on the line to be char #1 */ |
284 | if (s != 0) | 283 | s = xatoi_positive(ntok) - 1; |
285 | s--; | ||
286 | } | 284 | } |
287 | 285 | ||
288 | /* get the end pos */ | 286 | /* get the end pos */ |
289 | if (ltok == NULL) { | 287 | if (ltok == NULL) { |
290 | e = s; | 288 | e = s; |
291 | } else if (!ltok[0]) { | 289 | } else if (!ltok[0]) { |
290 | /* if the user specified no end position, | ||
291 | * that means "til the end of the line" */ | ||
292 | e = INT_MAX; | 292 | e = INT_MAX; |
293 | } else { | 293 | } else { |
294 | e = xatoi_positive(ltok); | 294 | /* again, arrays are zero based, lines are 1 based */ |
295 | /* if the user specified and end position of 0, | 295 | e = xatoi_positive(ltok) - 1; |
296 | * that means "til the end of the line" */ | ||
297 | if (!*ltok) | ||
298 | e = INT_MAX; | ||
299 | else if (e < s) | ||
300 | bb_error_msg_and_die("%d<%d", e, s); | ||
301 | e--; /* again, arrays are zero based, lines are 1 based */ | ||
302 | } | 296 | } |
303 | 297 | ||
298 | if (s < 0 || e < s) | ||
299 | bb_error_msg_and_die("invalid range %s-%s", ntok, ltok ?: ntok); | ||
300 | |||
304 | /* add the new list */ | 301 | /* add the new list */ |
305 | cut_lists = xrealloc_vector(cut_lists, 4, nlists); | 302 | cut_lists = xrealloc_vector(cut_lists, 4, nlists); |
306 | /* NB: startpos is always >= 0 */ | 303 | /* NB: startpos is always >= 0 */ |
diff --git a/testsuite/cut.tests b/testsuite/cut.tests index 0b401bc00..c335f824b 100755 --- a/testsuite/cut.tests +++ b/testsuite/cut.tests | |||
@@ -31,7 +31,10 @@ testing "-b encapsulated" "cut -b 3-8,4-6 input" "e:two:\npha:be\ne quic\n" \ | |||
31 | #testing "cut -bO overlaps" \ | 31 | #testing "cut -bO overlaps" \ |
32 | # "cut --output-delimiter ' ' -b 1-3,2-5,7-9,9-10 input" \ | 32 | # "cut --output-delimiter ' ' -b 1-3,2-5,7-9,9-10 input" \ |
33 | # "one:t o:th\nalpha beta\nthe q ick \n" "$abc" "" | 33 | # "one:t o:th\nalpha beta\nthe q ick \n" "$abc" "" |
34 | testing "cut high-low error" "cut -b 8-3 abc.txt 2>/dev/null || echo err" "err\n" \ | 34 | testing "cut high-low error" "cut -b 8-3 input 2>/dev/null || echo err" "err\n" \ |
35 | "$abc" "" | ||
36 | |||
37 | testing "cut -b 2-1 error" "cut -b 2-1 input 2>/dev/null || echo err" "err\n" \ | ||
35 | "$abc" "" | 38 | "$abc" "" |
36 | 39 | ||
37 | testing "cut -c a-b" "cut -c 4-10 input" ":two:th\nha:beta\n quick \n" "$abc" "" | 40 | testing "cut -c a-b" "cut -c 4-10 input" ":two:th\nha:beta\n quick \n" "$abc" "" |