summaryrefslogtreecommitdiffstats
path: root/demos/utils.c
blob: 7c95d00fe76cf04b43131db08493662091dcfaf6 (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
// License: CC0 / Public Domain

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "utils.h"
#include "../src/os.h"

#ifdef PL_HAVE_WIN32
#include <shlobj.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#endif

const char *get_cache_dir(char (*buf)[512])
{
    // Check if XDG_CACHE_HOME is set for Linux/BSD
    const char* xdg_cache_home = getenv("XDG_CACHE_HOME");
    if (xdg_cache_home)
        return xdg_cache_home;

#ifdef _WIN32
    const char* local_app_data = getenv("LOCALAPPDATA");
    if (local_app_data)
        return local_app_data;
#endif

#ifdef __APPLE__
    struct passwd* pw = getpwuid(getuid());
    if (pw) {
        int ret = snprintf(*buf, sizeof(*buf), "%s/%s", pw->pw_dir, "Library/Caches");
        if (ret > 0 && ret < sizeof(*buf))
            return *buf;
    }
#endif

    const char* home = getenv("HOME");
    if (home) {
        int ret = snprintf(*buf, sizeof(*buf), "%s/.cache", home);
        if (ret > 0 && ret < sizeof(*buf))
            return *buf;
    }

    return NULL;
}