Permalink
Browse files

Added snprintfcat to vsprintf.c

Author: Far McKon <FarMcKon@buglabs.net>
Signed-off-by: Matt Isaacs <haveahennessy@gmail.com>
  • Loading branch information...
1 parent 01e21c4 commit 2a354d94757ef4685bfeead627e7a5328c45bade @haveahennessy haveahennessy committed May 10, 2011
Showing with 32 additions and 0 deletions.
  1. +2 −0 include/linux/kernel.h
  2. +30 −0 lib/vsprintf.c
View
@@ -196,6 +196,8 @@ extern int vsprintf(char *buf, const char *, va_list)
__attribute__ ((format (printf, 2, 0)));
extern int snprintf(char * buf, size_t size, const char * fmt, ...)
__attribute__ ((format (printf, 3, 4)));
+extern int snprintfcat(char *buf, size_t size, const char *fmt, ...)
+ __attribute__ ((format (printf, 3, 4)));
extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
__attribute__ ((format (printf, 3, 0)));
extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
View
@@ -1236,6 +1236,36 @@ int format_decode(const char *fmt, struct printf_spec *spec)
}
/**
+ * snprintfcat - Formats and appends a string to a passed buffer.
+ * @buf: The buffer to place the result into
+ * @size: The size of the buffer, including the trailing null space
+ * @fmt: The format string to use
+ * @args: Arguments for the format string
+ *
+ * see vsnprintf for supported C conventions
+ *
+ * The return value is the number of characters in the entire buffer
+ * excluding the trailing '\0' (per ISO C99). If the filled buffer size
+ * is greater than or equal to @size, the resulting
+ * string is truncated.
+ */
+int snprintfcat(char *buf, size_t size,
+ char const *fmt, ...)
+{
+ size_t result;
+ va_list args;
+ size_t len = strnlen(buf, size);
+
+ if (len >= size)
+ return len; /* no string added, buffer full */
+
+ va_start(args, fmt);
+ result = vsnprintf(buf + len, size - len, fmt, args);
+ va_end(args);
+ return result + len;
+}
+
+/**
* vsnprintf - Format a string and place it in a buffer
* @buf: The buffer to place the result into
* @size: The size of the buffer, including the trailing null space

0 comments on commit 2a354d9

Please sign in to comment.