[SLOF] [PATCH v3 2/8] libc: Add the snprintf() function

Thomas Huth thuth at redhat.com
Sat May 26 16:06:02 AEST 2018


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 at redhat.com>
---
 lib/libc/include/stdio.h    |  1 +
 lib/libc/stdio/Makefile.inc |  2 +-
 lib/libc/stdio/snprintf.c   | 28 ++++++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 lib/libc/stdio/snprintf.c

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..19c8a38
--- /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;
+}
-- 
1.8.3.1



More information about the SLOF mailing list