ref: 34bc68e7ade75c1627c7de6cbaa605601f2479fe
parent: 5a13b1a8c6e3232be716f6cfeb7dec710f947f14
author: Roberto E. Vargas Caballero <[email protected]>
date: Sun Dec 10 04:56:42 EST 2017
[lib/c] Add sprintf(), snprintf(), vsprintf() and vsnprintf()
--- a/lib/c/src/Makefile
+++ b/lib/c/src/Makefile
@@ -5,6 +5,7 @@
OBJ = bsearch.o qsort.o \
abs.o __abs.o labs.o __labs.o llabs.o __labs.o \
perror.o strerror.o \
+ sprintf.o snprintf.o vsprintf.o vsnprintf.o \
printf.o fprintf.o vfprintf.o \
fgets.o gets.of fgetc.o fputc.o getchar.o putchar.o \
fputs.o puts.o fread.o fwrite.o \
--- /dev/null
+++ b/lib/c/src/snprintf.c
@@ -1,0 +1,18 @@
+
+#include <stdarg.h>
+#include <stdio.h>
+#undef snprintf
+
+int
+snprintf(char * restrict s, size_t siz, const char * restrict fmt, ...)
+{
+ int r;
+ va_list va;
+
+ va_list va_arg;
+ va_start(va, fmt);
+ r = vsnprintf(s, siz, fmt, va);
+ va_end(va);
+
+ return r;
+}
--- /dev/null
+++ b/lib/c/src/sprintf.c
@@ -1,0 +1,17 @@
+
+#include <stdarg.h>
+#include <stdio.h>
+#undef sprintf
+
+int
+sprintf(char * restrict s, const char * restrict fmt, ...)
+{
+ int r;
+
+ va_list va;
+ va_start(va, fmt);
+ r = vsprintf(s, fmt, va);
+ va_end(va);
+
+ return r;
+}
--- /dev/null
+++ b/lib/c/src/vsnprintf.c
@@ -1,0 +1,26 @@
+
+#include <stdarg.h>
+#include <stdio.h>
+#undef vsnprintf
+
+int
+vsnprintf(char * restrict s, size_t siz, const char * restrict fmt, va_list ap)
+{
+ FILE f;
+ int r;
+
+ f.flag = _IOWRT | _IOSTRG;
+ f.size = siz;
+ f.buf = s;
+ f.wp = s;
+ f.rp = s + siz;
+
+ r = vfprintf(&f, fmt, va);
+ if (s) {
+ if (f.wp == f.rp)
+ --f.wp;
+ *f.wp = '\0';
+ }
+
+ return r;
+}
--- /dev/null
+++ b/lib/c/src/vsprintf.c
@@ -1,0 +1,12 @@
+
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#undef vsprintf
+
+
+int
+vsprintf(char * restrict s, const char * restrict fmt, va_list va)
+{
+ return vsnprintf(s, SIZE_MAX, fmt, va);
+}