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