Skip to content

Commit aba13e7

Browse files
raalkmlgitster
authored andcommitted
git_pathdup: returns xstrdup-ed copy of the formatted path
Signed-off-by: Junio C Hamano <[email protected]>
1 parent 958a478 commit aba13e7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
484484
__attribute__((format (printf, 3, 4)));
485485
extern char *git_snpath(char *buf, size_t n, const char *fmt, ...)
486486
__attribute__((format (printf, 3, 4)));
487+
extern char *git_pathdup(const char *fmt, ...)
488+
__attribute__((format (printf, 1, 2)));
487489

488490
/* Return a statically allocated filename matching the sha1 signature */
489491
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));

path.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ char *mksnpath(char *buf, size_t n, const char *fmt, ...)
4747
return cleanup_path(buf);
4848
}
4949

50-
char *git_snpath(char *buf, size_t n, const char *fmt, ...)
50+
static char *git_vsnpath(char *buf, size_t n, const char *fmt, va_list args)
5151
{
5252
const char *git_dir = get_git_dir();
53-
va_list args;
5453
size_t len;
5554

5655
len = strlen(git_dir);
@@ -59,9 +58,7 @@ char *git_snpath(char *buf, size_t n, const char *fmt, ...)
5958
memcpy(buf, git_dir, len);
6059
if (len && !is_dir_sep(git_dir[len-1]))
6160
buf[len++] = '/';
62-
va_start(args, fmt);
6361
len += vsnprintf(buf + len, n - len, fmt, args);
64-
va_end(args);
6562
if (len >= n)
6663
goto bad;
6764
return cleanup_path(buf);
@@ -70,6 +67,25 @@ char *git_snpath(char *buf, size_t n, const char *fmt, ...)
7067
return buf;
7168
}
7269

70+
char *git_snpath(char *buf, size_t n, const char *fmt, ...)
71+
{
72+
va_list args;
73+
va_start(args, fmt);
74+
(void)git_vsnpath(buf, n, fmt, args);
75+
va_end(args);
76+
return buf;
77+
}
78+
79+
char *git_pathdup(const char *fmt, ...)
80+
{
81+
char path[PATH_MAX];
82+
va_list args;
83+
va_start(args, fmt);
84+
(void)git_vsnpath(path, sizeof(path), fmt, args);
85+
va_end(args);
86+
return xstrdup(path);
87+
}
88+
7389
char *mkpath(const char *fmt, ...)
7490
{
7591
va_list args;

0 commit comments

Comments
 (0)