summaryrefslogtreecommitdiff
path: root/src/libstddjb/sareadlink.c
blob: d6c2a4e4a95186c4c1f3a4dfcc25572571102823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* ISC license. */

#include <unistd.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>

int sareadlink (stralloc *sa, char const *path)
{
  unsigned int n = 128 ;
  int wasnull = !sa->s ;
  register int r ;

  for (;;)
  {
    if (!stralloc_readyplus(sa, n)) goto err ;
    r = readlink(path, sa->s + sa->len, n) ;
    if (r < 0) goto err ;
    if ((unsigned int)r < n) break ;
    n += 128 ;
  }
  sa->len += r ;
  return 0 ;

err:
  if (wasnull) stralloc_free(sa) ;
  return -1 ;
}