summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code2/appendsnprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'comp/lucas-standen-NEA/code2/appendsnprintf.c')
-rw-r--r--comp/lucas-standen-NEA/code2/appendsnprintf.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/comp/lucas-standen-NEA/code2/appendsnprintf.c b/comp/lucas-standen-NEA/code2/appendsnprintf.c
new file mode 100644
index 0000000..637d214
--- /dev/null
+++ b/comp/lucas-standen-NEA/code2/appendsnprintf.c
@@ -0,0 +1,35 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *genfmt(char *buf, char *fmt){
+ int len = strlen(buf) + strlen(fmt) + 1;
+ char *out = malloc(len);
+
+ int j = 0, i = 0;
+ while (buf[i] != '\0'){
+ out[i] = buf[i];
+ i++;
+ }
+ while (fmt[j] != '\0'){
+ out[i] = fmt[j];
+ i++;
+ j++;
+ }
+ return out;
+}
+
+char *appendsnprintf(char *buf, int size, char *format, ...){
+ va_list ap;
+ char *outputbuf = malloc(size);
+ va_start(ap, format);
+ char *fmt = genfmt(buf, format);
+ vsnprintf(outputbuf, size, fmt, ap);
+ free(fmt);
+
+ buf = realloc(outputbuf, strlen(outputbuf) + 1);
+ va_end(ap);
+
+ return buf;
+}