Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions obstack.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@

#include <stddef.h> /* For size_t and ptrdiff_t. */
#include <string.h> /* For __GNU_LIBRARY__, and memcpy. */
#include <stdarg.h> /* For va_list. */

#if _OBSTACK_INTERFACE_VERSION == 1
/* For binary compatibility with obstack version 1, which used "int"
Expand Down Expand Up @@ -213,6 +214,7 @@ extern _OBSTACK_SIZE_T _obstack_memory_used (struct obstack *)

/* Declare obstack_printf; it's in obstack_printf.c. */
extern int obstack_printf(struct obstack *obstack, const char *__restrict fmt, ...);
extern int obstack_vprintf(struct obstack *obstack, const char *__restrict fmt, va_list args);


/* Error handler called when 'obstack_chunk_alloc' failed to allocate
Expand Down
29 changes: 26 additions & 3 deletions obstack_printf.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
#include <stdarg.h>
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include "obstack.h"

int obstack_printf(struct obstack *obstack, const char *__restrict fmt, ...)
{
char buf[1024];
va_list ap;
int len;

va_start(ap, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, ap);
obstack_grow(obstack, buf, len);
len = obstack_vprintf(obstack, fmt, ap);
va_end(ap);

return len;
}


int obstack_vprintf (struct obstack *obstack, const char *__restrict fmt, va_list args)
{
char buf[1024];
size_t len;
char * str = buf;

len = vsnprintf (buf, sizeof(buf), fmt, args);
if (len >= sizeof(buf)) {
str = NULL;
len = vasprintf(&str, fmt, args);
if (len < 0) {
free(str);
return -1;
}
}
obstack_grow (obstack, str, len);

if (str != buf)
free(str);

return len;
}