From 75920d2c12eb643621d64529eaf3ce1675b6122d Mon Sep 17 00:00:00 2001 From: Dan Robertson Date: Sat, 10 Mar 2018 02:23:50 +0000 Subject: [PATCH] Add implementations of sprintf and snprintf Add implementations of sprintf and snprintf so that we can get a bit closer to compiling libc-test. --- include/bits/stdio.h | 42 ++++++++++++++++++++++++++++++------------ include/sys/types.h | 7 +++++++ tests/.gitignore | 1 + tests/Makefile | 1 + tests/sprintf.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 tests/sprintf.c diff --git a/include/bits/stdio.h b/include/bits/stdio.h index 351dc1e774..d990dae61b 100644 --- a/include/bits/stdio.h +++ b/include/bits/stdio.h @@ -2,21 +2,39 @@ #define _BITS_STDIO_H int fprintf(FILE * stream, const char * fmt, ...) { - int ret; - va_list ap; - va_start(ap, fmt); - ret = vfprintf(stream, fmt, ap); - va_end(ap); - return ret; + int ret; + va_list ap; + va_start(ap, fmt); + ret = vfprintf(stream, fmt, ap); + va_end(ap); + return ret; } int printf(const char * fmt, ...) { - int ret; - va_list ap; - va_start(ap, fmt); - ret = vprintf(fmt, ap); - va_end(ap); - return ret; + int ret; + va_list ap; + va_start(ap, fmt); + ret = vprintf(fmt, ap); + va_end(ap); + return ret; +} + +int snprintf(char *s, size_t n, const char * fmt, ...) { + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsnprintf(s, n, fmt, ap); + va_end(ap); + return ret; +} + +int sprintf(char *s, const char * fmt, ...) { + int ret; + va_list ap; + va_start(ap, fmt); + ret = vsprintf(s, fmt, ap); + va_end(ap); + return ret; } #endif /* _BITS_STDIO_H */ diff --git a/include/sys/types.h b/include/sys/types.h index 7312acfd4b..0db183a796 100644 --- a/include/sys/types.h +++ b/include/sys/types.h @@ -32,4 +32,11 @@ typedef int clockid_t; typedef void* timer_t; +#ifdef __linux__ +#define _SC_PAGE_SIZE 30 +#endif +#ifdef __redox__ +#define _SC_PAGE_SIZE 8 +#endif + #endif /* _SYS_TYPES_H */ diff --git a/tests/.gitignore b/tests/.gitignore index 620d8c7244..43fdd53666 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -26,6 +26,7 @@ /printf /rmdir /setid +/sprintf /stdlib/strtol /unlink /write diff --git a/tests/Makefile b/tests/Makefile index ae73d603b1..3b732f4cca 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -21,6 +21,7 @@ BINS=\ rmdir \ setid \ sleep \ + sprintf \ stdlib/strtol \ unlink \ write diff --git a/tests/sprintf.c b/tests/sprintf.c new file mode 100644 index 0000000000..e91f9baee3 --- /dev/null +++ b/tests/sprintf.c @@ -0,0 +1,30 @@ + +#include + +int main(int argc, char ** argv) { + char buffer[72]; + int ret = sprintf( + buffer, + "This string fits in the buffer because it is only %d bytes in length", + 68 + ); + + if (ret) { + printf("Failed! %d\n", ret); + return -1; + } + + ret = snprintf( + buffer, + 72, + "This string is way longer and does not fit in the buffer because it %d bytes in length", + 87 + ); + + if (!ret) { + return 0; + } else { + printf("Failed! %d", ret); + return -1; + } +}