summaryrefslogtreecommitdiff
path: root/src/libstddjb/sarealpath.c
blob: 8c994c46ed3e07856c91038edae91a37af5fc2f0 (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
28
29
30
31
32
33
34
35
36
37
/* ISC license. */

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

int sarealpath (stralloc *sa, char const *path)
{
  if (sa->s)
  {
#ifdef PATH_MAX
    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 ;
    if (!stralloc_cats(sa, p) || !stralloc_0(sa))
    {
      free(p) ;
      return -1 ;
    }
    free(p) ;
#endif
  }
  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 ;
}