skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

sareadlink.c (479B)


      1 /* ISC license. */
      2 
      3 #include <unistd.h>
      4 #include <skalibs/stralloc.h>
      5 #include <skalibs/djbunix.h>
      6 
      7 int sareadlink (stralloc *sa, char const *path)
      8 {
      9   size_t n = 128 ;
     10   int wasnull = !sa->s ;
     11   ssize_t r ;
     12 
     13   for (;;)
     14   {
     15     if (!stralloc_readyplus(sa, n)) goto err ;
     16     r = readlink(path, sa->s + sa->len, n) ;
     17     if (r < 0) goto err ;
     18     if ((size_t)r < n) break ;
     19     n += 128 ;
     20   }
     21   sa->len += r ;
     22   return 0 ;
     23 
     24 err:
     25   if (wasnull) stralloc_free(sa) ;
     26   return -1 ;
     27 }