scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 720fe1d5f39c96fe6b8a334ba50132375d02658b
parent 66e334f5543b1b425aa0f34849357b970d6a746a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 10 Dec 2017 09:56:42 +0100

[lib/c] Add sprintf(), snprintf(), vsprintf() and vsnprintf()

Diffstat:
Mlib/c/src/Makefile | 1+
Alib/c/src/snprintf.c | 18++++++++++++++++++
Alib/c/src/sprintf.c | 17+++++++++++++++++
Alib/c/src/vsnprintf.c | 26++++++++++++++++++++++++++
Alib/c/src/vsprintf.c | 12++++++++++++
5 files changed, 74 insertions(+), 0 deletions(-)

diff --git a/lib/c/src/Makefile b/lib/c/src/Makefile @@ -5,6 +5,7 @@ include ../../../config.mk 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 \ diff --git a/lib/c/src/snprintf.c b/lib/c/src/snprintf.c @@ -0,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; +} diff --git a/lib/c/src/sprintf.c b/lib/c/src/sprintf.c @@ -0,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; +} diff --git a/lib/c/src/vsnprintf.c b/lib/c/src/vsnprintf.c @@ -0,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; +} diff --git a/lib/c/src/vsprintf.c b/lib/c/src/vsprintf.c @@ -0,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); +}