break;
default:
fprintf(stderr,
- "FATAL dstr_vputf: unknown format specifier `%c'\n", p[-1]);
+ "FATAL vgprintf: unknown format specifier `%c'\n", p[-1]);
abort();
}
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;
}
#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':
return (n);
}
+/*----- Utilities ---------------------------------------------------------*/
+
+/* --- @gprintf_memputf@ --- *
+ *
+ * Arguments: @char **buf_inout@ = address of output buffer pointer
+ * @size_t *sz_inout@ = address of buffer size
+ * @size_t maxsz@ = buffer size needed for this operation
+ * @const char *p@ = pointer to format string
+ * @va_list *ap@ = captured format-arguments tail
+ *
+ * Returns: The formatted length.
+ *
+ * Use: Generic utility for mostly implementing the @nputf@ output
+ * function, if you don't have a better option.
+ *
+ * On entry, @*buf_inout@ should be null or a buffer pointer,
+ * with @*sz_inout@ either zero or the buffer's size,
+ * respectively. On exit, @*buf_input@ and @*sz_inout@ will be
+ * updated, if necessary, to describe a sufficiently large
+ * buffer, and the formatted string will have been written to
+ * the buffer.
+ *
+ * When the buffer is no longer required, free it using @xfree@.
+ */
+
+size_t gprintf_memputf(char **buf_inout, size_t *sz_inout,
+ size_t maxsz, const char *p, va_list ap)
+{
+ char *buf = *buf_inout;
+ size_t sz = *sz_inout;
+ int n;
+
+ if (sz <= maxsz) {
+ if (!sz) sz = 32;
+ while (sz <= maxsz) sz *= 2;
+ if (buf) xfree(buf);
+ buf = xmalloc(sz); *buf_inout = buf; *sz_inout = sz;
+ }
+
+#ifdef HAVE_SNPRINTF
+ n = vsnprintf(buf, maxsz + 1, p, ap);
+#else
+ n = vsprintf(buf, p, ap);
+#endif
+ assert(0 <= n && n <= maxsz);
+ return (n);
+}
+
/*----- Standard printers -------------------------------------------------*/
static int file_putch(void *out, int ch)