diff options
-rw-r--r-- | testing/toxic/configdir.c | 108 |
1 files changed, 63 insertions, 45 deletions
diff --git a/testing/toxic/configdir.c b/testing/toxic/configdir.c index 6cbb06bc..18e211ce 100644 --- a/testing/toxic/configdir.c +++ b/testing/toxic/configdir.c | |||
@@ -28,65 +28,83 @@ | |||
28 | #ifdef WIN32 | 28 | #ifdef WIN32 |
29 | #include <shlobj.h> | 29 | #include <shlobj.h> |
30 | #include <direct.h> | 30 | #include <direct.h> |
31 | #endif | 31 | #else /* WIN32 */ |
32 | |||
33 | #ifdef __APPLE__ | ||
34 | #include <unistd.h> | 32 | #include <unistd.h> |
35 | #include <pwd.h> | 33 | #include <pwd.h> |
36 | #endif | 34 | #endif /* WIN32 */ |
37 | 35 | ||
38 | #include "configdir.h" | 36 | #include "configdir.h" |
39 | 37 | ||
40 | /* | 38 | /** |
41 | * Retrieves a correct configuration directory, depending on the OS used, with a trailing slash | 39 | * @brief Get the users config directory. |
40 | * | ||
41 | * This is without a trailing slash. | ||
42 | * | ||
43 | * @return The users config dir or NULL on error. | ||
42 | */ | 44 | */ |
43 | char *get_user_config_dir(void) | 45 | char *get_user_config_dir(void) |
44 | { | 46 | { |
45 | char *user_config_dir; | 47 | char *user_config_dir; |
48 | #ifdef WIN32 | ||
49 | char appdata[MAX_PATH]; | ||
50 | BOOL ok; | ||
46 | 51 | ||
47 | #ifdef WIN32 | 52 | ok = SHGetSpecialFolderPathA(NULL, appdata, CSIDL_PROFILE, TRUE); |
53 | if (!ok) { | ||
54 | return NULL; | ||
55 | } | ||
48 | 56 | ||
49 | char appdata[MAX_PATH]; | 57 | user_config_dir = strdup(appdata); |
50 | HRESULT result = SHGetFolderPath( | 58 | |
51 | NULL, | 59 | return user_config_dir; |
52 | CSIDL_APPDATA, | 60 | |
53 | NULL, | 61 | #else /* WIN32 */ |
54 | SHGFP_TYPE_CURRENT, | 62 | |
55 | appdata | 63 | #ifndef NSS_BUFLEN_PASSWD |
56 | ) | 64 | #define NSS_BUFLEN_PASSWD 4096 |
57 | if (!result) return NULL; | 65 | #endif /* NSS_BUFLEN_PASSWD */ |
58 | 66 | ||
59 | user_config_dir = strdup(appdata); | 67 | struct passwd pwd; |
60 | 68 | struct passwd *pwdbuf; | |
61 | return user_config_dir; | 69 | const char *home; |
62 | 70 | char buf[NSS_BUFLEN_PASSWD]; | |
63 | #elif defined __APPLE__ | 71 | size_t len; |
64 | 72 | int rc; | |
65 | struct passwd *pass = getpwuid(getuid()); | 73 | |
66 | if (!pass) return NULL; | 74 | rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf); |
67 | char *home = pass->pw_dir; | 75 | if (rc == 0) { |
68 | user_config_dir = malloc(strlen(home) + strlen("/Library/Application Support") + 1); | 76 | home = pwd.pw_dir; |
69 | 77 | } else { | |
70 | if(user_config_dir) { | 78 | home = getenv("HOME"); |
71 | strcpy(user_config_dir, home); | 79 | if (home == NULL) { |
72 | strcat(user_config_dir, "/Library/Application Support"); | 80 | return NULL; |
73 | } | 81 | } |
74 | return user_config_dir; | 82 | /* env variables can be tainted */ |
83 | snprintf(buf, sizeof(buf), "%s", home); | ||
84 | home = buf; | ||
85 | } | ||
75 | 86 | ||
76 | #else | 87 | # if defined(__APPLE__) |
88 | len = strlen(home) + strlen("/Library/Application Support") + 1; | ||
89 | user_config_dir = malloc(len); | ||
90 | if (user_config_dir == NULL) { | ||
91 | return NULL; | ||
92 | } | ||
77 | 93 | ||
78 | if (getenv("XDG_CONFIG_HOME")) { | 94 | snprintf(user_config_dir, len, "%s/Library/Application Support", home); |
79 | user_config_dir = strdup(getenv("XDG_CONFIG_HOME")); | 95 | # else /* __APPLE__ */ |
80 | } else { | 96 | len = strlen(home) + strlen("/.config") + 1; |
81 | user_config_dir = malloc(strlen(getenv("HOME")) + strlen("/.config") + 1); | 97 | user_config_dir = malloc(len); |
82 | if (user_config_dir) { | 98 | if (user_config_dir == NULL) { |
83 | strcpy(user_config_dir, getenv("HOME")); | 99 | return NULL; |
84 | strcat(user_config_dir, "/.config"); | ||
85 | } | 100 | } |
86 | } | ||
87 | return user_config_dir; | ||
88 | 101 | ||
89 | #endif | 102 | snprintf(user_config_dir, len, "%s/.config", home); |
103 | # endif /* __APPLE__ */ | ||
104 | |||
105 | return user_config_dir; | ||
106 | #undef NSS_BUFLEN_PASSWD | ||
107 | #endif /* WIN32 */ | ||
90 | } | 108 | } |
91 | 109 | ||
92 | /* | 110 | /* |