summaryrefslogtreecommitdiff
path: root/src/libs6rc/s6rc_sanitize_dir.c
blob: 69c4d132f89bf290238c2a0e43cf17562bef74ab (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 <skalibs/bytestr.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
#include <s6-rc/s6rc-utils.h>

int s6rc_sanitize_dir (stralloc *sa, char const *fn, unsigned int *dirlen)
{
  int wasnull = !sa->s ;
  unsigned int base = sa->len ;
  unsigned int fnlen = str_len(fn) ;
  unsigned int ddirlen ;
  if (!sadirname(sa, fn, fnlen)) return 0 ;
  if (sa->len != base + 1 || sa->s[base] != '/')
    if (!stralloc_catb(sa, "/", 1)) goto err ;
  ddirlen = sa->len ;
  if (!sabasename(sa, fn, fnlen)) goto err ;
  *dirlen = ddirlen ;
  return 1 ;

 err:
  if (wasnull) stralloc_free(sa) ;
  else sa->len = base ;
  return 0 ;
}