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 <gitster@pobox.com>
Alex Riesen committed
Oct 27, 2008 at 10:22 UTC
fe2d7776d5191896e361973f478ca078fa95b324
2 files changed
+25
cache.h
+2
@@ -482,6 +482,8 @@ extern int check_repository_format(void);
482
483
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
488
/* Return a statically allocated filename matching the sha1 signature */
489
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
path.c
+23
@@ -47,6 +47,29 @@ 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, ...)
51
+{
52
+ const char *git_dir = get_git_dir();
53
+ va_list args;
54
+ size_t len;
55
+
56
+ len = strlen(git_dir);
57
+ if (n < len + 1)
58
+ goto bad;
59
+ memcpy(buf, git_dir, len);
60
+ if (len && !is_dir_sep(git_dir[len-1]))
61
+ buf[len++] = '/';
62
+ va_start(args, fmt);
63
+ len += vsnprintf(buf + len, n - len, fmt, args);
64
+ va_end(args);
65
+ if (len >= n)
66
+ goto bad;
67
+ return cleanup_path(buf);
68
+bad:
69
+ snprintf(buf, n, bad_path);
70
+ return buf;
71
+}
72
+
73
char *mkpath(const char *fmt, ...)
74
{
75
va_list args;