bug-findutils
[Top][All Lists]
Advanced

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

[PATCH 2/7] Simplify -fprintf %%; handle %% like a regular format specif


From: James Youngman
Subject: [PATCH 2/7] Simplify -fprintf %%; handle %% like a regular format specifier.
Date: Wed, 29 Jun 2011 10:16:51 +0100

* find/print.c (make_segment): Handle foo%% by simply generating a
format string of foo%% (that is, let vfprintf handle the escaped
%).
(insert_fprintf): Handle %% as KIND_FORMAT instead of KIND_PLAIN.
(do_fprintf): Handle %%.
---
 ChangeLog    |    7 +++++++
 find/print.c |   50 ++++++++++++++++++++++++++++----------------------
 2 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index fc6f945..6cdc8b3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2011-06-25  James Youngman  <address@hidden>
 
+       Simplify -fprintf %%; handle %% like a regular format specifier.
+       * find/print.c (make_segment): Handle foo%% by simply generating a
+       format string of foo%% (that is, let vfprintf handle the escaped
+       %).
+       (insert_fprintf): Handle %% as KIND_FORMAT instead of KIND_PLAIN.
+       (do_fprintf): Handle %%.
+
        Clarify variable naming in insert_fprintf.
        * find/print.c (insert_fprintf): Rename some variables for greater
        clarity:
diff --git a/find/print.c b/find/print.c
index e0a1331..31a46cd 100644
--- a/find/print.c
+++ b/find/print.c
@@ -116,6 +116,10 @@ make_segment (struct segment **segment,
   assert (kind == KIND_FORMAT);
   switch (format_char)
     {
+    case '%':                  /* literal % */
+      *fmt++ = '%';
+      break;
+
     case 'l':                  /* object of symlink */
       pred->need_stat = true;
       mycost = NeedsLinkName;
@@ -297,36 +301,34 @@ insert_fprintf (struct format_val *vec,
          segstart = fmt_inpos + 1; /* Move past the escape. */
          fmt_editpos = fmt_inpos;  /* Incremented immediately by `for'. */
        }
-      else if (*fmt_editpos == '%')
+      else if (fmt_editpos[0] == '%')
        {
-         if (fmt_editpos[1] == 0)
+         if (fmt_editpos[1] == '%')
+           {
+             /* % escapes itself.  That is, %% produces just %. */
+             fmt_inpos = fmt_editpos+1;
+           }
+         else if (fmt_editpos[1] == 0)
            {
              /* Trailing %.  We don't like those. */
              error (EXIT_FAILURE, 0,
                     _("error: %s at end of format string"), fmt_editpos);
            }
-         else if (fmt_editpos[1] == '%')
-           {
-             segmentp = make_segment (segmentp,
-                                      segstart, fmt_editpos - segstart + 1,
-                                      KIND_PLAIN, 0, 0,
-                                      our_pred);
-             fmt_editpos++;
-             segstart = fmt_editpos + 1;
-             continue;
-           }
-         /* Scan past flags, width and precision, to verify kind. */
-         for (fmt_inpos = fmt_editpos;
-              *++fmt_inpos && strchr ("-+ #", *fmt_inpos);)
+         else
            {
-             /* Do nothing. */
+             /* Scan past flags, width and precision, to verify kind. */
+             for (fmt_inpos = fmt_editpos;
+                  *++fmt_inpos && strchr ("-+ #", *fmt_inpos);)
+               {
+                 /* Do nothing. */
+               }
+             while (ISDIGIT (*fmt_inpos))
+               fmt_inpos++;
+             if (*fmt_inpos == '.')
+               for (fmt_inpos++; ISDIGIT (*fmt_inpos); fmt_inpos++)
+                 /* Do nothing. */ ;
            }
-         while (ISDIGIT (*fmt_inpos))
-           fmt_inpos++;
-         if (*fmt_inpos == '.')
-           for (fmt_inpos++; ISDIGIT (*fmt_inpos); fmt_inpos++)
-             /* Do nothing. */ ;
-         if (strchr ("abcdDfFgGhHiklmMnpPsStuUyYZ", *fmt_inpos))
+         if (strchr ("abcdDfFgGhHiklmMnpPsStuUyYZ%", *fmt_inpos))
            {
              segmentp = make_segment (segmentp, segstart, fmt_inpos - segstart,
                                       KIND_FORMAT, *fmt_inpos, 0,
@@ -1174,6 +1176,10 @@ do_fprintf (struct format_val *dest,
              }
          }
          break;
+
+       case '%':
+         checked_fprintf (dest, segment->text);
+         break;
        }
       /* end of KIND_FORMAT case */
       break;
-- 
1.7.2.5




reply via email to

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