From dcea4c409f4deadbc176b43e8cdbfd5cd9edac3f Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 27 Oct 2008 10:18:06 +0100 Subject: [PATCH] Add mksnpath which allows to specify the output buffer To be used as alternative to mkpath where the buffer for the created path may not be be reused by subsequent calls of the function or will be copied anyway. It is actually just a vsnprintf's but additionally calls cleanup_path on the result. --- cache.h | 2 ++ path.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 0 deletions(-) diff --git a/cache.h b/cache.h index b0edbf9..9efa602 100644 --- a/cache.h +++ b/cache.h @@ -495,6 +495,8 @@ 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 76e8872..8b64878 100644 --- 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; -- 1.6.0.3.549.gb475d