diff options
author | Jim Meyering <jim@meyering.net> | 1996-03-22 04:54:46 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1996-03-22 04:54:46 +0000 |
commit | 7178674807632f117c58eda232f961578a13cee4 (patch) | |
tree | 06d09f9b80cd4630d4fe873f3d1e6b262113ba33 | |
parent | 1954f681c6ca11ff91ab59ec074a03859ab0dc46 (diff) | |
download | coreutils-7178674807632f117c58eda232f961578a13cee4.tar.xz |
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.
-rw-r--r-- | src/csplit.c | 30 |
1 files changed, 26 insertions, 4 deletions
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 <config.h> #include <stdio.h> + +/* Disable assertions. Some systems have broken assert macros. */ +#define NDEBUG 1 + +#include <assert.h> #include <getopt.h> #include <sys/types.h> #include <signal.h> @@ -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; } |