sarealpath.c (765B)
1 /* ISC license. */ 2 3 #include <limits.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <skalibs/stralloc.h> 7 #include <skalibs/djbunix.h> 8 9 int sarealpath (stralloc *sa, char const *path) 10 { 11 if (sa->s) 12 { 13 #ifdef PATH_MAX 14 if (!stralloc_readyplus(sa, PATH_MAX)) return -1 ; 15 if (!realpath(path, sa->s + sa->len)) return -1 ; 16 sa->len += strlen(sa->s + sa->len) ; 17 #else 18 char *p = realpath(path, 0) ; 19 if (!p) return -1 ; 20 if (!stralloc_cats(sa, p) || !stralloc_0(sa)) 21 { 22 free(p) ; 23 return -1 ; 24 } 25 free(p) ; 26 #endif 27 } 28 else 29 { 30 char *p = realpath(path, 0) ; 31 if (!p) return -1 ; 32 sa->s = p ; /* XXX: incompatible with alloc() interposition */ 33 sa->len = strlen(p) ; 34 sa->a = sa->len + 1 ; 35 } 36 return 0 ; 37 }