diff --git a/libc/Makefile b/libc/Makefile index 9e89d148..72a902e1 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -80,10 +80,13 @@ stdio/getline.o \ stdio/rewind.o \ stdio/setbuf.o \ stdio/setvbuf.o \ -stdio/sprint.o \ +stdio/snprintf.o \ +stdio/sprintf.o \ stdio/sscanf.o \ stdio/ungetc.o \ stdio/vfscanf.o \ +stdio/vsnprintf.o \ +stdio/vsprintf.o \ stdio/vsscanf.o \ stdlib/abort.o \ stdlib/abs.o \ diff --git a/libc/stdio/snprintf.cpp b/libc/stdio/snprintf.cpp new file mode 100644 index 00000000..eba42fbb --- /dev/null +++ b/libc/stdio/snprintf.cpp @@ -0,0 +1,36 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + stdio/snprintf.cpp + Prints a formatted string to a limited string. + +*******************************************************************************/ + +#include +#include + +extern "C" +int snprintf(char* restrict str, size_t size, const char* restrict format, ...) +{ + va_list list; + va_start(list, format); + int result = vsnprintf(str, size, format, list); + va_end(list); + return result; +} diff --git a/libc/stdio/sprintf.cpp b/libc/stdio/sprintf.cpp new file mode 100644 index 00000000..fd9cc5d7 --- /dev/null +++ b/libc/stdio/sprintf.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + stdio/sprintf.cpp + Prints a formatted string to a string. + +*******************************************************************************/ + +#include +#include + +extern "C" int sprintf(char* restrict str, const char* restrict format, ...) +{ + va_list list; + va_start(list, format); + int result = vsprintf(str, format, list); + va_end(list); + return result; +} diff --git a/libc/stdio/sprint.cpp b/libc/stdio/vsnprintf.cpp similarity index 64% rename from libc/stdio/sprint.cpp rename to libc/stdio/vsnprintf.cpp index fc517a9c..449e4a90 100644 --- a/libc/stdio/sprint.cpp +++ b/libc/stdio/vsnprintf.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. This file is part of the Sortix C Library. @@ -17,15 +17,13 @@ You should have received a copy of the GNU Lesser General Public License along with the Sortix C Library. If not, see . - stdio/sprint.cpp - Provides the stubs for the sprintf family of functions. + stdio/vsnprintf.cpp + Prints a formatted string to a limited string. *******************************************************************************/ -#include #include #include -#include #include typedef struct vsnprintf_struct @@ -36,7 +34,8 @@ typedef struct vsnprintf_struct size_t written; } vsnprintf_t; -static size_t StringPrintCallback(void* user, const char* string, size_t stringlen) +static size_t StringPrintCallback(void* user, const char* string, + size_t stringlen) { vsnprintf_t* info = (vsnprintf_t*) user; if ( info->produced < info->size ) @@ -50,37 +49,17 @@ static size_t StringPrintCallback(void* user, const char* string, size_t stringl return stringlen; } -extern "C" int vsnprintf(char* restrict str, size_t size, const char* restrict format, va_list list) +extern "C" +int vsnprintf(char* restrict str, size_t size, const char* restrict format, + va_list list) { vsnprintf_t info; info.str = str; - info.size = (size) ? size-1 : 0; + info.size = size ? size-1 : 0; info.produced = 0; info.written = 0; vprintf_callback(StringPrintCallback, &info, format, list); - if ( size ) { info.str[info.written] = '\0'; } + if ( size ) + info.str[info.written] = '\0'; return (int) info.produced; } - -extern "C" int snprintf(char* restrict str, size_t size, const char* restrict format, ...) -{ - va_list list; - va_start(list, format); - int result = vsnprintf(str, size, format, list); - va_end(list); - return result; -} - -extern "C" int vsprintf(char* restrict str, const char* restrict format, va_list list) -{ - return vsnprintf(str, SIZE_MAX, format, list); -} - -extern "C" int sprintf(char* restrict str, const char* restrict format, ...) -{ - va_list list; - va_start(list, format); - int result = vsprintf(str, format, list); - va_end(list); - return result; -} diff --git a/libc/stdio/vsprintf.cpp b/libc/stdio/vsprintf.cpp new file mode 100644 index 00000000..2e1b39ab --- /dev/null +++ b/libc/stdio/vsprintf.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + stdio/vsprintf.cpp + Prints a formatted string to a string. + +*******************************************************************************/ + +#include +#include +#include + +extern "C" +int vsprintf(char* restrict str, const char* restrict format, va_list list) +{ + return vsnprintf(str, SIZE_MAX, format, list); +}