diff options
| author | Shawn O. Pearce <spearce@spearce.org> | 2008-12-31 16:06:48 -0800 |
|---|---|---|
| committer | Shawn O. Pearce <spearce@spearce.org> | 2008-12-31 16:06:48 -0800 |
| commit | 2c4b7707f7e1d591ec1765ab7057a03283841c3e (patch) | |
| tree | bc584bb33674be3b3925c7bbc129f1f53af21869 /src/util.c | |
| parent | 5690f02e87e4fd31dfa9fd7c9c01aba03603cde8 (diff) | |
| download | libgit2-2c4b7707f7e1d591ec1765ab7057a03283841c3e.tar.gz | |
Add git__fmt as an easier to use snprintf
Checking the return value of snprintf is a pain, as it must be
>= 0 and < sizeof(buffer). git__fmt is a simple wrapper to
perform these checks.
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'src/util.c')
| -rw-r--r-- | src/util.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c index feb1bd8ab..80829e69e 100644 --- a/src/util.c +++ b/src/util.c @@ -1,5 +1,7 @@ #define GIT__NO_HIDE_MALLOC #include "common.h" +#include <stdarg.h> +#include <stdio.h> void *git__malloc(size_t n) { @@ -25,6 +27,19 @@ char *git__strdup(const char *s) return r; } +int git__fmt(char *buf, size_t buf_sz, const char *fmt, ...) +{ + va_list va; + int r; + + va_start(va, fmt); + r = vsnprintf(buf, buf_sz, fmt, va); + va_end(va); + if (r < 0 || r >= buf_sz) + return GIT_ERROR; + return r; +} + int git__prefixcmp(const char *str, const char *prefix) { for (;;) { |
