summaryrefslogtreecommitdiff
path: root/src/xdg.cc
blob: d230e0763a74b7f20f5071d7aeaac49fe9c077ee (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// -*- mode: c++; c-basic-offset: 2; -*-

#include "common.hh"

#include "paths.hh"
#include "xdg.hh"

#include <map>
#include <memory>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>

namespace {

std::string home_based_dir(char const* env_name, char const* fallback) {
  auto env = getenv(env_name);
  if (!env || !*env) {
    return Paths::join(XDG::home(), fallback);
  }
  return Paths::cleanup(env);
}

void dirs(char const* dirs, std::vector<std::string>* ret) {
  auto start = dirs;
  auto i = dirs;
  while (true) {
    if (*i == ':' || !*i) {
      if (i > start) {
        auto tmp = Paths::cleanup(std::string(start, i - start));
        if (tmp[0] == '/') {
          ret->push_back(tmp);
        }
      }
      if (!*i) break;
      start = ++i;
    } else {
      ++i;
    }
  }
}

void dirs(char const* env_name, char const* fallback,
          std::vector<std::string>* ret) {
  auto env = getenv(env_name);
  if (!env || !*env) {
    dirs(fallback, ret);
  } else {
    dirs(env, ret);
  }
}

void remove_dupes(std::vector<std::string>* lst) {
  std::multimap<size_t, std::string*> mem;
  bool modifying = false;
  auto out = lst->begin();
  for (auto it = lst->begin(); it != lst->end(); ++it) {
    auto len = it->size();
    auto pair = mem.equal_range(len);
    bool remove = false;
    for (auto j = pair.first; j != pair.second; ++j) {
      if (*(j->second) == *it) {
        remove = true;
        break;
      }
    }
    if (remove) {
      if (!modifying) {
        modifying = true;
        out = it;
      }
    } else {
      if (modifying) {
        *(out++) = *it;
      }
      mem.insert(pair.first, std::make_pair(len, &(*it)));
    }
  }
  if (modifying) {
    lst->resize(out - lst->begin());
  }
}

}  // namespace

// static
std::string XDG::config_home() {
  return home_based_dir("XDG_CONFIG_HOME", ".config");
}

// static
std::vector<std::string> XDG::config_dirs() {
  std::vector<std::string> ret;
  ret.push_back(config_home());
  dirs("XDG_CONFIG_DIRS", SYSCONFDIR "/xdg", &ret);
  remove_dupes(&ret);
  return ret;
}

// static
std::string XDG::data_home() {
  return home_based_dir("XDG_DATA_HOME", ".local/share");
}

// static
std::vector<std::string> XDG::data_dirs() {
  std::vector<std::string> ret;
  ret.push_back(data_home());
  dirs("XDG_DATA_DIRS", "/usr/local/share/:/usr/share/", &ret);
  remove_dupes(&ret);
  return ret;
}

// static
std::string XDG::cache_home() {
  return home_based_dir("XDG_CACHE_HOME", ".cache");
}

// static
std::string XDG::home() {
  auto env = getenv("HOME");
  if (!env || !*env) {
    auto size = sysconf(_SC_GETPW_R_SIZE_MAX);
    if (size == -1) size = 16384;
    auto buf = std::unique_ptr<char[]>(new char[size]);
    struct passwd pwd;
    struct passwd* result;
    auto ret = getpwuid_r(getuid(), &pwd, buf.get(), size, &result);
    if (result && ret == 0) {
      return Paths::cleanup(result->pw_dir);
    }
    return ".";
  }
  return Paths::cleanup(env);
}