diff options
author | René Scharfe <l.s.r@web.de> | 2014-07-28 20:24:29 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-07-28 13:48:07 -0700 |
commit | f22a76e9110e8e31efa3781b1a3cf2b4565d9e8a (patch) | |
tree | ad22c9025beca75ad94f43bede840ca46a529ba6 /strbuf.c | |
parent | ebc5da3208824e25a89672a3b91bd13629b215fe (diff) | |
download | git-f22a76e9110e8e31efa3781b1a3cf2b4565d9e8a.tar.gz |
strbuf: add strbuf_getcwd()
Add strbuf_getcwd(), which puts the current working directory into a
strbuf. Because it doesn't use a fixed-size buffer it supports
arbitrarily long paths, provided 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>
Helped-by: Duy Nguyen <pclouds@gmail.com>
Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -398,6 +398,27 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) return -1; } +int strbuf_getcwd(struct strbuf *sb) +{ + size_t oldalloc = sb->alloc; + size_t guessed_len = 128; + + for (;; guessed_len *= 2) { + strbuf_grow(sb, guessed_len); + if (getcwd(sb->buf, sb->alloc)) { + strbuf_setlen(sb, strlen(sb->buf)); + return 0; + } + if (errno != ERANGE) + break; + } + if (oldalloc == 0) + strbuf_release(sb); + else + strbuf_reset(sb); + return -1; +} + int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) { int ch; |