return NULL;
else string++;
}
-
+\f
static void
vprintf_core (const char *format, va_list args,
void (*output) (char, void *), void *aux);
static void
-output_console (char ch, void *aux __attribute__ ((unused)))
+vprintk_helper (char ch, void *aux __attribute__ ((unused)))
{
vga_putc (ch);
serial_outb (ch);
vprintk (const char *format, va_list args)
{
enum if_level old_level = intr_disable ();
- vprintf_core (format, args, output_console, NULL);
+ vprintf_core (format, args, vprintk_helper, NULL);
intr_set_level (old_level);
}
va_end (args);
}
\f
+struct vsnprintf_aux
+ {
+ char *p;
+ int length;
+ int max_length;
+ };
+
+static void
+vsnprintf_helper (char ch, void *aux_)
+{
+ struct vsnprintf_aux *aux = aux_;
+
+ if (aux->length++ < aux->max_length)
+ *aux->p++ = ch;
+}
+
+int
+vsnprintf (char *buffer, size_t buf_size,
+ const char *format, va_list args)
+{
+ struct vsnprintf_aux aux;
+ aux.p = buffer;
+ aux.length = 0;
+ aux.max_length = buf_size > 0 ? buf_size - 1 : 0;
+
+ vprintf_core (format, args, vsnprintf_helper, &aux);
+
+ if (buf_size > 0)
+ *aux.p = '\0';
+
+ return aux.length;
+}
+
+int
+snprintf (char *buffer, size_t buf_size,
+ const char *format, ...)
+{
+ va_list args;
+
+ va_start (args, format);
+ vsnprintf (buffer, buf_size, format, args);
+ va_end (args);
+}
+\f
/* printf() and friends internals. You do not need to understand
this code. */
};
static const char *
-parse_conversion (const char *format, struct printf_conversion *c, va_list *args)
+parse_conversion (const char *format, struct printf_conversion *c,
+ va_list *args)
{
/* Parse flag characters. */
c->flags = 0;
#include <stdarg.h>
#include <stddef.h>
+#include "debug.h"
#define ROUND_UP(X, STEP) (((X) + (STEP) - 1) / (STEP) * (STEP))
#define DIV_ROUND_UP(X, STEP) (((X) + (STEP) - 1) / (STEP))
size_t strlen (const char *);
void vprintk (const char *, va_list);
-void printk (const char *, ...)
- __attribute__ ((format (printf, 1, 2)));
+void printk (const char *, ...) PRINTF_FORMAT (1, 2);
+int vsnprintf (char *, size_t, const char *, va_list);
+int snprintf (char *, size_t, const char *, ...) PRINTF_FORMAT (3, 4);
static inline int
isdigit (int c)