~nytpu/xdgbasedir.c

ref: 88e492907d26640fee777e818bac2e8323dc7ee4 xdgbasedir.c/xdgbasedir.c -rw-r--r-- 2.6 KiB
88e49290nytpu fix bug not following spec 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
// 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, 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, 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 *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(const char *base_path, const char *file)
{
	if (base_path == NULL || file == NULL) return NULL;
	char *n = malloc(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 *fallback)
{
	char *env = getenv(env_main);
	if (env != NULL) {
		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", ".local/share");
}

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

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

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

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