Line data Source code
1 : /**
2 : * POSIX path implementation.
3 : * Uses $HOME / $XDG_CACHE_HOME / $XDG_CONFIG_HOME / $XDG_DATA_HOME,
4 : * falling back to getpwuid(getuid()) for the home directory.
5 : */
6 : #include "../path.h"
7 : #include <stdlib.h>
8 : #include <stdio.h>
9 : #include <string.h>
10 : #include <pwd.h>
11 : #include <unistd.h>
12 : #include <sys/types.h>
13 :
14 : static char g_home[4096];
15 : static char g_cache[8192];
16 : static char g_config[8192];
17 : static char g_data[8192];
18 :
19 197 : const char *platform_home_dir(void) {
20 197 : const char *h = getenv("HOME");
21 197 : if (!h || !*h) {
22 2 : struct passwd *pw = getpwuid(getuid());
23 2 : if (pw) h = pw->pw_dir;
24 : }
25 197 : if (!h) return NULL;
26 197 : snprintf(g_home, sizeof(g_home), "%s", h);
27 197 : return g_home;
28 : }
29 :
30 98 : const char *platform_cache_dir(void) {
31 98 : const char *xdg = getenv("XDG_CACHE_HOME");
32 98 : if (xdg && *xdg) {
33 1 : snprintf(g_cache, sizeof(g_cache), "%s", xdg);
34 1 : return g_cache;
35 : }
36 97 : const char *home = platform_home_dir();
37 97 : if (!home) return NULL;
38 97 : snprintf(g_cache, sizeof(g_cache), "%s/.cache", home);
39 97 : return g_cache;
40 : }
41 :
42 63 : const char *platform_config_dir(void) {
43 63 : const char *xdg = getenv("XDG_CONFIG_HOME");
44 63 : if (xdg && *xdg) {
45 1 : snprintf(g_config, sizeof(g_config), "%s", xdg);
46 1 : return g_config;
47 : }
48 62 : const char *home = platform_home_dir();
49 62 : if (!home) return NULL;
50 62 : snprintf(g_config, sizeof(g_config), "%s/.config", home);
51 62 : return g_config;
52 : }
53 :
54 115 : const char *platform_data_dir(void) {
55 115 : const char *xdg = getenv("XDG_DATA_HOME");
56 115 : if (xdg && *xdg) {
57 81 : snprintf(g_data, sizeof(g_data), "%s", xdg);
58 81 : return g_data;
59 : }
60 34 : const char *home = platform_home_dir();
61 34 : if (!home) return NULL;
62 34 : snprintf(g_data, sizeof(g_data), "%s/.local/share", home);
63 34 : return g_data;
64 : }
|