]> Pileus Git - ~andy/git/commitdiff
Add mksnpath which allows you to specify the output buffer
authorAlex Riesen <raa.lkml@gmail.com>
Sun, 26 Oct 2008 21:59:13 +0000 (22:59 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Oct 2008 05:08:58 +0000 (22:08 -0700)
This is just vsnprintf's but additionally calls cleanup_path() on the
result. To be used as alternatives to mkpath() where the buffer for the
created path may not be reused by subsequent calls of the same formatting
function.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
path.c

diff --git a/cache.h b/cache.h
index 884fae826cb8c296520aecbc8c23d9848dacb606..aea13b0822d56546f30a02eb366524e6993c55df 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -480,6 +480,9 @@ extern int check_repository_format(void);
 #define DATA_CHANGED    0x0020
 #define TYPE_CHANGED    0x0040
 
+extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
+       __attribute__((format (printf, 3, 4)));
+
 /* Return a statically allocated filename matching the sha1 signature */
 extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
 extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
diff --git a/path.c b/path.c
index 76e8872622e435b050f77198ef6eef6e6ff6869e..8b64878c2147825a3a7ce483e9e6f05a0570a751 100644 (file)
--- a/path.c
+++ b/path.c
@@ -32,6 +32,21 @@ static char *cleanup_path(char *path)
        return path;
 }
 
+char *mksnpath(char *buf, size_t n, const char *fmt, ...)
+{
+       va_list args;
+       unsigned len;
+
+       va_start(args, fmt);
+       len = vsnprintf(buf, n, fmt, args);
+       va_end(args);
+       if (len >= n) {
+               snprintf(buf, n, bad_path);
+               return buf;
+       }
+       return cleanup_path(buf);
+}
+
 char *mkpath(const char *fmt, ...)
 {
        va_list args;