git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Add mksnpath and git_snpath which allow to specify the output buffer

From
Alex Riesen <raa.lkml@gmail.com>
Date
Oct 26, 2008, 21:59 UTC
Message-ID
<20081026215913.GA18594@blimp.localdomain>

Both are actually just vsnprintf's but additionally call cleanup_path on the result. To be used as alternatives to mkpath and git_path 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>
---
 cache.h |    4 ++++
 path.c  |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/cache.h b/cache.h
index b0edbf9..a9024db 100644
--- a/cache.h
+++ b/cache.h
@@ -495,6 +495,10 @@ 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)));
+extern char *git_snpath(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)));
diff --git a/path.c b/path.c
index 76e8872..85ab28a 100644
--- a/path.c
+++ b/path.c
@@ -32,6 +32,44 @@ 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 *git_snpath(char *buf, size_t n, const char *fmt, ...)
+{
+	const char *git_dir = get_git_dir();
+	va_list args;
+	size_t len;
+
+	len = strlen(git_dir);
+	if (n < len + 1)
+		goto bad;
+	memcpy(buf, git_dir, len);
+	if (len && !is_dir_sep(git_dir[len-1]))
+		buf[len++] = '/';
+	va_start(args, fmt);
+	len += vsnprintf(buf + len, n - len, fmt, args);
+	va_end(args);
+	if (len >= n)
+		goto bad;
+	return cleanup_path(buf);
+bad:
+	snprintf(buf, n, bad_path);
+	return buf;
+}
+
 char *mkpath(const char *fmt, ...)
 {
 	va_list args;
-- 
1.6.0.3.540.g3f8b
Next: Alex Riesen
Message 1 of 10 in “Add mksnpath and git_snpath which allow to specify the output buffer”
  1. Add mksnpath and git_snpath which allow to specify the output bufferAlex Riesen, Oct 26, 2008
  2. Fix mkpath abuse in dwim_ref and dwim_log of sha1_name.cAlex Riesen, Oct 26, 2008
  3. Fix potentially dangerous uses of mkpath and git_pathAlex Riesen, Oct 26, 2008
  4. Johannes SixtOct 27, 2008
  5. Alex RiesenOct 27, 2008
  6. Junio C HamanoOct 27, 2008
  7. Alex RiesenOct 27, 2008
  8. Junio C HamanoOct 28, 2008
  9. Alex RiesenOct 28, 2008
  10. Alex RiesenOct 28, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.