diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2024-05-24 19:18:11 +0000 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2024-05-24 19:18:11 +0000 |
commit | ff7ed8f586589d620a806c3758fac4a47a8e7e15 (patch) | |
tree | 729bbcb92231538fa61dab6c3d890b025484b7f5 /src/nvim/os/stdpaths.c | |
parent | 376914f419eb08fdf4c1a63a77e1f035898a0f10 (diff) | |
parent | 28c04948a1c887a1cc0cb64de79fa32631700466 (diff) | |
download | rneovim-ff7ed8f586589d620a806c3758fac4a47a8e7e15.tar.gz rneovim-ff7ed8f586589d620a806c3758fac4a47a8e7e15.tar.bz2 rneovim-ff7ed8f586589d620a806c3758fac4a47a8e7e15.zip |
Merge remote-tracking branch 'upstream/master' into mix_20240309
Diffstat (limited to 'src/nvim/os/stdpaths.c')
-rw-r--r-- | src/nvim/os/stdpaths.c | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/src/nvim/os/stdpaths.c b/src/nvim/os/stdpaths.c index ede17bc7c8..e5bdd56fe6 100644 --- a/src/nvim/os/stdpaths.c +++ b/src/nvim/os/stdpaths.c @@ -2,13 +2,16 @@ #include <stdbool.h> #include <string.h> +#include "klib/kvec.h" #include "nvim/ascii_defs.h" #include "nvim/fileio.h" #include "nvim/globals.h" #include "nvim/memory.h" #include "nvim/os/os.h" +#include "nvim/os/os_defs.h" #include "nvim/os/stdpaths_defs.h" #include "nvim/path.h" +#include "nvim/strings.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "os/stdpaths.c.generated.h" @@ -93,6 +96,46 @@ bool appname_is_valid(void) return true; } +/// Remove duplicate directories in the given XDG directory. +/// @param[in] List of directories possibly with duplicates +/// @param[out] List of directories without duplicates +static char *xdg_remove_duplicate(char *ret, const char *sep) +{ + kvec_t(char *) data = KV_INITIAL_VALUE; + char *saveptr; + + char *token = os_strtok(ret, sep, &saveptr); + while (token != NULL) { + // Check if the directory is not already in the list + bool is_duplicate = false; + for (size_t i = 0; i < data.size; i++) { + if (path_fnamecmp(kv_A(data, i), token) == 0) { + is_duplicate = true; + break; + } + } + // If it's not a duplicate, add it to the list + if (!is_duplicate) { + kv_push(data, token); + } + token = os_strtok(NULL, sep, &saveptr); + } + + StringBuilder result = KV_INITIAL_VALUE; + + for (size_t i = 0; i < data.size; i++) { + if (i == 0) { + kv_printf(result, "%s", kv_A(data, i)); + } else { + kv_printf(result, "%s%s", sep, kv_A(data, i)); + } + } + + kv_destroy(data); + xfree(ret); + return result.items; +} + /// Return XDG variable value /// /// @param[in] idx XDG variable to use. @@ -131,6 +174,10 @@ char *stdpaths_get_xdg_var(const XDGVarType idx) ret = xmemdupz(ret, len >= 2 ? len - 1 : 0); // Trim trailing slash. } + if ((idx == kXDGDataDirs || idx == kXDGConfigDirs) && ret != NULL) { + ret = xdg_remove_duplicate(ret, ENV_SEPSTR); + } + return ret; } @@ -151,7 +198,7 @@ char *get_xdg_home(const XDGVarType idx) assert(appname_len < (IOSIZE - sizeof("-data"))); if (dir) { - xstrlcpy(IObuff, appname, appname_len + 1); + xmemcpyz(IObuff, appname, appname_len); #if defined(MSWIN) if (idx == kXDGDataHome || idx == kXDGStateHome) { xstrlcat(IObuff, "-data", IOSIZE); |