mirror of
https://github.com/moparisthebest/curl
synced 2024-11-16 06:25:03 -05:00
tool_homedir: Change GetEnv() to use libcurl's curl_getenv()
- Deduplicate GetEnv() code. - On Windows change ultimate call to use Windows API GetEnvironmentVariable() instead of C runtime getenv(). Prior to this change both libcurl and the tool had their own GetEnv which over time diverged. Now the tool's GetEnv is a wrapper around curl_getenv (libcurl API function which is itself a wrapper around libcurl's GetEnv). Furthermore this change fixes a bug in that Windows API GetEnvironmentVariable() is called instead of C runtime getenv() to get the environment variable since some changes aren't always visible to the latter. Reported-by: Christoph M. Becker Fixes https://github.com/curl/curl/issues/4774 Closes https://github.com/curl/curl/pull/4863
This commit is contained in:
parent
39d5621cbd
commit
9dc350b60c
45
lib/getenv.c
45
lib/getenv.c
@ -27,25 +27,48 @@
|
|||||||
|
|
||||||
#include "memdebug.h"
|
#include "memdebug.h"
|
||||||
|
|
||||||
static
|
static char *GetEnv(const char *variable)
|
||||||
char *GetEnv(const char *variable)
|
|
||||||
{
|
{
|
||||||
#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
|
#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
|
||||||
(void)variable;
|
(void)variable;
|
||||||
return NULL;
|
return NULL;
|
||||||
#else
|
#elif defined(WIN32)
|
||||||
#ifdef WIN32
|
/* This uses Windows API instead of C runtime getenv() to get the environment
|
||||||
char env[4096];
|
variable since some changes aren't always visible to the latter. #4774 */
|
||||||
char *temp = getenv(variable);
|
char *buf = NULL;
|
||||||
env[0] = '\0';
|
char *tmp;
|
||||||
if(temp != NULL)
|
DWORD bufsize;
|
||||||
ExpandEnvironmentStringsA(temp, env, sizeof(env));
|
DWORD rc = 1;
|
||||||
return (env[0] != '\0')?strdup(env):NULL;
|
const DWORD max = 32768; /* max env var size from MSCRT source */
|
||||||
|
|
||||||
|
for(;;) {
|
||||||
|
tmp = realloc(buf, rc);
|
||||||
|
if(!tmp) {
|
||||||
|
free(buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf = tmp;
|
||||||
|
bufsize = rc;
|
||||||
|
|
||||||
|
/* It's possible for rc to be 0 if the variable was found but empty.
|
||||||
|
Since getenv doesn't make that distinction we ignore it as well. */
|
||||||
|
rc = GetEnvironmentVariableA(variable, buf, bufsize);
|
||||||
|
if(!rc || rc == bufsize || rc > max) {
|
||||||
|
free(buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* if rc < bufsize then rc is bytes written not including null */
|
||||||
|
if(rc < bufsize)
|
||||||
|
return buf;
|
||||||
|
|
||||||
|
/* else rc is bytes needed, try again */
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
char *env = getenv(variable);
|
char *env = getenv(variable);
|
||||||
return (env && env[0])?strdup(env):NULL;
|
return (env && env[0])?strdup(env):NULL;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *curl_getenv(const char *v)
|
char *curl_getenv(const char *v)
|
||||||
|
@ -25,38 +25,23 @@
|
|||||||
# include <pwd.h>
|
# include <pwd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <curl/mprintf.h>
|
||||||
|
|
||||||
#include "tool_homedir.h"
|
#include "tool_homedir.h"
|
||||||
|
|
||||||
#include "memdebug.h" /* keep this as LAST include */
|
#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;
|
char *dupe, *env;
|
||||||
#ifdef WIN32
|
|
||||||
char buf1[1024], buf2[1024];
|
|
||||||
DWORD rc;
|
|
||||||
|
|
||||||
/* Don't use getenv(); it doesn't find variable added after program was
|
env = curl_getenv(variable);
|
||||||
* started. Don't accept truncated results (i.e. rc >= sizeof(buf1)). */
|
if(!env)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
rc = GetEnvironmentVariableA(variable, buf1, sizeof(buf1));
|
dupe = strdup(env);
|
||||||
if(rc > 0 && rc < sizeof(buf1)) {
|
curl_free(env);
|
||||||
env = buf1;
|
return dupe;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return the home directory of the current user as an allocated string */
|
/* return the home directory of the current user as an allocated string */
|
||||||
@ -64,11 +49,11 @@ char *homedir(void)
|
|||||||
{
|
{
|
||||||
char *home;
|
char *home;
|
||||||
|
|
||||||
home = GetEnv("CURL_HOME", FALSE);
|
home = GetEnv("CURL_HOME");
|
||||||
if(home)
|
if(home)
|
||||||
return home;
|
return home;
|
||||||
|
|
||||||
home = GetEnv("HOME", FALSE);
|
home = GetEnv("HOME");
|
||||||
if(home)
|
if(home)
|
||||||
return home;
|
return home;
|
||||||
|
|
||||||
@ -86,10 +71,18 @@ char *homedir(void)
|
|||||||
}
|
}
|
||||||
#endif /* PWD-stuff */
|
#endif /* PWD-stuff */
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
home = GetEnv("APPDATA", TRUE);
|
home = GetEnv("APPDATA");
|
||||||
if(!home)
|
if(!home) {
|
||||||
home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only
|
char *env = GetEnv("USERPROFILE");
|
||||||
on Win-2K/XP */
|
if(env) {
|
||||||
|
char *path = curl_maprintf("%s\\Application Data", env);
|
||||||
|
if(path) {
|
||||||
|
home = strdup(path);
|
||||||
|
curl_free(path);
|
||||||
|
}
|
||||||
|
free(env);
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif /* WIN32 */
|
#endif /* WIN32 */
|
||||||
return home;
|
return home;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user