aboutsummaryrefslogtreecommitdiff
path: root/src/tool_homedir.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tool_homedir.c')
-rw-r--r--src/tool_homedir.c55
1 files changed, 24 insertions, 31 deletions
diff --git a/src/tool_homedir.c b/src/tool_homedir.c
index 6bc69551e..e4ea97d38 100644
--- a/src/tool_homedir.c
+++ b/src/tool_homedir.c
@@ -25,38 +25,23 @@
# include <pwd.h>
#endif
+#include <curl/mprintf.h>
+
#include "tool_homedir.h"
#include "memdebug.h" /* keep this as LAST include */
-static char *GetEnv(const char *variable, char do_expand)
+static char *GetEnv(const char *variable)
{
- char *env = NULL;
-#ifdef WIN32
- char buf1[1024], buf2[1024];
- DWORD rc;
+ char *dupe, *env;
- /* Don't use getenv(); it doesn't find variable added after program was
- * started. Don't accept truncated results (i.e. rc >= sizeof(buf1)). */
+ env = curl_getenv(variable);
+ if(!env)
+ return NULL;
- rc = GetEnvironmentVariableA(variable, buf1, sizeof(buf1));
- if(rc > 0 && rc < sizeof(buf1)) {
- env = buf1;
- variable = buf1;
- }
- if(do_expand && strchr(variable, '%')) {
- /* buf2 == variable if not expanded */
- rc = ExpandEnvironmentStringsA(variable, buf2, sizeof(buf2));
- if(rc > 0 && rc < sizeof(buf2) &&
- !strchr(buf2, '%')) /* no vars still unexpanded */
- env = buf2;
- }
-#else
- (void)do_expand;
- /* no length control */
- env = getenv(variable);
-#endif
- return (env && env[0]) ? strdup(env) : NULL;
+ dupe = strdup(env);
+ curl_free(env);
+ return dupe;
}
/* return the home directory of the current user as an allocated string */
@@ -64,11 +49,11 @@ char *homedir(void)
{
char *home;
- home = GetEnv("CURL_HOME", FALSE);
+ home = GetEnv("CURL_HOME");
if(home)
return home;
- home = GetEnv("HOME", FALSE);
+ home = GetEnv("HOME");
if(home)
return home;
@@ -86,10 +71,18 @@ char *homedir(void)
}
#endif /* PWD-stuff */
#ifdef WIN32
- home = GetEnv("APPDATA", TRUE);
- if(!home)
- home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only
- on Win-2K/XP */
+ home = GetEnv("APPDATA");
+ if(!home) {
+ char *env = GetEnv("USERPROFILE");
+ if(env) {
+ char *path = curl_maprintf("%s\\Application Data", env);
+ if(path) {
+ home = strdup(path);
+ curl_free(path);
+ }
+ free(env);
+ }
+ }
#endif /* WIN32 */
return home;
}