forked from void-linux/musl-obstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
28 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |