diff options
author | Steven Schubiger <schubiger@gmail.com> | 2008-02-18 22:39:22 +0100 |
---|---|---|
committer | Jim Meyering <meyering@redhat.com> | 2008-02-19 15:41:16 +0100 |
commit | 4f3ff2e7007a6591294e1a656782b5f239f9840c (patch) | |
tree | 7ed8264c7566377c6730cddaaf3b47567e73151b /src | |
parent | a1e715698a038af7ff341011a2aeecf6729c8de9 (diff) | |
download | coreutils-4f3ff2e7007a6591294e1a656782b5f239f9840c.tar.xz |
seq: give better diagnostics for invalid formats.
* src/seq.c: (validate_format): New function.
(main): Use it.
* tests/misc/seq (fmt-d, fmt-e): Test for expected diagnostics with
invalid formats.
* NEWS: Mention this change.
* TODO: Remove this item.
[jm: src/seq.c: make diagnostics more consistent
tests/misc/seq (fmt-eos1): adjust the expected diagnostic ]
Diffstat (limited to 'src')
-rw-r--r-- | src/seq.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -177,6 +177,33 @@ scan_arg (const char *arg) return ret; } +/* Validate the format, FMT. Print a diagnostic and exit + if there is not exactly one %-directive. */ + +static void +validate_format (char const *fmt) +{ + unsigned int n_directives = 0; + char const *p; + + for (p = fmt; *p; p++) + { + if (p[0] == '%' && p[1] != '%' && p[1] != '\0') + { + ++n_directives; + ++p; + } + } + if (! n_directives) + { + error (0, 0, _("no %% directive in format string %s"), quote (fmt)); + usage (EXIT_FAILURE); + } + else if (1 < n_directives) + error (EXIT_FAILURE, 0, _("too many %% directives in format string %s"), + quote (fmt)); +} + /* If FORMAT is a valid printf format for a double argument, return its long double equivalent, possibly allocated from dynamic storage, and store into *LAYOUT a description of the output layout; @@ -405,6 +432,7 @@ main (int argc, char **argv) if (format_str) { + validate_format (format_str); char const *f = long_double_format (format_str, &layout); if (! f) { |