From 7178674807632f117c58eda232f961578a13cee4 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Fri, 22 Mar 1996 04:54:46 +0000 Subject: Include assert.h, but disable assertions. (process_line_count): Replace if-abort with a slightly relaxed assertion. Before, `echo |csplit - 1 1' would abort. Reported by Samuli.Karkkainen@hut.fi. (parse_patterns): Disallow uses like `csplit FILE 0' with zero line number, `csplit FILE 2 1' with decreasing line numbers, and warn about uses like `csplit FILE 3 3' that have equal line numbers. --- src/csplit.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/csplit.c b/src/csplit.c index 8ff468b6f..874433a69 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -21,6 +21,11 @@ #include #include + +/* Disable assertions. Some systems have broken assert macros. */ +#define NDEBUG 1 + +#include #include #include #include @@ -825,10 +830,11 @@ process_line_count (const struct control *p, int repetition) linenum = get_first_line_in_buffer (); - /* Check for requesting a line that has already been written out. - If this ever happens, it's due to a bug in csplit. */ - if (linenum >= last_line_to_save) - abort (); + /* Initially, I wanted to assert linenum < last_line_to_save, but that + condition is false for the valid command: echo | csplit - 1 '{*}'. + So, relax it just a little. */ + assert ((linenum == 1 && last_line_to_save == 1) + || linenum < last_line_to_save); while (linenum++ < last_line_to_save) { @@ -1218,6 +1224,7 @@ parse_patterns (int argc, int start, char **argv) int i; /* Index into ARGV. */ struct control *p; /* New control record created. */ unsigned long val; + static unsigned long last_val = 0; for (i = start; i < argc; i++) { @@ -1229,9 +1236,24 @@ parse_patterns (int argc, int start, char **argv) { p = new_control_record (); p->argnum = i; + if (xstrtoul (argv[i], NULL, 10, &val, NULL) != LONGINT_OK || val > INT_MAX) error (1, 0, _("%s: invalid pattern"), argv[i]); + if (val == 0) + error (1, 0, _("%s: line number must be greater than zero"), + argv[i]); + if (val < last_val) + error (1, 0, + _("line number `%s' is smaller than preceding line number, %lu"), + argv[i], last_val); + + if (val == last_val) + error (0, 0, + _("warning: line number `%s' is the same as preceding line number"), + argv[i]); + last_val = val; + p->lines_required = (int) val; } -- cgit v1.2.3-54-g00ecf