diff options
author | Thomas Huth <thuth@redhat.com> | 2018-05-26 08:06:02 +0200 |
---|---|---|
committer | Alexey Kardashevskiy <aik@ozlabs.ru> | 2018-05-29 19:06:14 +1000 |
commit | 16661f937c717e68962e3956fcd0b11198fca557 (patch) | |
tree | 0c4fc2985a725cb9994822c2841af46fc1b730f3 /lib | |
parent | b08529db7053c9bb06a2b4fd4500b28181bdfdf8 (diff) | |
download | SLOF-16661f937c717e68962e3956fcd0b11198fca557.zip SLOF-16661f937c717e68962e3956fcd0b11198fca557.tar.gz SLOF-16661f937c717e68962e3956fcd0b11198fca557.tar.bz2 |
libc: Add the snprintf() function
Code has been taken from the sprintf() function (which is almost the same,
except that snprintf calls vsnprintf instead of vsprintf internally).
Signed-off-by: Thomas Huth <thuth@redhat.com>
[aik: fixed traling spaces]
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/include/stdio.h | 1 | ||||
-rw-r--r-- | lib/libc/stdio/Makefile.inc | 2 | ||||
-rw-r--r-- | lib/libc/stdio/snprintf.c | 28 |
3 files changed, 30 insertions, 1 deletions
diff --git a/lib/libc/include/stdio.h b/lib/libc/include/stdio.h index c54528f..1cd5faf 100644 --- a/lib/libc/include/stdio.h +++ b/lib/libc/include/stdio.h @@ -43,6 +43,7 @@ int fileno(FILE *stream); int printf(const char *format, ...) __attribute__((format (printf, 1, 2))); int fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3))); int sprintf(char *str, const char *format, ...) __attribute__((format (printf, 2, 3))); +int snprintf(char *str, size_t size, const char *format, ...) __attribute__((format (printf, 3, 4))); int vfprintf(FILE *stream, const char *format, va_list); int vsprintf(char *str, const char *format, va_list); int vsnprintf(char *str, size_t size, const char *format, va_list); diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc index ac5302d..6688317 100644 --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -13,7 +13,7 @@ STDIO_SRC_C = fscanf.c sprintf.c vfprintf.c vsnprintf.c vsprintf.c fprintf.c \ printf.c setvbuf.c putc.c puts.c putchar.c scanf.c stdchnls.c \ - vfscanf.c vsscanf.c fileno.c + vfscanf.c vsscanf.c fileno.c snprintf.c STDIO_SRC_ASM = STDIO_SRCS = $(STDIO_SRC_C:%=$(STDIOCMNDIR)/%) $(STDIO_SRC_ASM:%=$(STDIOCMNDIR)/%) diff --git a/lib/libc/stdio/snprintf.c b/lib/libc/stdio/snprintf.c new file mode 100644 index 0000000..e3cfa6e --- /dev/null +++ b/lib/libc/stdio/snprintf.c @@ -0,0 +1,28 @@ +/****************************************************************************** + * Copyright (c) 2004, 2008 IBM Corporation + * All rights reserved. + * This program and the accompanying materials + * are made available under the terms of the BSD License + * which accompanies this distribution, and is available at + * http://www.opensource.org/licenses/bsd-license.php + * + * Contributors: + * IBM Corporation - initial implementation + *****************************************************************************/ + +#include <stdio.h> + +int snprintf(char *buff, size_t size, const char *format, ...) +{ + va_list ar; + int count; + + if (!buff || !format) + return -1; + + va_start(ar, format); + count = vsnprintf(buff, size, format, ar); + va_end(ar); + + return count; +} |