bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

FYI: more pr fixes


From: Jim Meyering
Subject: FYI: more pr fixes
Date: Tue, 15 Mar 2005 20:28:52 +0100

I've just made this change:

        Both `pr -0' and e.g., `pr -03' would evoke `column count too large'.
        `pr -0' should give a better diagnostic and `pr -03' should be
        equivalent to `pr -3'.
        * src/pr.c (parse_column_count): Change return type to void.
        Call error (EXIT_FAILURE, ... for an invalid string.
        (main): Allocate space for column_count_string using malloc.
        Accumulate all old-style column-count digits before converting.
        When the number of columns is specified via both old-style,
        (e.g., -3), and a long option (--columns=5), ensure that only
        the last one specified takes effect.
        * tests/pr/Test.pm: Add tests for the above.

Index: src/pr.c
===================================================================
RCS file: /fetish/cu/src/pr.c,v
retrieving revision 1.129
retrieving revision 1.130
diff -u -p -u -r1.129 -r1.130
--- src/pr.c    10 Mar 2005 08:51:49 -0000      1.129
+++ src/pr.c    15 Mar 2005 18:05:13 -0000      1.130
@@ -837,17 +837,17 @@ first_last_page (char const *pages)
    within range of the type of `columns') set the global variables
    columns and explicit_columns and return true.
    Otherwise, return false.  */
-static bool
+static void
 parse_column_count (char const *s)
 {
   long int tmp_long;
   if (xstrtol (s, NULL, 10, &tmp_long, "") != LONGINT_OK
       || !(1 <= tmp_long && tmp_long <= INT_MAX))
-    return false;
+    error (EXIT_FAILURE, 0,
+          _("invalid number of columns: `%s'"), s);
 
   columns = tmp_long;
   explicit_columns = true;
-  return true;
 }
 
 /* Estimate length of col_sep_string with option -S.  */
@@ -870,15 +870,10 @@ main (int argc, char **argv)
   bool old_s = false;
   char **file_names;
 
-  /* Use this buffer to accumulate the digits of old-style options like -99.
-     Make it one byte larger than the size required for the largest value;
-     if the user-supplied string would overflow, we'll discover that fact
-     (and fail) when accumulating the first additional byte.
-     FIXME: we're using INT_BUFSIZE_BOUND (uintmax_t) here already, in
-     anticipation of the clean-up that changes the type of `columns'
-     from int to size_t.  */
-  char column_count_string[1 + INT_BUFSIZE_BOUND (uintmax_t)];
+  /* Accumulate the digits of old-style options like -99.  */
+  char *column_count_string = NULL;
   size_t n_digits = 0;
+  size_t n_alloc = 0;
 
   char const *short_options = (posix2_version () < 200112
                               ? COMMON_SHORT_OPTIONS "S::"
@@ -902,10 +897,13 @@ main (int argc, char **argv)
     {
       if (ISDIGIT (c))
        {
+         /* Accumulate column-count digits specified via old-style options. */
+         if (n_digits == n_alloc)
+           column_count_string
+             = x2nrealloc (column_count_string, &n_alloc,
+                           sizeof *column_count_string);
          column_count_string[n_digits++] = c;
          column_count_string[n_digits] = 0;
-         if ( ! parse_column_count (column_count_string))
-           error (EXIT_FAILURE, 0, _("column count too large"));
          continue;
        }
 
@@ -932,9 +930,14 @@ main (int argc, char **argv)
 
        case COLUMNS_OPTION:    /* --columns=COLUMN */
          {
-           if ( ! parse_column_count (optarg))
-             error (EXIT_FAILURE, 0,
-                    _("invalid number of columns: `%s'"), optarg);
+           parse_column_count (optarg);
+
+           /* If there was a prior column count specified via the
+              short-named option syntax, e.g., -9, ensure that this
+              long-name-specified value overrides it.  */
+           free (column_count_string);
+           column_count_string = NULL;
+           n_alloc = 0;
            break;
          }
 
@@ -1087,6 +1090,9 @@ main (int argc, char **argv)
        }
     }
 
+  if (column_count_string)
+    parse_column_count (column_count_string);
+
   if (! date_format)
     date_format = (getenv ("POSIXLY_CORRECT") && !hard_locale (LC_TIME)
                   ? "%b %e %H:%M %Y"




reply via email to

[Prev in Thread] Current Thread [Next in Thread]