Skip to content

Commit 108bebe

Browse files
raalkmlgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9fe7a64 commit 108bebe

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ extern int check_repository_format(void);
480480
#define DATA_CHANGED 0x0020
481481
#define TYPE_CHANGED 0x0040
482482

483+
extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
484+
__attribute__((format (printf, 3, 4)));
485+
483486
/* Return a statically allocated filename matching the sha1 signature */
484487
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
485488
extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));

path.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ static char *cleanup_path(char *path)
3232
return path;
3333
}
3434

35+
char *mksnpath(char *buf, size_t n, const char *fmt, ...)
36+
{
37+
va_list args;
38+
unsigned len;
39+
40+
va_start(args, fmt);
41+
len = vsnprintf(buf, n, fmt, args);
42+
va_end(args);
43+
if (len >= n) {
44+
snprintf(buf, n, bad_path);
45+
return buf;
46+
}
47+
return cleanup_path(buf);
48+
}
49+
3550
char *mkpath(const char *fmt, ...)
3651
{
3752
va_list args;

0 commit comments

Comments
 (0)