| 579 | #define STRBUF_MAXLINK (32767) |
| 580 | |
| 581 | int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) |
| 582 | { |
| 583 | size_t oldalloc = sb->alloc; |
| 584 | |
| 585 | if (hint < 32) |
| 586 | hint = 32; |
| 587 | |
| 588 | while (hint < STRBUF_MAXLINK) { |
| 589 | ssize_t len; |
| 590 | |
| 591 | strbuf_grow(sb, hint + 1); |
| 592 | len = readlink(path, sb->buf, hint + 1); |
| 593 | if (len < 0) { |
| 594 | if (errno != ERANGE) |
| 595 | break; |
| 596 | } else if (len <= hint) { |
| 597 | strbuf_setlen(sb, len); |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* .. the buffer was too small - try again */ |
| 602 | hint *= 2; |
| 603 | } |
| 604 | if (oldalloc == 0) |
| 605 | strbuf_release(sb); |
| 606 | return -1; |
| 607 | } |
| 608 | |
| 609 | int strbuf_getcwd(struct strbuf *sb) |
| 610 | { |