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

[PATCH 1/3] strbuf: add strbuf_add_cwd()

From
René Scharfe <l.s.r@web.de>
Date
Jul 20, 2014, 11:21 UTC
Message-ID
<53CBA62A.5010800@web.de>
In-Reply-To
<53CBA59C.8050901@web.de>

Add strbuf_add_cwd(), which adds the current working directory to a strbuf. Because it doesn't use a fixed-size buffer it supports arbitrarily long paths, as long as the platform's getcwd() does as well. At least on Linux and FreeBSD it handles paths longer than PATH_MAX just fine.

Suggested-by: Karsten Blees <karsten.blees@gmail.com>
Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 Documentation/technical/api-strbuf.txt |  4 ++++
 strbuf.c                               | 22 ++++++++++++++++++++++
 strbuf.h                               |  1 +
 3 files changed, 27 insertions(+)
diff --git a/Documentation/technical/api-strbuf.txt b/Documentation/technical/api-strbuf.txt
index f9c06a7..b96b78c 100644
--- a/Documentation/technical/api-strbuf.txt
+++ b/Documentation/technical/api-strbuf.txt
@@ -257,6 +257,10 @@ which can be used by the programmer of the callback as she sees fit.
 
 	Add a formatted string to the buffer.
 
+`strbuf_add_cwd`::
+
+	Add the current working directory to the buffer.
+
 `strbuf_commented_addf`::
 
 	Add a formatted string prepended by a comment character and a
diff --git a/strbuf.c b/strbuf.c
index 33018d8..4e44773 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -406,6 +406,28 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
 	return -1;
 }
 
+int strbuf_add_cwd(struct strbuf *sb)
+{
+	size_t oldalloc = sb->alloc;
+	size_t guessed_len = 32;
+
+	for (;; guessed_len *= 2) {
+		char *cwd;
+
+		strbuf_grow(sb, guessed_len);
+		cwd = getcwd(sb->buf + sb->len, sb->alloc - sb->len);
+		if (cwd) {
+			strbuf_setlen(sb, sb->len + strlen(cwd));
+			return 0;
+		}
+		if (errno != ERANGE)
+			break;
+	}
+	if (oldalloc == 0)
+		strbuf_release(sb);
+	return -1;
+}
+
 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 {
 	int ch;
diff --git a/strbuf.h b/strbuf.h
index a7c0192..ba95cd6 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -174,6 +174,7 @@ extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
 extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
 extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
 extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
+extern int strbuf_add_cwd(struct strbuf *sb);
 
 extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
 extern int strbuf_getline(struct strbuf *, FILE *, int);
-- 
2.0.2
Previous: René ScharfeNext: Duy Nguyen
Message 2 of 10 in “getcwd without PATH_MAX”
  1. 0/3 getcwd without PATH_MAXRené Scharfe, Jul 20, 2014
  2. 1/3 strbuf: add strbuf_add_cwd()René Scharfe, Jul 20, 2014
  3. Duy NguyenJul 20, 2014
  4. René ScharfeJul 20, 2014
  5. 2/3 wrapper: add xgetcwd()René Scharfe, Jul 20, 2014
  6. Duy NguyenJul 20, 2014
  7. René ScharfeJul 20, 2014
  8. 3/3 use xgetcwd() get the current directory or dieRené Scharfe, Jul 20, 2014
  9. Duy NguyenJul 20, 2014
  10. René ScharfeJul 20, 2014

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.