Skip to content

Commit

Permalink
git_pathdup: returns xstrdup-ed copy of the formatted path
Browse files Browse the repository at this point in the history
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Alex Riesen authored and Junio C Hamano committed Oct 31, 2008
1 parent 958a478 commit aba13e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
__attribute__((format (printf, 3, 4)));
extern char *git_snpath(char *buf, size_t n, const char *fmt, ...)
__attribute__((format (printf, 3, 4)));
extern char *git_pathdup(const char *fmt, ...)
__attribute__((format (printf, 1, 2)));

/* Return a statically allocated filename matching the sha1 signature */
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
Expand Down
24 changes: 20 additions & 4 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -70,6 +67,25 @@ char *git_snpath(char *buf, size_t n, const char *fmt, ...)
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;
Expand Down

0 comments on commit aba13e7

Please sign in to comment.