utils/gprintf.c: Return the correct number of output characters.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 22 Feb 2024 19:43:58 +0000 (19:43 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 22 Feb 2024 19:49:47 +0000 (19:49 +0000)
Also fix the test program to check that the output count is correct.

struct/t/dstr-putf-test.c
utils/gprintf.c

index 416b164..044da71 100644 (file)
@@ -18,13 +18,16 @@ static char strbuf[1024];
 
 #define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name)
 
-static void PRINTF_LIKE(1, 2) format(const char *fmt, ...)
+static int PRINTF_LIKE(1, 2) format(const char *fmt, ...)
 {
   va_list ap;
+  int n;
+
   va_start(ap, fmt);
   dstr_reset(&d);
-  dstr_vputf(&d, fmt, &ap);
+  n = dstr_vputf(&d, fmt, &ap);
   va_end(ap);
+  return (n);
 }
 
 static void PRINTF_LIKE(1, 2) prepare(const char *fmt, ...)
@@ -42,14 +45,18 @@ static void PRINTF_LIKE(1, 2) prepare(const char *fmt, ...)
 }
 
 #define TEST_KNOWN(fmtargs, want) do {                                 \
-  format fmtargs;                                                      \
+  int n; n = format fmtargs;                                           \
+  tvec_claimeq_int(&tvstate, n, strlen(want),                          \
+                  __FILE__, __LINE__, "format " #fmtargs);             \
   tvec_claimeq_string(&tvstate, d.buf, d.len, want, strlen(want),      \
                      __FILE__, __LINE__, "format " #fmtargs);          \
 } while (0)
 
 #define TEST_REF(fmtargs) do {                                         \
-  format fmtargs;                                                      \
+  int n = format fmtargs;                                              \
   prepare fmtargs;                                                     \
+  tvec_claimeq_int(&tvstate, n, strlen(strbuf),                                \
+                  __FILE__, __LINE__, "format " #fmtargs);             \
   tvec_claimeq_string(&tvstate, d.buf, d.len, strbuf, strlen(strbuf),  \
                      __FILE__, __LINE__, "format " #fmtargs);          \
 } while (0)
index b575e1f..8825bd1 100644 (file)
@@ -416,9 +416,13 @@ int vgprintf(const struct gprintf_ops *ops, void *out,
 
     if (fs->fmt == fmt_unset) {
       switch (fs->ch) {
-       case 0: break;
-       case '%': ops->putch(out, '%'); break;
-       default: abort();
+       case 0:
+         break;
+       case '%':
+         if (ops->putch(out, '%')) return (-1);
+         tot++; break;
+       default:
+         abort();
       }
       continue;
     }
@@ -497,7 +501,7 @@ int vgprintf(const struct gprintf_ops *ops, void *out,
 #else
 #  define MSG "<no float support>"
        if (ops->putm(out, MSG, sizeof(MSG) - 1)) return (-1);
-       continue;
+       tot += sizeof(MSG) - 1; continue;
 #  undef MSG
 #endif
       case 's':