Thread (10 messages) flat view 10 messages, 2 authors, 2016-06-15

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

From: Duy Nguyen <hidden>
Date: 2016-06-15 23:01:59

On Sun, Jul 20, 2014 at 6:21 PM, René Scharfe [off-list ref] wrote:
+int strbuf_add_cwd(struct strbuf *sb)
+{
+       size_t oldalloc = sb->alloc;
+       size_t guessed_len = 32;
For Linux, I think this is enough to succesfully get cwd in the first
pass. Windows' $HOME is usually deep in C:\Users\Blahblah. Maybe
increase this to 128? And we probably want to keep the guessed value,
so if the first strbuf_add_cwd needs a few rounds to get cwd, the next
strbuf_add_cwd() call does not.
+
+       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;
+}
The loop and the following strbuf_release() are correct. But I wonder
if it's easier to read if you save getcwd in a separate/local strbuf
variable and only concat it to "sb" when you successfully get cwd..
-- 
Duy
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help