diff options
Diffstat (limited to 'hw4/l4basic')
-rw-r--r-- | hw4/l4basic/l4str.c | 22 | ||||
-rw-r--r-- | hw4/l4basic/l4str.h | 2 |
2 files changed, 18 insertions, 6 deletions
diff --git a/hw4/l4basic/l4str.c b/hw4/l4basic/l4str.c index ad8c2e6..c1f6c51 100644 --- a/hw4/l4basic/l4str.c +++ b/hw4/l4basic/l4str.c @@ -25,18 +25,28 @@ bool lbs_str_has_suffix (const char* str, const char* suffix) { char* lbs_str_printf (const char* format, ...) { va_list ap; char* newstr; - int len; va_start (ap, format); - len = vsnprintf (NULL, 0, format, ap) + 1; + newstr = lbs_str_vprintf (format, ap); va_end (ap); + return newstr; +} + +char* lbs_str_vprintf (const char* format, va_list ap) { + va_list ap1, ap2; + char* newstr; + int len; + + va_copy (ap1, ap); + len = vsnprintf (NULL, 0, format, ap1) + 1; + va_end (ap1); + newstr = xmalloc (len); - va_start (ap, format); - vsnprintf (newstr, len, format, ap); - va_end (ap); + va_copy (ap2, ap); + vsnprintf (newstr, len, format, ap2); + va_end (ap2); return newstr; } - diff --git a/hw4/l4basic/l4str.h b/hw4/l4basic/l4str.h index cda3c1d..b041e22 100644 --- a/hw4/l4basic/l4str.h +++ b/hw4/l4basic/l4str.h @@ -3,11 +3,13 @@ #define LBS_STR_H #include <l4common.h> +#include <stdarg.h> #define LBS_STR_STATIC_STRLEN(x) (sizeof(x)/sizeof(char) - 1) #define LBS_STR_ARRAY_LEN(x,t) (sizeof(x)/sizeof(t)) bool lbs_str_has_suffix (const char* str, const char* suffix); char* lbs_str_printf (const char* format, ...); +char* lbs_str_vprintf (const char* format, va_list ap); #endif /* LBS_STR_H */ |