From 108bebeab31881654b7b0f1b5b393a6655d74d3f Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Sun, 26 Oct 2008 22:59:13 +0100 Subject: Add mksnpath which allows you to specify the output buffer 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 Signed-off-by: Junio C Hamano --- path.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'path.c') diff --git a/path.c b/path.c index 76e8872622..8b64878c21 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; -- cgit v1.2.3 From fe2d7776d5191896e361973f478ca078fa95b324 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 27 Oct 2008 10:22:21 +0100 Subject: Add git_snpath: a .git path formatting routine with output buffer The function's purpose is to replace git_path where the buffer of formatted path may not be reused by subsequent calls of the function or will be copied anyway. Signed-off-by: Junio C Hamano --- path.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'path.c') diff --git a/path.c b/path.c index 8b64878c21..85ab28a0f1 100644 --- a/path.c +++ b/path.c @@ -47,6 +47,29 @@ char *mksnpath(char *buf, size_t n, const char *fmt, ...) return cleanup_path(buf); } +char *git_snpath(char *buf, size_t n, const char *fmt, ...) +{ + const char *git_dir = get_git_dir(); + va_list args; + size_t len; + + len = strlen(git_dir); + if (n < len + 1) + goto bad; + memcpy(buf, git_dir, len); + if (len && !is_dir_sep(git_dir[len-1])) + buf[len++] = '/'; + va_start(args, fmt); + len += vsnprintf(buf + len, n - len, fmt, args); + va_end(args); + if (len >= n) + goto bad; + return cleanup_path(buf); +bad: + snprintf(buf, n, bad_path); + return buf; +} + char *mkpath(const char *fmt, ...) { va_list args; -- cgit v1.2.3 From aba13e7c0566f578f866504bfcb388a72f7e5079 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 27 Oct 2008 11:17:51 +0100 Subject: git_pathdup: returns xstrdup-ed copy of the formatted path Signed-off-by: Junio C Hamano --- path.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'path.c') diff --git a/path.c b/path.c index 85ab28a0f1..092ce57190 100644 --- a/path.c +++ b/path.c @@ -47,10 +47,9 @@ char *mksnpath(char *buf, size_t n, const char *fmt, ...) return cleanup_path(buf); } -char *git_snpath(char *buf, size_t n, const char *fmt, ...) +static char *git_vsnpath(char *buf, size_t n, const char *fmt, va_list args) { const char *git_dir = get_git_dir(); - va_list args; size_t len; len = strlen(git_dir); @@ -59,9 +58,7 @@ char *git_snpath(char *buf, size_t n, const char *fmt, ...) memcpy(buf, git_dir, len); if (len && !is_dir_sep(git_dir[len-1])) buf[len++] = '/'; - va_start(args, fmt); len += vsnprintf(buf + len, n - len, fmt, args); - va_end(args); if (len >= n) goto bad; return cleanup_path(buf); @@ -70,6 +67,25 @@ bad: return buf; } +char *git_snpath(char *buf, size_t n, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + (void)git_vsnpath(buf, n, fmt, args); + va_end(args); + return buf; +} + +char *git_pathdup(const char *fmt, ...) +{ + char path[PATH_MAX]; + va_list args; + va_start(args, fmt); + (void)git_vsnpath(path, sizeof(path), fmt, args); + va_end(args); + return xstrdup(path); +} + char *mkpath(const char *fmt, ...) { va_list args; -- cgit v1.2.3