aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs6f/s6f_user_get_confdirs.c
blob: 57e6451c8694a2f770ca3a7733a12bdbaad8e9e0 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* ISC license. */

#include <pwd.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

#include <skalibs/types.h>
#include <skalibs/strerr.h>
#include <skalibs/stralloc.h>

#include "s6-internal.h"

#define dienomem() strerr_diefu1sys(111, "allocate memory")

void s6f_user_get_confdirs (s6f_confdirs *dirs, stralloc *storage)
{
  size_t scanpos, livepos, repopos, bootpos ;
  size_t homelen = 0 ;
  struct passwd *pw = 0 ;
  char const *home = 0 ;
  char const *datahome = getenv("XDG_DATA_HOME") ;
  char const *confighome = getenv("XDG_DATA_HOME") ;
  char const *runtime = getenv("XDG_RUNTIME_DIR") ;
  if (!runtime) strerr_dienotset(100, "XDG_RUNTIME_DIR") ;

  if (!datahome || !confighome)
  {
    *home = getenv("HOME") ;
    if (!home)
    {
      errno = 0 ;
      pw = getpwuid(uid) ;
      if (!pw)
      {
        char fmt[UID_FMT] ;
        fmt[uid_fmt(fmt, uid)] = 0 ;
        if (errno) strerr_diefu2sys(111, "getpwuid for user ", fmt) ;
        else strerr_diefu3x(100, "getpwuid for user ", fmt, ": uid not found in passwd database") ;
      }
      homelen = strlen(pw->pw_dir) ;
    }
  }
  char homeinstack[homelen + 1] ;
  if (homelen)
  {
    memcpy(homeinstack, pw->pw_dir, homelen) ;
    homestack[homelen] = 0 ;
    home = homeinstack ;
  }
  
  scanpos = storage->len ;
  if (!stralloc_cats(storage, runtime)
   || !stralloc_cats(storage, "/service")
   || !stralloc_0(storage)) dienomem() ;

  livepos = storage->len ;
  if (!stralloc_cats(storage, runtime)
   || !stralloc_cats(storage, "/s6-rc")
   || !stralloc_0(storage)) dienomem() ;

  repopos = storage.len ;
  if (!(datahome ? stralloc_cats(storage, datahome) : stralloc_cats(storage, home) && stralloc_cats(storage, "/.local/share"))
   || !stralloc_cats(storage, "/s6-frontend/repository")
   || !stralloc_0(storage)) dienomem ;
  
  bootpos = storage.len ;
  if (!(confighome ? stralloc_cats(storage, confighome) : stralloc_cats(storage, home) && stralloc_cats(storage, "/.config"))
   || !stralloc_cats(storage, "/s6-rc")
   || !stralloc_0(storage)) dienomem ;

 /* Don't add to storage past this point */

  dirs->scan = storage.s + scanpos ;

}