From 88e492907d26640fee777e818bac2e8323dc7ee4 Mon Sep 17 00:00:00 2001 From: nytpu Date: Mon, 18 Oct 2021 10:33:06 -0600 Subject: [PATCH] fix bug not following spec --- README.md | 2 +- xdgbasedir.c | 36 +++++++++++------------------------- xdgbasedir.h | 17 +++++++---------- 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index e589a90..26a45de 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ environment variables are set. See [`xdgbasedir.h`](xdgbasedir.h) for more information. -![version: 1.1.0](https://img.shields.io/badge/version-1.1.0-informational.svg) +![version: 1.1.1](https://img.shields.io/badge/version-1.1.1-informational.svg) [![license: MPL-2.0](https://img.shields.io/badge/license-MPL--2.0-informational.svg)](LICENSE) diff --git a/xdgbasedir.c b/xdgbasedir.c index b06c20b..4bfb294 100644 --- a/xdgbasedir.c +++ b/xdgbasedir.c @@ -1,9 +1,7 @@ // 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 + * $XDG_DATA_HOME. If that variable doesn't exist or is empty, returns the * default directory such as $HOME/.local/share. * * @@ -28,12 +26,11 @@ 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. +// 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 *env_set, const char *fallback); +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 * @@ -48,10 +45,10 @@ strdup(const char *s) #endif char * -xdg_append_to_path(char *base_path, const char *file) +xdg_append_to_path(const 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' + 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) { @@ -62,24 +59,13 @@ xdg_append_to_path(char *base_path, const char *file) } static char * -get_dir(const char *env_main, const char *env_set, const char *fallback) +get_dir(const char *env_main, 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"); @@ -92,29 +78,29 @@ get_dir(const char *env_main, const char *env_set, const char *fallback) char * xdg_get_data_dir(void) { - return get_dir("XDG_DATA_HOME", "XDG_DATA_DIRS", ".local/share"); + return get_dir("XDG_DATA_HOME", ".local/share"); } char * xdg_get_config_dir(void) { - return get_dir("XDG_CONFIG_HOME", "XDG_CONFIG_DIRS", ".config"); + return get_dir("XDG_CONFIG_HOME", ".config"); } char * xdg_get_state_dir(void) { - return get_dir("XDG_STATE_HOME", NULL, ".local/state"); + return get_dir("XDG_STATE_HOME", ".local/state"); } char * xdg_get_cache_dir(void) { - return get_dir("XDG_CACHE_HOME", NULL, ".cache"); + return get_dir("XDG_CACHE_HOME", ".cache"); } char * xdg_get_runtime_dir(void) { - return get_dir("XDG_RUNTIME_DIR", NULL, NULL); + return get_dir("XDG_RUNTIME_DIR", NULL); } diff --git a/xdgbasedir.h b/xdgbasedir.h index aeaa474..e66ca17 100644 --- a/xdgbasedir.h +++ b/xdgbasedir.h @@ -1,9 +1,7 @@ -// VERSION: 1.1.0 +// VERSION: 1.1.1 /* 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 + * $XDG_DATA_HOME. If that variable doesn't exist or is empty, returns the * default directory such as $HOME/.local/share. * * @@ -23,14 +21,13 @@ #define SMOLLIBS_XDGBASEDIR_H // Append a filename (or config directory name) to a base path, using realloc. -// For instance -// xdg_append_to_path("/home/user/.config", "program/program.conf") -// would result in -// /home/user/.config/program/program.conf -// being returned. +// For example: +// char *c = xdg_get_config_dir(); // pretend it returns "/home/user/.config" +// c = xdg_append_to_path(c, "program/program.conf") +// // c == "/home/user/.config/program/program.conf" // Returns NULL if an error occurs. // The returned string should be freed. -char *xdg_append_to_path(char *base_path, const char *file); +char *xdg_append_to_path(const char *base_path, const char *file); // Find a directory to store data---stored application data that is necessary // to be preserved. -- 2.34.2