~nytpu/xdgbasedir.c

ref: e0344cc04c7ed83928dce5abbe002f63477d78a1 xdgbasedir.c/xdgbasedir.c -rw-r--r-- 3.1 KiB
e0344cc0nytpu fix license issue 6 months ago
                                                                                
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
// VERSION: 1.1.0
/* Return directories following the XDG Base Directory Specification.  First
 * searches the "single base directory" environment variables such as
 * $XDG_DATA_HOME.  If that variable doesn't exist or is empty, then returns
 * the first directory in the "set" environment variables such as
 * $XDG_DATA_DIRS.  If the set variable doesn't exist or is empty, returns the
 * default directory such as $HOME/.local/share.
 *
 * <https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html>
 *
 *
 * Copyright (c) 2021 nytpu <alex [at] nytpu.com>
 * SPDX-License-Identifier: MPL-2.0
 * The orginal source for this file is available at <https://git.sr.ht/~nytpu/xdgbasedir.c>.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
 */

#include "xdgbasedir.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Duplicate a string as per POSIX.1-2008
char *strdup(const char *s);

// Return an XDG directory.  First check in the mainenv variable, and if it
// exists return it.  Otherwise, check for the setenv variable and return the
// first directory in it, if it exists.  Finally, fallback to $HOME/fallback.
// If $HOME and all other checked environment variables don't exist or an error
// occurs, returns NULL.
// The returned string should be freed.
static char *get_dir(const char *env_main, const char *env_set, const char *fallback);

#if ! (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 500 || defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
char *
strdup(const char *s)
{
	if (s == NULL) return NULL;
	char *n = malloc(strlen(s) + 1);
	if (n == NULL) return NULL;
	strcpy(n, s);
	return n;
}
#endif

char *
xdg_append_to_path(char *base_path, const char *file)
{
	if (base_path == NULL || file == NULL) return NULL;
	char *n = realloc(base_path, strlen(base_path) + 1 + strlen(file) + 1); // base_path + '/' + file + '\0'
	if (n == NULL) return NULL;
	int ret = sprintf(n, "%s/%s", base_path, file);
	if (ret < 1) {
		free(n);
		return NULL;
	}
	return n;
}

static char *
get_dir(const char *env_main, const char *env_set, const char *fallback)
{
	char *env = getenv(env_main);
	if (env != NULL) {
		return strdup(env);
	}

	// Should only be used for data and config dirs
	if (env_set != NULL) {
		env = getenv(env_set);
		if (env != NULL) {
			char *e = env;
			while (*e != '\0' && *e != ':') e++;
			*e = '\0';
			return strdup(env);
		}
	}

	// Everything except for runtime dir
	if (fallback != NULL) {
		env = getenv("HOME");
		if (env != NULL) return xdg_append_to_path(env, fallback);
	}

	return NULL;
}

char *
xdg_get_data_dir(void)
{
	return get_dir("XDG_DATA_HOME", "XDG_DATA_DIRS", ".local/share");
}

char *
xdg_get_config_dir(void)
{
	return get_dir("XDG_CONFIG_HOME", "XDG_CONFIG_DIRS", ".config");
}

char *
xdg_get_state_dir(void)
{
	return get_dir("XDG_STATE_HOME", NULL, ".local/state");
}

char *
xdg_get_cache_dir(void)
{
	return get_dir("XDG_CACHE_HOME", NULL, ".cache");
}

char *
xdg_get_runtime_dir(void)
{
	return get_dir("XDG_RUNTIME_DIR", NULL, NULL);
}