summaryrefslogtreecommitdiff
path: root/src/libstddjb/sarealpath.c
blob: 28631c62adf5e385d4fc1ba03ac883ee58d2824d (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
/* ISC license. */

#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>

int sarealpath (stralloc *sa, char const *path)
{
  if (sa->s)
  {
    if (!stralloc_readyplus(sa, PATH_MAX)) return -1 ;
    if (!realpath(path, sa->s + sa->len)) return -1 ;
    sa->len += strlen(sa->s + sa->len) ;
  }
  else
  {
    char *p = realpath(path, 0) ;
    if (!p) return -1 ;
    sa->s = p ; /* XXX: incompatible with alloc() interposition */
    sa->len = strlen(p) ;
    sa->a = sa->len + 1 ;
  }
  return 0 ;
}