mescc: Support gcc-3.4.0: *printf: Support `%n'.
authorJan Nieuwenhuizen <janneke@gnu.org>
Wed, 6 Jun 2018 17:29:32 +0000 (19:29 +0200)
committerJan Nieuwenhuizen <janneke@gnu.org>
Wed, 6 Jun 2018 17:29:32 +0000 (19:29 +0200)
* lib/libc+tcc.c (vfprintf, vsprintf): Support `%n'.  For Gcc 3.4.0.
* scaffold/tests/70-printf.c: Test it.

lib/libc+tcc.c
scaffold/tests/70-printf.c

index 56892b5d2613af1c1bd816e38c41cfd82ef6afa0..6b66eabba1a634de47e79124d7bf4db6e272f30a 100644 (file)
@@ -576,9 +576,13 @@ vfprintf (FILE* f, char const* format, va_list ap)
 {
   int fd = (int)f;
   char const *p = format;
+  int count = 0;
   while (*p)
     if (*p != '%')
-      fputc (*p++, fd);
+      {
+        count++;
+        fputc (*p++, fd);
+      }
     else
       {
         p++;
@@ -618,7 +622,7 @@ vfprintf (FILE* f, char const* format, va_list ap)
           c = *++p;
         switch (c)
           {
-          case '%': {fputc (*p, fd); break;}
+          case '%': {fputc (*p, fd); count++; break;}
           case 'c': {char c; c = va_arg (ap, int); fputc (c, fd); break;}
           case 'd':
           case 'i':
@@ -638,15 +642,22 @@ vfprintf (FILE* f, char const* format, va_list ap)
                 width = width - strlen (s);
               if (!left_p && !precision_p)
                 while (!precision_p && width-- > 0)
-                  fputc (pad, f);
+                  {
+                    fputc (pad, f);
+                    count++;
+                  }
               while (*s)
                 {
                   if (precision_p && width-- == 0)
                     break;
                   fputc (*s++, f);
+                  count++;
                 }
               while (!precision_p && width-- > 0)
-                fputc (pad, f);
+                {
+                  fputc (pad, f);
+                  count++;
+                }
               break;
             }
           case 's':
@@ -656,15 +667,28 @@ vfprintf (FILE* f, char const* format, va_list ap)
                 width = width - strlen (s);
               if (!left_p && !precision_p)
                 while (!precision_p && width-- > 0)
-                  fputc (pad, f);
+                  {
+                    fputc (pad, f);
+                    count++;
+                  }
               while (*s)
                 {
                   if (precision_p && width-- == 0)
                     break;
                   fputc (*s++, f);
+                  count++;
                 }
               while (!precision_p && width-- > 0)
-                fputc (pad, f);
+                {
+                  fputc (pad, f);
+                  count++;
+                }
+              break;
+            }
+          case 'n':
+            {
+              int *n = va_arg (ap, int *);
+              *n = count;
               break;
             }
           default:
@@ -741,9 +765,13 @@ int
 vsprintf (char *str, char const* format, va_list ap)
 {
   char const *p = format;
+  int count = 0;
   while (*p)
     if (*p != '%')
-      *str++ = *p++;
+      {
+        *str++ = *p++;
+        count++;
+      }
     else
       {
         p++;
@@ -783,8 +811,8 @@ vsprintf (char *str, char const* format, va_list ap)
           c = *++p;
         switch (c)
           {
-          case '%': {*str++ = *p; break;}
-          case 'c': {c = va_arg (ap, int); *str++ = c; break;}
+          case '%': {*str++ = *p; count++; break;}
+          case 'c': {c = va_arg (ap, int); *str++ = c; count++; break;}
           case 'd':
           case 'i':
           case 'o':
@@ -803,15 +831,22 @@ vsprintf (char *str, char const* format, va_list ap)
                 width = width - strlen (s);
               if (!left_p && !precision_p)
                 while (!precision_p && width-- > 0)
-                  *str++ = pad;
+                  {
+                    *str++ = pad;
+                    count++;
+                  }
               while (*s)
                 {
                   if (precision_p && width-- == 0)
                     break;
                   *str++ = *s++;
+                  count++;
                 }
               while (!precision_p && width-- > 0)
-                *str++ = pad;
+                {
+                  *str++ = pad;
+                  count++;
+                }
               break;
             }
           case 's':
@@ -821,15 +856,28 @@ vsprintf (char *str, char const* format, va_list ap)
                 width = width - strlen (s);
               if (!left_p && !precision_p)
                 while (!precision_p && width-- > 0)
-                  *str++ = pad;
+                  {
+                    *str++ = pad;
+                    count++;
+                  }
               while (*s)
                 {
                   if (precision_p && width-- == 0)
                     break;
                   *str++ = *s++;
+                  count++;
                 }
               while (!precision_p && width-- > 0)
-                *str++ = pad;
+                {
+                  *str++ = pad;
+                  count++;
+                }
+              break;
+            }
+          case 'n':
+            {
+              int *n = va_arg (ap, int *);
+              *n = count;
               break;
             }
           default:
index d62e3825da2207a74566ec1757a756deb78fcaf1..653e212842238c38054fa4528558d9d13a3f78ff 100644 (file)
@@ -134,5 +134,17 @@ test ()
   if (strcmp (buf, ">>C<<\n"))
     return 19;
 
+  int n;
+  fprintf (stderr, "foo bar\n%n", &n);
+  if (n != 8)
+    return 20;
+
+  sprintf (buf, "foo%nbar\n", &n);
+  eputs ("buf="); eputs (buf); eputs ("\n");
+  if (strcmp (buf, "foobar\n"))
+    return 21;
+  if (n != 3)
+    return 22;
+
   return 0;
 }