X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=strbuf.c;h=bdf49544d47b97475800b104ed9efe8f846db265;hb=b452cc16d85ea9de7d3f15c83a917b5534a91120;hp=13be67e4d3e38472adbff019a34e3f5ce9621dd7;hpb=8c1944dd3468c8585ab3bdf149834d3256c797b4;p=git.git diff --git a/strbuf.c b/strbuf.c index 13be67e4d..bdf49544d 100644 --- a/strbuf.c +++ b/strbuf.c @@ -288,6 +288,33 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint) return sb->len - oldlen; } +#define STRBUF_MAXLINK (2*PATH_MAX) + +int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) +{ + if (hint < 32) + hint = 32; + + while (hint < STRBUF_MAXLINK) { + int len; + + strbuf_grow(sb, hint); + len = readlink(path, sb->buf, hint); + if (len < 0) { + if (errno != ERANGE) + break; + } else if (len < hint) { + strbuf_setlen(sb, len); + return 0; + } + + /* .. the buffer was too small - try again */ + hint *= 2; + } + strbuf_release(sb); + return -1; +} + int strbuf_getline(struct strbuf *sb, FILE *fp, int term) { int ch;