1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 03:13:01 +01:00

toxic: Fix get_user_config_dir().

This commit is contained in:
Andreas Schneider 2013-08-11 12:52:56 +02:00
parent b2f4e266d3
commit b5a4cac6e1

View File

@ -28,65 +28,83 @@
#ifdef WIN32 #ifdef WIN32
#include <shlobj.h> #include <shlobj.h>
#include <direct.h> #include <direct.h>
#endif #else /* WIN32 */
#ifdef __APPLE__
#include <unistd.h> #include <unistd.h>
#include <pwd.h> #include <pwd.h>
#endif #endif /* WIN32 */
#include "configdir.h" #include "configdir.h"
/* /**
* Retrieves a correct configuration directory, depending on the OS used, with a trailing slash * @brief Get the users config directory.
*
* This is without a trailing slash.
*
* @return The users config dir or NULL on error.
*/ */
char *get_user_config_dir(void) char *get_user_config_dir(void)
{ {
char *user_config_dir; char *user_config_dir;
#ifdef WIN32 #ifdef WIN32
char appdata[MAX_PATH]; char appdata[MAX_PATH];
HRESULT result = SHGetFolderPath( BOOL ok;
NULL,
CSIDL_APPDATA, ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE);
NULL, if (!ok) {
SHGFP_TYPE_CURRENT, return NULL;
appdata }
)
if (!result) return NULL;
user_config_dir = strdup(appdata); user_config_dir = strdup(appdata);
return user_config_dir; return user_config_dir;
#elif defined __APPLE__ #else /* WIN32 */
struct passwd *pass = getpwuid(getuid()); #ifndef NSS_BUFLEN_PASSWD
if (!pass) return NULL; #define NSS_BUFLEN_PASSWD 4096
char *home = pass->pw_dir; #endif /* NSS_BUFLEN_PASSWD */
user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + 1);
if(user_config_dir) { struct passwd pwd;
strcpy(user_config_dir, home); struct passwd *pwdbuf;
strcat(user_config_dir, "/Library/Application Support"); const char *home;
} char buf[NSS_BUFLEN_PASSWD];
return user_config_dir; size_t len;
int rc;
#else rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
if (rc == 0) {
if (getenv("XDG_CONFIG_HOME")) { home = pwd.pw_dir;
user_config_dir = strdup(getenv("XDG_CONFIG_HOME"));
} else { } else {
user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1); home = getenv("HOME");
if (user_config_dir) { if (home == NULL) {
strcpy(user_config_dir, getenv("HOME")); return NULL;
strcat(user_config_dir, "/.config");
} }
/* env variables can be tainted */
snprintf(buf, sizeof(buf), "%s", home);
home = buf;
} }
return user_config_dir;
#endif # if defined(__APPLE__)
len = strlen(home) + strlen("/Library/Application Support") + 1;
user_config_dir = malloc(len);
if (user_config_dir == NULL) {
return NULL;
}
snprintf(user_config_dir, len, "%s/Library/Application Support", home);
# else /* __APPLE__ */
len = strlen(home) + strlen("/.config") + 1;
user_config_dir = malloc(len);
if (user_config_dir == NULL) {
return NULL;
}
snprintf(user_config_dir, len, "%s/.config", home);
# endif /* __APPLE__ */
return user_config_dir;
#undef NSS_BUFLEN_PASSWD
#endif /* WIN32 */
} }
/* /*