Skip to content

Commit

Permalink
Add mksnpath which allows you to specify the output buffer
Browse files Browse the repository at this point in the history
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 <raa.lkml@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Alex Riesen authored and Junio C Hamano committed Oct 27, 2008
1 parent 9fe7a64 commit 108bebe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ extern int check_repository_format(void);
#define DATA_CHANGED 0x0020
#define TYPE_CHANGED 0x0040

extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
__attribute__((format (printf, 3, 4)));

/* Return a statically allocated filename matching the sha1 signature */
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
Expand Down
15 changes: 15 additions & 0 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ static char *cleanup_path(char *path)
return path;
}

char *mksnpath(char *buf, size_t n, const char *fmt, ...)
{
va_list args;
unsigned len;

va_start(args, fmt);
len = vsnprintf(buf, n, fmt, args);
va_end(args);
if (len >= n) {
snprintf(buf, n, bad_path);
return buf;
}
return cleanup_path(buf);
}

char *mkpath(const char *fmt, ...)
{
va_list args;
Expand Down

0 comments on commit 108bebe

Please sign in to comment.