git_pathdup: returns xstrdup-ed copy of the formatted path
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alex Riesen committed
Oct 27, 2008 at 11:17 UTC
aba13e7c0566f578f866504bfcb388a72f7e5079
2 files changed
+22
-4
cache.h
+2
@@ -484,6 +484,8 @@ extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
484
__attribute__((format (printf, 3, 4)));
485
extern char *git_snpath(char *buf, size_t n, const char *fmt, ...)
486
__attribute__((format (printf, 3, 4)));
487
+extern char *git_pathdup(const char *fmt, ...)
488
+ __attribute__((format (printf, 1, 2)));
489
490
/* Return a statically allocated filename matching the sha1 signature */
491
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
path.c
+20
-4
@@ -47,10 +47,9 @@ char *mksnpath(char *buf, size_t n, const char *fmt, ...)
47
return cleanup_path(buf);
48
}
49
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)
51
{
52
const char *git_dir = get_git_dir();
53
- va_list args;
53
size_t len;
54
55
len = strlen(git_dir);
@@ -59,9 +58,7 @@ char *git_snpath(char *buf, size_t n, const char *fmt, ...)
58
memcpy(buf, git_dir, len);
59
if (len && !is_dir_sep(git_dir[len-1]))
60
buf[len++] = '/';
62
- va_start(args, fmt);
61
len += vsnprintf(buf + len, n - len, fmt, args);
64
- va_end(args);
62
if (len >= n)
63
goto bad;
64
return cleanup_path(buf);
@@ -70,6 +67,25 @@ bad:
67
return buf;
68
}
69
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
+
89
char *mkpath(const char *fmt, ...)
90
{
91
va_list args;