2002-09-03 07:52:59 -04:00
|
|
|
/***************************************************************************
|
2004-10-06 03:50:18 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
1999-12-29 09:20:26 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-02-12 19:40:08 -05:00
|
|
|
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2004-10-06 03:50:18 -04:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
2002-09-03 07:52:59 -04:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
***************************************************************************/
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2001-11-12 05:19:36 -05:00
|
|
|
|
2004-01-29 08:56:45 -05:00
|
|
|
#include <curl/curl.h>
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2004-01-29 08:56:45 -05:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2000-10-09 07:12:34 -04:00
|
|
|
|
2020-01-29 03:23:55 -05:00
|
|
|
static char *GetEnv(const char *variable)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
2016-08-21 07:44:33 -04:00
|
|
|
#if defined(_WIN32_WCE) || defined(CURL_WINDOWS_APP)
|
|
|
|
(void)variable;
|
2004-11-02 05:12:22 -05:00
|
|
|
return NULL;
|
2020-01-29 03:23:55 -05:00
|
|
|
#elif defined(WIN32)
|
|
|
|
/* This uses Windows API instead of C runtime getenv() to get the environment
|
|
|
|
variable since some changes aren't always visible to the latter. #4774 */
|
|
|
|
char *buf = NULL;
|
|
|
|
char *tmp;
|
|
|
|
DWORD bufsize;
|
|
|
|
DWORD rc = 1;
|
|
|
|
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 */
|
|
|
|
}
|
2001-08-06 08:19:26 -04:00
|
|
|
#else
|
1999-12-29 09:20:26 -05:00
|
|
|
char *env = getenv(variable);
|
2007-09-27 14:39:10 -04:00
|
|
|
return (env && env[0])?strdup(env):NULL;
|
1999-12-29 09:20:26 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-08-14 04:25:47 -04:00
|
|
|
char *curl_getenv(const char *v)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
|
|
|
return GetEnv(v);
|
|
|
|
}
|