2000-02-18 03:38:33 +08:00
|
|
|
/* environ.cc: Cygwin-adopted functions from newlib to manipulate
|
|
|
|
process's environment.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2014-12-02 18:16:03 +08:00
|
|
|
#include <userenv.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdlib.h>
|
2008-02-26 02:32:23 +08:00
|
|
|
#include <wchar.h>
|
2008-03-07 19:24:51 +08:00
|
|
|
#include <wctype.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <ctype.h>
|
2009-03-24 20:18:34 +08:00
|
|
|
#include <locale.h>
|
2002-06-13 09:28:51 +08:00
|
|
|
#include <assert.h>
|
2014-12-02 18:16:03 +08:00
|
|
|
#include <sys/param.h>
|
2000-09-08 10:56:55 +08:00
|
|
|
#include <cygwin/version.h>
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "pinfo.h"
|
2001-03-05 14:28:25 +08:00
|
|
|
#include "perprocess.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "path.h"
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2005-01-13 06:40:46 +08:00
|
|
|
#include "fhandler.h"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "dtable.h"
|
2000-09-03 12:45:52 +08:00
|
|
|
#include "cygheap.h"
|
2005-04-03 21:06:43 +08:00
|
|
|
#include "cygtls.h"
|
2008-03-07 19:24:51 +08:00
|
|
|
#include "tls_pbuf.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "registry.h"
|
|
|
|
#include "environ.h"
|
2002-06-21 13:01:17 +08:00
|
|
|
#include "child_info.h"
|
2011-06-09 03:27:48 +08:00
|
|
|
#include "shared_info.h"
|
2010-05-18 22:30:51 +08:00
|
|
|
#include "ntdll.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2018-06-06 23:45:55 +08:00
|
|
|
/* If this is not NULL, it points to memory allocated by us. */
|
2001-09-06 12:41:59 +08:00
|
|
|
static char **lastenviron;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2011-06-10 01:50:41 +08:00
|
|
|
/* Parse CYGWIN options */
|
|
|
|
|
|
|
|
static NO_COPY bool export_settings = false;
|
|
|
|
|
|
|
|
enum settings
|
|
|
|
{
|
|
|
|
isfunc,
|
2012-02-26 23:47:43 +08:00
|
|
|
setdword,
|
|
|
|
setbool,
|
2011-06-10 01:50:41 +08:00
|
|
|
setbit
|
|
|
|
};
|
|
|
|
|
|
|
|
/* When BUF is:
|
|
|
|
null or empty: disables globbing
|
|
|
|
"ignorecase": enables case-insensitive globbing
|
|
|
|
anything else: enables case-sensitive globbing */
|
|
|
|
static void
|
|
|
|
glob_init (const char *buf)
|
|
|
|
{
|
|
|
|
if (!buf || !*buf)
|
|
|
|
{
|
|
|
|
allow_glob = false;
|
|
|
|
ignore_case_with_glob = false;
|
|
|
|
}
|
|
|
|
else if (ascii_strncasematch (buf, "ignorecase", 10))
|
|
|
|
{
|
|
|
|
allow_glob = true;
|
|
|
|
ignore_case_with_glob = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
allow_glob = true;
|
|
|
|
ignore_case_with_glob = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_proc_retry (const char *buf)
|
|
|
|
{
|
|
|
|
child_info::retry_count = strtoul (buf, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2013-04-24 18:16:13 +08:00
|
|
|
static void
|
|
|
|
set_winsymlinks (const char *buf)
|
|
|
|
{
|
|
|
|
if (!buf || !*buf)
|
|
|
|
allow_winsymlinks = WSYM_lnk;
|
|
|
|
else if (ascii_strncasematch (buf, "lnk", 3))
|
|
|
|
allow_winsymlinks = WSYM_lnk;
|
|
|
|
/* Make sure to try native symlinks only on systems supporting them. */
|
2013-05-23 22:23:01 +08:00
|
|
|
else if (ascii_strncasematch (buf, "native", 6))
|
2015-12-15 23:34:40 +08:00
|
|
|
allow_winsymlinks = ascii_strcasematch (buf + 6, "strict")
|
|
|
|
? WSYM_nativestrict : WSYM_native;
|
2013-04-24 18:16:13 +08:00
|
|
|
}
|
|
|
|
|
2011-06-10 01:50:41 +08:00
|
|
|
/* The structure below is used to set up an array which is used to
|
|
|
|
parse the CYGWIN environment variable or, if enabled, options from
|
|
|
|
the registry. */
|
|
|
|
static struct parse_thing
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
union parse_setting
|
|
|
|
{
|
|
|
|
bool *b;
|
|
|
|
DWORD *x;
|
|
|
|
int *i;
|
|
|
|
void (*func)(const char *);
|
|
|
|
} setting;
|
|
|
|
|
|
|
|
enum settings disposition;
|
|
|
|
char *remember;
|
|
|
|
union parse_values
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
const char *s;
|
|
|
|
} values[2];
|
|
|
|
} known[] NO_COPY =
|
|
|
|
{
|
2012-02-26 23:47:43 +08:00
|
|
|
{"dosfilewarning", {&dos_file_warning}, setbool, NULL, {{false}, {true}}},
|
2011-06-10 01:50:41 +08:00
|
|
|
{"error_start", {func: error_start_init}, isfunc, NULL, {{0}, {0}}},
|
2012-02-26 23:47:43 +08:00
|
|
|
{"export", {&export_settings}, setbool, NULL, {{false}, {true}}},
|
2011-06-10 01:50:41 +08:00
|
|
|
{"glob", {func: glob_init}, isfunc, NULL, {{0}, {s: "normal"}}},
|
2012-04-29 03:49:58 +08:00
|
|
|
{"pipe_byte", {&pipe_byte}, setbool, NULL, {{false}, {true}}},
|
2011-06-10 01:50:41 +08:00
|
|
|
{"proc_retry", {func: set_proc_retry}, isfunc, NULL, {{0}, {5}}},
|
2012-02-26 23:47:43 +08:00
|
|
|
{"reset_com", {&reset_com}, setbool, NULL, {{false}, {true}}},
|
2013-07-27 01:28:00 +08:00
|
|
|
{"wincmdln", {&wincmdln}, setbool, NULL, {{false}, {true}}},
|
2013-04-24 18:16:13 +08:00
|
|
|
{"winsymlinks", {func: set_winsymlinks}, isfunc, NULL, {{0}, {0}}},
|
2012-02-26 23:47:43 +08:00
|
|
|
{NULL, {0}, setdword, 0, {{0}, {0}}}
|
2011-06-10 01:50:41 +08:00
|
|
|
};
|
|
|
|
|
2014-02-10 04:30:24 +08:00
|
|
|
/* Return a possibly-quoted token.
|
|
|
|
Returns NULL when no more tokens available. */
|
|
|
|
static char *
|
|
|
|
strbrk(char *&buf)
|
|
|
|
{
|
|
|
|
buf += strspn(buf, " \t");
|
|
|
|
if (!*buf)
|
|
|
|
return NULL;
|
|
|
|
char *tok = buf;
|
|
|
|
char *sep = buf + strcspn(buf, " \t");
|
|
|
|
char *quotestart = strchr(buf, '"');
|
2014-02-10 09:59:37 +08:00
|
|
|
if (!quotestart || quotestart > sep)
|
2014-02-10 04:30:24 +08:00
|
|
|
{
|
2014-02-10 09:59:37 +08:00
|
|
|
buf = sep + !!*sep; /* Don't point beyond EOS */
|
2014-02-10 04:30:24 +08:00
|
|
|
quotestart = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *quote = quotestart;
|
|
|
|
sep = NULL;
|
|
|
|
while (!sep)
|
|
|
|
{
|
|
|
|
char *clquote = strchr (quote + 1, '"');
|
|
|
|
if (!clquote)
|
|
|
|
sep = strchr (quote, '\0');
|
|
|
|
else if (clquote[-1] != '\\')
|
|
|
|
sep = clquote;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memmove (clquote - 1, clquote, 1 + strchr (clquote, '\0') - clquote);
|
|
|
|
quote = clquote - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf = sep + 1;
|
|
|
|
memmove (quotestart, quotestart + 1, sep - quotestart);
|
|
|
|
sep--;
|
|
|
|
}
|
|
|
|
*sep = '\0';
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-10 01:50:41 +08:00
|
|
|
/* Parse a string of the form "something=stuff somethingelse=more-stuff",
|
|
|
|
silently ignoring unknown "somethings". */
|
|
|
|
static void __stdcall
|
|
|
|
parse_options (const char *inbuf)
|
|
|
|
{
|
|
|
|
int istrue;
|
|
|
|
parse_thing *k;
|
|
|
|
|
|
|
|
if (inbuf == NULL)
|
|
|
|
{
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
char *newbuf = tp.c_get ();
|
|
|
|
newbuf[0] = '\0';
|
|
|
|
for (k = known; k->name != NULL; k++)
|
|
|
|
if (k->remember)
|
|
|
|
{
|
|
|
|
strcat (strcat (newbuf, " "), k->remember);
|
|
|
|
free (k->remember);
|
|
|
|
k->remember = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (export_settings)
|
|
|
|
{
|
|
|
|
debug_printf ("%s", newbuf + 1);
|
|
|
|
setenv ("CYGWIN", newbuf + 1, 1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *buf = strcpy ((char *) alloca (strlen (inbuf) + 1), inbuf);
|
2014-02-10 04:30:24 +08:00
|
|
|
|
|
|
|
while (char *p = strbrk (buf))
|
2011-06-10 01:50:41 +08:00
|
|
|
{
|
|
|
|
char *keyword_here = p;
|
|
|
|
if (!(istrue = !ascii_strncasematch (p, "no", 2)))
|
|
|
|
p += 2;
|
|
|
|
else if (!(istrue = *p != '-'))
|
|
|
|
p++;
|
|
|
|
|
|
|
|
char ch, *eq;
|
|
|
|
if ((eq = strchr (p, '=')) != NULL || (eq = strchr (p, ':')) != NULL)
|
|
|
|
ch = *eq, *eq++ = '\0';
|
|
|
|
else
|
|
|
|
ch = 0;
|
|
|
|
|
|
|
|
for (parse_thing *k = known; k->name != NULL; k++)
|
|
|
|
if (ascii_strcasematch (p, k->name))
|
|
|
|
{
|
|
|
|
switch (k->disposition)
|
|
|
|
{
|
|
|
|
case isfunc:
|
2015-07-29 19:32:29 +08:00
|
|
|
k->setting.func ((!eq || !istrue) ? k->values[istrue].s : eq);
|
2011-06-10 01:50:41 +08:00
|
|
|
debug_printf ("%s (called func)", k->name);
|
|
|
|
break;
|
2012-02-26 23:47:43 +08:00
|
|
|
case setdword:
|
2011-06-10 01:50:41 +08:00
|
|
|
if (!istrue || !eq)
|
|
|
|
*k->setting.x = k->values[istrue].i;
|
|
|
|
else
|
|
|
|
*k->setting.x = strtol (eq, NULL, 0);
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("%s %u", k->name, *k->setting.x);
|
2011-06-10 01:50:41 +08:00
|
|
|
break;
|
2012-02-26 23:47:43 +08:00
|
|
|
case setbool:
|
|
|
|
if (!istrue || !eq)
|
|
|
|
*k->setting.b = k->values[istrue].i;
|
|
|
|
else
|
|
|
|
*k->setting.b = !!strtol (eq, NULL, 0);
|
|
|
|
debug_printf ("%s%s", *k->setting.b ? "" : "no", k->name);
|
|
|
|
break;
|
2011-06-10 01:50:41 +08:00
|
|
|
case setbit:
|
|
|
|
*k->setting.x &= ~k->values[istrue].i;
|
|
|
|
if (istrue || (eq && strtol (eq, NULL, 0)))
|
|
|
|
*k->setting.x |= k->values[istrue].i;
|
|
|
|
debug_printf ("%s %x", k->name, *k->setting.x);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-29 19:32:29 +08:00
|
|
|
int n = 0;
|
2011-06-10 01:50:41 +08:00
|
|
|
if (eq)
|
2015-07-29 19:32:29 +08:00
|
|
|
{
|
|
|
|
*--eq = ch;
|
|
|
|
n = eq - p;
|
|
|
|
}
|
2011-06-10 01:50:41 +08:00
|
|
|
p = strdup (keyword_here);
|
|
|
|
if (n > 0)
|
|
|
|
p[n] = ':';
|
|
|
|
k->remember = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug_printf ("returning");
|
|
|
|
}
|
|
|
|
|
2008-03-12 20:41:50 +08:00
|
|
|
/* Helper functions for the below environment variables which have to
|
|
|
|
be converted Win32<->POSIX. */
|
|
|
|
extern "C" ssize_t env_PATH_to_posix (const void *, void *, size_t);
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
env_plist_to_posix (const void *win32, void *posix, size_t size)
|
|
|
|
{
|
|
|
|
return cygwin_conv_path_list (CCP_WIN_A_TO_POSIX | CCP_RELATIVE, win32,
|
|
|
|
posix, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
env_plist_to_win32 (const void *posix, void *win32, size_t size)
|
|
|
|
{
|
|
|
|
return cygwin_conv_path_list (CCP_POSIX_TO_WIN_A | CCP_RELATIVE, posix,
|
|
|
|
win32, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
env_path_to_posix (const void *win32, void *posix, size_t size)
|
|
|
|
{
|
|
|
|
return cygwin_conv_path (CCP_WIN_A_TO_POSIX | CCP_ABSOLUTE, win32,
|
|
|
|
posix, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
env_path_to_win32 (const void *posix, void *win32, size_t size)
|
|
|
|
{
|
|
|
|
return cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE, posix,
|
|
|
|
win32, size);
|
|
|
|
}
|
2006-08-02 23:11:48 +08:00
|
|
|
|
2000-11-09 04:36:37 +08:00
|
|
|
#define ENVMALLOC \
|
|
|
|
(CYGWIN_VERSION_DLL_MAKE_COMBINED (user_data->api_major, user_data->api_minor) \
|
|
|
|
<= CYGWIN_VERSION_DLL_MALLOC_ENV)
|
|
|
|
|
2002-06-16 13:45:37 +08:00
|
|
|
#define NL(x) x, (sizeof (x) - 1)
|
2000-02-18 03:38:33 +08:00
|
|
|
/* List of names which are converted from dos to unix
|
2000-12-10 08:45:12 +08:00
|
|
|
on the way in and back again on the way out.
|
2001-03-19 05:11:25 +08:00
|
|
|
|
2000-12-10 08:45:12 +08:00
|
|
|
PATH needs to be here because CreateProcess uses it and gdb uses
|
|
|
|
CreateProcess. HOME is here because most shells use it and would be
|
|
|
|
confused by Windows style path names. */
|
2004-05-08 10:55:38 +08:00
|
|
|
static win_env conv_envvars[] =
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2008-03-12 20:41:50 +08:00
|
|
|
{NL ("PATH="), NULL, NULL, env_PATH_to_posix, env_plist_to_win32, true},
|
|
|
|
{NL ("HOME="), NULL, NULL, env_path_to_posix, env_path_to_win32, false},
|
|
|
|
{NL ("LD_LIBRARY_PATH="), NULL, NULL,
|
|
|
|
env_plist_to_posix, env_plist_to_win32, true},
|
|
|
|
{NL ("TMPDIR="), NULL, NULL, env_path_to_posix, env_path_to_win32, false},
|
|
|
|
{NL ("TMP="), NULL, NULL, env_path_to_posix, env_path_to_win32, false},
|
|
|
|
{NL ("TEMP="), NULL, NULL, env_path_to_posix, env_path_to_win32, false},
|
|
|
|
{NULL, 0, NULL, NULL, 0, 0}
|
2000-02-18 03:38:33 +08:00
|
|
|
};
|
|
|
|
|
2011-08-20 02:19:22 +08:00
|
|
|
#define WC ((unsigned char) 1)
|
|
|
|
/* Note: You *must* fill in this array setting the ordinal value of the first
|
|
|
|
character of the above environment variable names to 1.
|
|
|
|
This table is intended to speed up lookup of these variables. */
|
|
|
|
|
2011-08-22 02:45:07 +08:00
|
|
|
static const unsigned char conv_start_chars[256] =
|
2011-08-20 02:19:22 +08:00
|
|
|
{
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/* A B C D E F G */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/* 72 */
|
|
|
|
/* H I J K L M N O */
|
|
|
|
WC, 0, 0, 0, WC, 0, 0, 0,
|
|
|
|
/* 80 */
|
|
|
|
/* P Q R S T U V W */
|
|
|
|
WC, 0, 0, 0, WC, 0, 0, 0,
|
|
|
|
/* 88 */
|
|
|
|
/* x Y Z */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/* 96 */
|
|
|
|
/* a b c d e f g */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
/* 104 */
|
|
|
|
/* h i j k l m n o */
|
|
|
|
WC, 0, 0, 0, WC, 0, 0, 0,
|
|
|
|
/* 112 */
|
|
|
|
/* p q r s t u v w */
|
2011-12-18 07:39:47 +08:00
|
|
|
WC, 0, 0, 0, WC, 0, 0, 0,
|
2011-08-20 02:19:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline char
|
|
|
|
match_first_char (const char *s, unsigned char m)
|
|
|
|
{
|
2017-06-07 03:10:23 +08:00
|
|
|
return conv_start_chars[*(unsigned char *)s] & m;
|
2011-08-20 02:19:22 +08:00
|
|
|
}
|
2000-10-19 08:45:39 +08:00
|
|
|
|
2005-03-30 23:54:28 +08:00
|
|
|
struct win_env&
|
|
|
|
win_env::operator = (struct win_env& x)
|
|
|
|
{
|
|
|
|
name = x.name;
|
|
|
|
namelen = x.namelen;
|
|
|
|
toposix = x.toposix;
|
|
|
|
towin32 = x.towin32;
|
|
|
|
immediate = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
win_env::~win_env ()
|
|
|
|
{
|
|
|
|
if (posix)
|
|
|
|
free (posix);
|
|
|
|
if (native)
|
|
|
|
free (native);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
|
|
|
win_env::add_cache (const char *in_posix, const char *in_native)
|
|
|
|
{
|
|
|
|
posix = (char *) realloc (posix, strlen (in_posix) + 1);
|
|
|
|
strcpy (posix, in_posix);
|
|
|
|
if (in_native)
|
|
|
|
{
|
|
|
|
native = (char *) realloc (native, namelen + 1 + strlen (in_native));
|
2014-10-27 19:33:53 +08:00
|
|
|
stpcpy (stpcpy (native, name), in_native);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-07 19:24:51 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
char *buf = tp.c_get ();
|
2008-03-12 20:41:50 +08:00
|
|
|
towin32 (in_posix, buf, NT_MAX_PATH);
|
2007-12-12 20:12:24 +08:00
|
|
|
native = (char *) realloc (native, namelen + 1 + strlen (buf));
|
2014-10-27 19:33:53 +08:00
|
|
|
stpcpy (stpcpy (native, name), buf);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2005-12-29 11:46:56 +08:00
|
|
|
if (immediate && cygwin_finished_initializing)
|
2005-03-30 23:54:28 +08:00
|
|
|
{
|
2013-05-24 21:32:10 +08:00
|
|
|
wchar_t s[sys_mbstowcs (NULL, 0, native) + 1];
|
|
|
|
sys_mbstowcs (s, sizeof s, native);
|
|
|
|
/* Hack. Relies on affected variables only having ASCII names. */
|
|
|
|
s[namelen - 1] = L'\0';
|
|
|
|
SetEnvironmentVariableW (s, s + namelen);
|
2005-03-30 23:54:28 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
debug_printf ("posix %s", posix);
|
|
|
|
debug_printf ("native %s", native);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check for a "special" environment variable name. *env is the pointer
|
2002-06-13 09:28:51 +08:00
|
|
|
to the beginning of the environment variable name. *in_posix is any
|
|
|
|
known posix value for the environment variable. Returns a pointer to
|
|
|
|
the appropriate conversion structure. */
|
2013-01-21 12:34:52 +08:00
|
|
|
win_env * __reg3
|
2005-03-30 23:54:28 +08:00
|
|
|
getwinenv (const char *env, const char *in_posix, win_env *temp)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2011-08-20 02:19:22 +08:00
|
|
|
if (!match_first_char (env, WC))
|
2000-10-19 08:45:39 +08:00
|
|
|
return NULL;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
for (int i = 0; conv_envvars[i].name != NULL; i++)
|
2001-04-19 05:10:15 +08:00
|
|
|
if (strncmp (env, conv_envvars[i].name, conv_envvars[i].namelen) == 0)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-03-30 23:54:28 +08:00
|
|
|
win_env *we = conv_envvars + i;
|
2000-02-18 03:38:33 +08:00
|
|
|
const char *val;
|
2002-06-13 09:28:51 +08:00
|
|
|
if (!cur_environ () || !(val = in_posix ?: getenv (we->name)))
|
2000-02-18 03:38:33 +08:00
|
|
|
debug_printf ("can't set native for %s since no environ yet",
|
|
|
|
we->name);
|
2011-06-10 03:08:22 +08:00
|
|
|
else if (!we->posix || strcmp (val, we->posix) != 0)
|
2005-03-30 23:54:28 +08:00
|
|
|
{
|
|
|
|
if (temp)
|
|
|
|
{
|
|
|
|
*temp = *we;
|
|
|
|
we = temp;
|
|
|
|
}
|
|
|
|
we->add_cache (val);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
return we;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert windows path specs to POSIX, if appropriate.
|
|
|
|
*/
|
2011-08-20 02:19:22 +08:00
|
|
|
inline static void
|
|
|
|
posify_maybe (char **here, const char *value, char *outenv)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
char *src = *here;
|
|
|
|
win_env *conv;
|
|
|
|
|
|
|
|
if (!(conv = getwinenv (src)))
|
|
|
|
return;
|
|
|
|
|
2000-10-18 12:53:37 +08:00
|
|
|
int len = strcspn (src, "=") + 1;
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
/* Turn all the items from c:<foo>;<bar> into their
|
|
|
|
mounted equivalents - if there is one. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
memcpy (outenv, src, len);
|
2006-08-02 23:11:48 +08:00
|
|
|
char *newvalue = outenv + len;
|
2017-07-19 16:30:47 +08:00
|
|
|
if (!conv->toposix (value, newvalue, NT_MAX_PATH - len) || errno != EIDRM)
|
2006-08-02 23:11:48 +08:00
|
|
|
conv->add_cache (newvalue, *value != '/' ? value : NULL);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The conversion routine removed elements from a path list so we have
|
|
|
|
to recalculate the windows path to remove elements there, too. */
|
2014-10-27 19:33:53 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
char *cleanvalue = tp.c_get ();
|
|
|
|
conv->towin32 (newvalue, cleanvalue, NT_MAX_PATH);
|
2006-08-02 23:11:48 +08:00
|
|
|
conv->add_cache (newvalue, cleanvalue);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
debug_printf ("env var converted to %s", outenv);
|
2007-12-12 20:12:24 +08:00
|
|
|
*here = strdup (outenv);
|
2000-09-03 12:16:35 +08:00
|
|
|
free (src);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2006-04-22 03:40:48 +08:00
|
|
|
/* Returns pointer to value associated with name, if any, else NULL.
|
|
|
|
Sets offset to be the offset of the name/value combination in the
|
|
|
|
environment array, for use by setenv(3) and unsetenv(3).
|
|
|
|
Explicitly removes '=' in argument name. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2013-07-10 20:15:32 +08:00
|
|
|
static char *
|
2000-02-18 03:38:33 +08:00
|
|
|
my_findenv (const char *name, int *offset)
|
|
|
|
{
|
|
|
|
register int len;
|
|
|
|
register char **p;
|
|
|
|
const char *c;
|
|
|
|
|
2018-06-06 23:45:55 +08:00
|
|
|
if (cur_environ () == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
c = name;
|
|
|
|
len = 0;
|
|
|
|
while (*c && *c != '=')
|
|
|
|
{
|
|
|
|
c++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
for (p = cur_environ (); *p; ++p)
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!strncmp (*p, name, len))
|
|
|
|
if (*(c = *p + len) == '=')
|
|
|
|
{
|
2000-08-03 00:28:18 +08:00
|
|
|
*offset = p - cur_environ ();
|
2000-02-18 03:38:33 +08:00
|
|
|
return (char *) (++c);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-05-28 23:50:14 +08:00
|
|
|
|
2006-04-22 03:19:31 +08:00
|
|
|
/* Primitive getenv before the environment is built. */
|
2006-04-22 01:21:41 +08:00
|
|
|
|
2013-07-10 20:15:32 +08:00
|
|
|
static char *
|
2006-04-22 05:34:38 +08:00
|
|
|
getearly (const char * name, int *)
|
2006-04-22 01:21:41 +08:00
|
|
|
{
|
2006-04-22 05:34:38 +08:00
|
|
|
char *ret;
|
2006-04-22 03:19:31 +08:00
|
|
|
char **ptr;
|
2006-04-22 05:34:38 +08:00
|
|
|
int len;
|
2006-04-22 03:19:31 +08:00
|
|
|
|
2006-04-22 05:34:38 +08:00
|
|
|
if (spawn_info && (ptr = spawn_info->moreinfo->envp))
|
|
|
|
{
|
|
|
|
len = strlen (name);
|
|
|
|
for (; *ptr; ptr++)
|
2006-04-25 00:42:07 +08:00
|
|
|
if (strncasematch (name, *ptr, len) && (*ptr)[len] == '=')
|
2006-04-22 05:34:38 +08:00
|
|
|
return *ptr + len + 1;
|
|
|
|
}
|
2008-02-26 02:32:23 +08:00
|
|
|
else if ((len = GetEnvironmentVariableA (name, NULL, 0))
|
2007-11-27 05:30:49 +08:00
|
|
|
&& (ret = (char *) cmalloc_abort (HEAP_2_STR, len))
|
2008-02-26 02:32:23 +08:00
|
|
|
&& GetEnvironmentVariableA (name, ret, len))
|
2006-04-22 05:34:38 +08:00
|
|
|
return ret;
|
2006-04-22 01:21:41 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-10 20:15:32 +08:00
|
|
|
static char * (*findenv_func)(const char *, int *) = getearly;
|
2006-04-22 01:21:41 +08:00
|
|
|
|
2006-04-22 03:19:31 +08:00
|
|
|
/* Returns ptr to value associated with name, if any, else NULL. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-02-21 11:13:24 +08:00
|
|
|
extern "C" char *
|
2000-02-18 03:38:33 +08:00
|
|
|
getenv (const char *name)
|
|
|
|
{
|
|
|
|
int offset;
|
2006-04-22 01:21:41 +08:00
|
|
|
return findenv_func (name, &offset);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2009-02-14 01:49:37 +08:00
|
|
|
/* This function is required so that newlib uses the same environment
|
|
|
|
as Cygwin. */
|
|
|
|
extern "C" char *
|
|
|
|
_getenv_r (struct _reent *, const char *name)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
return findenv_func (name, &offset);
|
|
|
|
}
|
|
|
|
|
2019-02-19 13:06:11 +08:00
|
|
|
/* Like getenv, but returns NULL if effective and real UID/GIDs do not match */
|
|
|
|
extern "C" char *
|
|
|
|
secure_getenv (const char *name)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
if (cygheap->user.issetuid ())
|
|
|
|
return NULL;
|
|
|
|
return findenv_func (name, &offset);
|
|
|
|
}
|
|
|
|
|
2018-06-06 23:45:55 +08:00
|
|
|
/* Return number of environment entries, including terminating NULL. */
|
2002-06-12 13:13:54 +08:00
|
|
|
static int __stdcall
|
|
|
|
envsize (const char * const *in_envp)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
|
|
|
const char * const *envp;
|
2018-06-06 23:45:55 +08:00
|
|
|
|
|
|
|
if (in_envp == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
for (envp = in_envp; *envp; envp++)
|
2002-06-12 13:13:54 +08:00
|
|
|
continue;
|
2018-06-06 23:45:55 +08:00
|
|
|
return 1 + envp - in_envp;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2000-07-26 09:44:16 +08:00
|
|
|
/* Takes similar arguments to setenv except that overwrite is
|
|
|
|
either -1, 0, or 1. 0 or 1 signify that the function should
|
|
|
|
perform similarly to setenv. Otherwise putenv is assumed. */
|
|
|
|
static int __stdcall
|
|
|
|
_addenv (const char *name, const char *value, int overwrite)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-07-26 09:44:16 +08:00
|
|
|
int issetenv = overwrite >= 0;
|
|
|
|
int offset;
|
|
|
|
char *p;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-26 09:44:16 +08:00
|
|
|
unsigned int valuelen = strlen (value);
|
|
|
|
if ((p = my_findenv (name, &offset)))
|
|
|
|
{ /* Already exists. */
|
|
|
|
if (!overwrite) /* Ok to overwrite? */
|
|
|
|
return 0; /* No. Wanted to add new value. FIXME: Right return value? */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-26 09:44:16 +08:00
|
|
|
/* We've found the offset into environ. If this is a setenv call and
|
|
|
|
there is room in the current environment entry then just overwrite it.
|
|
|
|
Otherwise handle this case below. */
|
|
|
|
if (issetenv && strlen (p) >= valuelen)
|
|
|
|
{
|
|
|
|
strcpy (p, value);
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2000-07-26 09:44:16 +08:00
|
|
|
{ /* Create new slot. */
|
2000-09-03 12:16:35 +08:00
|
|
|
int sz = envsize (cur_environ ());
|
2018-06-06 23:45:54 +08:00
|
|
|
|
2018-06-06 23:45:57 +08:00
|
|
|
/* If sz == 0, we need two new slots, one for the terminating NULL. */
|
|
|
|
int newsz = sz == 0 ? 2 : sz + 1;
|
|
|
|
int allocsz = newsz * sizeof (char *);
|
2000-07-26 09:44:16 +08:00
|
|
|
|
2018-06-06 23:45:57 +08:00
|
|
|
offset = newsz - 2;
|
2000-07-26 09:44:16 +08:00
|
|
|
|
2018-06-06 23:45:54 +08:00
|
|
|
/* Allocate space for additional element. */
|
2001-10-03 11:49:26 +08:00
|
|
|
if (cur_environ () == lastenviron)
|
2018-06-06 23:45:55 +08:00
|
|
|
lastenviron = __cygwin_environ = (char **) realloc (lastenviron,
|
2000-09-03 12:16:35 +08:00
|
|
|
allocsz);
|
2018-06-06 23:45:55 +08:00
|
|
|
else if ((lastenviron = (char **) realloc (lastenviron, allocsz)) != NULL)
|
|
|
|
__cygwin_environ = (char **) memcpy (lastenviron, __cygwin_environ,
|
|
|
|
sz * sizeof (char *));
|
|
|
|
if (!lastenviron)
|
2000-10-23 11:35:50 +08:00
|
|
|
{
|
2000-11-08 03:00:02 +08:00
|
|
|
#ifdef DEBUGGING
|
2000-10-23 11:35:50 +08:00
|
|
|
try_to_debug ();
|
|
|
|
#endif
|
2001-10-03 11:49:26 +08:00
|
|
|
return -1; /* Oops. No more memory. */
|
2000-10-23 11:35:50 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-26 09:44:16 +08:00
|
|
|
__cygwin_environ[offset + 1] = NULL; /* NULL terminate. */
|
|
|
|
update_envptrs (); /* Update any local copies of 'environ'. */
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-26 09:44:16 +08:00
|
|
|
char *envhere;
|
|
|
|
if (!issetenv)
|
2000-11-09 04:36:37 +08:00
|
|
|
/* Not setenv. Just overwrite existing. */
|
|
|
|
envhere = cur_environ ()[offset] = (char *) (ENVMALLOC ? strdup (name) : name);
|
2000-07-26 09:44:16 +08:00
|
|
|
else
|
|
|
|
{ /* setenv */
|
|
|
|
/* Look for an '=' in the name and ignore anything after that if found. */
|
|
|
|
for (p = (char *) name; *p && *p != '='; p++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int namelen = p - name; /* Length of name. */
|
|
|
|
/* Allocate enough space for name + '=' + value + '\0' */
|
2000-08-03 00:28:18 +08:00
|
|
|
envhere = cur_environ ()[offset] = (char *) malloc (namelen + valuelen + 2);
|
2000-07-26 09:44:16 +08:00
|
|
|
if (!envhere)
|
|
|
|
return -1; /* Oops. No more memory. */
|
|
|
|
|
|
|
|
/* Put name '=' value into current slot. */
|
2019-07-16 03:02:00 +08:00
|
|
|
memcpy (envhere, name, namelen);
|
2000-07-26 09:44:16 +08:00
|
|
|
envhere[namelen] = '=';
|
|
|
|
strcpy (envhere + namelen + 1, value);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-26 09:44:16 +08:00
|
|
|
/* Update cygwin's cache, if appropriate */
|
2000-02-18 03:38:33 +08:00
|
|
|
win_env *spenv;
|
2000-07-26 09:44:16 +08:00
|
|
|
if ((spenv = getwinenv (envhere)))
|
2000-02-18 03:38:33 +08:00
|
|
|
spenv->add_cache (value);
|
2011-06-10 01:50:41 +08:00
|
|
|
if (strcmp (name, "CYGWIN") == 0)
|
|
|
|
parse_options (value);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-22 03:19:31 +08:00
|
|
|
/* Set an environment variable */
|
2000-07-26 09:44:16 +08:00
|
|
|
extern "C" int
|
2004-11-26 12:21:47 +08:00
|
|
|
putenv (char *str)
|
2000-07-26 09:44:16 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2000-07-27 02:51:14 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (*str)
|
|
|
|
{
|
|
|
|
char *eq = strchr (str, '=');
|
|
|
|
if (eq)
|
|
|
|
return _addenv (str, eq + 1, -1);
|
2000-07-26 09:44:16 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
/* Remove str from the environment. */
|
|
|
|
unsetenv (str);
|
|
|
|
}
|
|
|
|
return 0;
|
2005-07-03 10:40:30 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2000-07-26 09:44:16 +08:00
|
|
|
}
|
|
|
|
|
2006-04-22 03:19:31 +08:00
|
|
|
/* Set the value of the environment variable "name" to be
|
2000-12-10 08:45:12 +08:00
|
|
|
"value". If overwrite is set, replace any current value. */
|
2000-07-26 09:44:16 +08:00
|
|
|
extern "C" int
|
|
|
|
setenv (const char *name, const char *value, int overwrite)
|
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2009-11-17 04:05:49 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (!name || !*name || strchr (name, '='))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
__leave;
|
|
|
|
}
|
|
|
|
return _addenv (name, value, !!overwrite);
|
2009-11-17 04:05:49 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2000-07-26 09:44:16 +08:00
|
|
|
}
|
|
|
|
|
2006-04-22 03:19:31 +08:00
|
|
|
/* Delete environment variable "name". */
|
2005-12-06 06:30:03 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
unsetenv (const char *name)
|
|
|
|
{
|
2000-07-18 03:18:21 +08:00
|
|
|
register char **e;
|
2000-02-18 03:38:33 +08:00
|
|
|
int offset;
|
2014-08-22 17:21:33 +08:00
|
|
|
|
|
|
|
__try
|
2005-12-06 06:30:03 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (!name || *name == '\0' || strchr (name, '='))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
__leave;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
while (my_findenv (name, &offset)) /* if set multiple times */
|
|
|
|
/* Move up the rest of the array */
|
|
|
|
for (e = cur_environ () + offset; ; e++)
|
|
|
|
if (!(*e = *(e + 1)))
|
|
|
|
break;
|
2005-12-06 06:30:03 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 23:45:56 +08:00
|
|
|
/* Clear the environment. */
|
|
|
|
extern "C" int
|
|
|
|
clearenv (void)
|
|
|
|
{
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
if (cur_environ () == lastenviron)
|
|
|
|
{
|
|
|
|
free (lastenviron);
|
|
|
|
lastenviron = NULL;
|
|
|
|
}
|
|
|
|
__cygwin_environ = NULL;
|
|
|
|
update_envptrs ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__except (EFAULT) {}
|
|
|
|
__endtry
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-11-11 18:51:03 +08:00
|
|
|
/* Minimal list of Windows vars which must be converted to uppercase.
|
|
|
|
Either for POSIX compatibility of for backward compatibility with
|
|
|
|
existing applications. */
|
|
|
|
static struct renv {
|
2008-11-27 01:21:04 +08:00
|
|
|
const char *name;
|
|
|
|
const size_t namelen;
|
2008-11-11 18:51:03 +08:00
|
|
|
} renv_arr[] = {
|
2015-01-14 18:40:03 +08:00
|
|
|
{ NL("COMMONPROGRAMFILES=") }, // 0
|
2008-11-27 01:21:04 +08:00
|
|
|
{ NL("COMSPEC=") },
|
2015-01-14 18:40:03 +08:00
|
|
|
{ NL("PATH=") }, // 2
|
2008-11-27 01:21:04 +08:00
|
|
|
{ NL("PROGRAMFILES=") },
|
2015-01-14 18:40:03 +08:00
|
|
|
{ NL("SYSTEMDRIVE=") }, // 4
|
2008-11-27 01:21:04 +08:00
|
|
|
{ NL("SYSTEMROOT=") },
|
2015-01-14 18:40:03 +08:00
|
|
|
{ NL("TEMP=") }, // 6
|
2008-11-27 01:21:04 +08:00
|
|
|
{ NL("TMP=") },
|
2015-01-14 18:40:03 +08:00
|
|
|
{ NL("WINDIR=") } // 8
|
2008-11-11 18:51:03 +08:00
|
|
|
};
|
|
|
|
#define RENV_SIZE (sizeof (renv_arr) / sizeof (renv_arr[0]))
|
2011-08-20 02:19:22 +08:00
|
|
|
|
2008-11-11 18:51:03 +08:00
|
|
|
/* Set of first characters of the above list of variables. */
|
2015-01-14 18:40:03 +08:00
|
|
|
static const char idx_arr[] = "CPSTW";
|
2008-11-11 18:51:03 +08:00
|
|
|
/* Index into renv_arr at which the variables with this specific character
|
|
|
|
starts. */
|
2015-01-14 18:40:03 +08:00
|
|
|
static const int start_at[] = { 0, 2, 4, 6, 8 };
|
2008-11-11 18:51:03 +08:00
|
|
|
|
2011-06-10 23:06:13 +08:00
|
|
|
/* Turn environment variable part of a=b string into uppercase - for some
|
|
|
|
environment variables only. */
|
2008-11-11 18:51:03 +08:00
|
|
|
static __inline__ void
|
2008-11-27 01:21:04 +08:00
|
|
|
ucenv (char *p, const char *eq)
|
2008-11-11 18:51:03 +08:00
|
|
|
{
|
2011-06-10 23:06:13 +08:00
|
|
|
/* Hopefully as quickly as possible - only upper case specific set of important
|
|
|
|
Windows variables. */
|
|
|
|
char first = cyg_toupper (*p);
|
|
|
|
const char *idx = strchr (idx_arr, first);
|
|
|
|
if (idx)
|
|
|
|
for (size_t i = start_at[idx - idx_arr];
|
|
|
|
i < RENV_SIZE && renv_arr[i].name[0] == first;
|
|
|
|
++i)
|
|
|
|
if (strncasematch (p, renv_arr[i].name, renv_arr[i].namelen))
|
|
|
|
{
|
|
|
|
strncpy (p, renv_arr[i].name, renv_arr[i].namelen);
|
|
|
|
break;
|
|
|
|
}
|
2008-11-11 18:51:03 +08:00
|
|
|
}
|
|
|
|
|
2014-12-08 19:21:14 +08:00
|
|
|
/* Initialize the environ array. Look for the CYGWIN environment variable and
|
|
|
|
set appropriate options from it. */
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
2000-10-17 07:55:58 +08:00
|
|
|
environ_init (char **envp, int envc)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2017-01-10 23:03:08 +08:00
|
|
|
PWCHAR rawenv;
|
2000-07-26 09:44:16 +08:00
|
|
|
char *p;
|
2000-09-26 00:36:12 +08:00
|
|
|
bool envp_passed_in;
|
2005-09-16 23:56:06 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
__try
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
if (!envp)
|
|
|
|
envp_passed_in = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
envc++;
|
|
|
|
envc *= sizeof (char *);
|
|
|
|
char **newenv = (char **) malloc (envc);
|
|
|
|
memcpy (newenv, envp, envc);
|
|
|
|
cfree (envp);
|
|
|
|
|
|
|
|
/* Older applications relied on the fact that cygwin malloced elements of the
|
|
|
|
environment list. */
|
|
|
|
envp = newenv;
|
|
|
|
if (ENVMALLOC)
|
|
|
|
for (char **e = newenv; *e; e++)
|
|
|
|
{
|
|
|
|
char *p = *e;
|
|
|
|
*e = strdup (p);
|
|
|
|
cfree (p);
|
|
|
|
}
|
|
|
|
envp_passed_in = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
rawenv = GetEnvironmentStringsW ();
|
|
|
|
if (!rawenv)
|
|
|
|
{
|
|
|
|
system_printf ("GetEnvironmentStrings returned NULL, %E");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debug_printf ("GetEnvironmentStrings returned %p", rawenv);
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2017-06-07 03:10:23 +08:00
|
|
|
lastenviron = envp = win32env_to_cygenv (rawenv, true);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
FreeEnvironmentStringsW (rawenv);
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2014-08-22 17:21:33 +08:00
|
|
|
out:
|
|
|
|
findenv_func = (char * (*)(const char*, int*)) my_findenv;
|
|
|
|
__cygwin_environ = envp;
|
|
|
|
update_envptrs ();
|
|
|
|
if (envp_passed_in)
|
|
|
|
{
|
|
|
|
p = getenv ("CYGWIN");
|
|
|
|
if (p)
|
|
|
|
parse_options (p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
__except (NO_ERROR)
|
2000-09-26 00:36:12 +08:00
|
|
|
{
|
2014-08-22 17:21:33 +08:00
|
|
|
api_fatal ("internal error reading the windows environment "
|
|
|
|
"- too many environment variables?");
|
2000-09-26 00:36:12 +08:00
|
|
|
}
|
2014-08-22 17:21:33 +08:00
|
|
|
__endtry
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2019-11-07 00:29:29 +08:00
|
|
|
int sawTERM = 0;
|
2017-01-10 23:03:08 +08:00
|
|
|
|
|
|
|
char ** __reg2
|
|
|
|
win32env_to_cygenv (PWCHAR rawenv, bool posify)
|
|
|
|
{
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
char **envp;
|
|
|
|
int envc;
|
|
|
|
char *newp;
|
|
|
|
int i;
|
2019-03-31 23:47:46 +08:00
|
|
|
const char cygterm[] = "TERM=cygwin";
|
|
|
|
const char xterm[] = "TERM=xterm-256color";
|
2017-01-10 23:03:08 +08:00
|
|
|
char *tmpbuf = tp.t_get ();
|
|
|
|
PWCHAR w;
|
|
|
|
|
|
|
|
/* Allocate space for environment + trailing NULL + CYGWIN env. */
|
|
|
|
envp = (char **) malloc ((4 + (envc = 100)) * sizeof (char *));
|
|
|
|
|
|
|
|
/* Current directory information is recorded as variables of the
|
|
|
|
form "=X:=X:\foo\bar; these must be changed into something legal
|
|
|
|
(we could just ignore them but maybe an application will
|
|
|
|
eventually want to use them). */
|
|
|
|
for (i = 0, w = rawenv; *w != L'\0'; w = wcschr (w, L'\0') + 1, i++)
|
|
|
|
{
|
|
|
|
sys_wcstombs_alloc_no_path (&newp, HEAP_NOTHEAP, w);
|
|
|
|
if (i >= envc)
|
|
|
|
envp = (char **) realloc (envp, (4 + (envc += 100)) * sizeof (char *));
|
|
|
|
envp[i] = newp;
|
|
|
|
if (*newp == '=')
|
|
|
|
*newp = '!';
|
|
|
|
char *eq = strchrnul (newp, '=');
|
|
|
|
ucenv (newp, eq); /* uppercase env vars which need it */
|
|
|
|
if (*newp == 'T' && strncmp (newp, "TERM=", 5) == 0)
|
|
|
|
sawTERM = 1;
|
|
|
|
else if (*newp == 'C' && strncmp (newp, "CYGWIN=", 7) == 0)
|
|
|
|
parse_options (newp + 7);
|
|
|
|
if (*eq && posify)
|
|
|
|
posify_maybe (envp + i, *++eq ? eq : --eq, tmpbuf);
|
|
|
|
debug_printf ("%p: %s", envp[i], envp[i]);
|
|
|
|
}
|
|
|
|
|
2019-03-31 23:47:46 +08:00
|
|
|
/* If console has 24 bit color capability, TERM=xterm-256color,
|
|
|
|
otherwise, TERM=cygwin */
|
2017-01-10 23:03:08 +08:00
|
|
|
if (!sawTERM)
|
2019-03-31 23:47:46 +08:00
|
|
|
envp[i++] = strdup (wincap.has_con_24bit_colors () ? xterm : cygterm);
|
2017-01-10 23:03:08 +08:00
|
|
|
|
|
|
|
envp[i] = NULL;
|
|
|
|
return envp;
|
|
|
|
}
|
|
|
|
|
2000-12-10 08:45:12 +08:00
|
|
|
/* Function called by qsort to sort environment strings. */
|
2000-02-18 03:38:33 +08:00
|
|
|
static int
|
|
|
|
env_sort (const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const char **p = (const char **) a;
|
|
|
|
const char **q = (const char **) b;
|
|
|
|
|
|
|
|
return strcmp (*p, *q);
|
|
|
|
}
|
|
|
|
|
2013-01-21 12:34:52 +08:00
|
|
|
char * __reg3
|
2002-06-29 10:36:08 +08:00
|
|
|
getwinenveq (const char *name, size_t namelen, int x)
|
|
|
|
{
|
2008-02-26 02:32:23 +08:00
|
|
|
WCHAR name0[namelen - 1];
|
|
|
|
WCHAR valbuf[32768]; /* Max size of an env.var including trailing '\0'. */
|
|
|
|
|
|
|
|
name0[sys_mbstowcs (name0, sizeof name0, name, namelen - 1)] = L'\0';
|
|
|
|
int totlen = GetEnvironmentVariableW (name0, valbuf, 32768);
|
2002-06-29 10:36:08 +08:00
|
|
|
if (totlen > 0)
|
|
|
|
{
|
Do not treat the command line or environment like paths
* dcrt0.cc (dll_crt0_1), environ.cc (environ_init, getwinenveq,
build_env), strfuncs.cc (sys_wcstombs, sys_wcstombs_alloc),
wchar.c (sys_wcstombs, sys_wcstombs_alloc): avoid mis-conversions
of text that does not, actually, refer to a path or file name
Detailed explanation:
Our WCS -> UTF conversion handles the private Unicode page specially
to allow for otherwise invalid file names. However, this handling makes
no sense for command-lines, nor environment variables, which we would
rather convert verbatim.
As a stop-gap solution, let's just introduce a version of the
sys_wcstombs() function that specifically excludes that file name
conversion magic.
The proper solution is to change sys_wcstombs() to assume that it is not
a path that wants to be converted, and introduce sys_wcstombs_path()
that does, but that is a bigger task which we leave for another patch.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2015-12-17 23:22:49 +08:00
|
|
|
totlen = sys_wcstombs_no_path (NULL, 0, valbuf) + 1;
|
2002-06-29 10:36:08 +08:00
|
|
|
if (x == HEAP_1_STR)
|
|
|
|
totlen += namelen;
|
|
|
|
else
|
|
|
|
namelen = 0;
|
2007-11-27 05:30:49 +08:00
|
|
|
char *p = (char *) cmalloc_abort ((cygheap_types) x, totlen);
|
2002-06-29 10:36:08 +08:00
|
|
|
if (namelen)
|
|
|
|
strcpy (p, name);
|
Do not treat the command line or environment like paths
* dcrt0.cc (dll_crt0_1), environ.cc (environ_init, getwinenveq,
build_env), strfuncs.cc (sys_wcstombs, sys_wcstombs_alloc),
wchar.c (sys_wcstombs, sys_wcstombs_alloc): avoid mis-conversions
of text that does not, actually, refer to a path or file name
Detailed explanation:
Our WCS -> UTF conversion handles the private Unicode page specially
to allow for otherwise invalid file names. However, this handling makes
no sense for command-lines, nor environment variables, which we would
rather convert verbatim.
As a stop-gap solution, let's just introduce a version of the
sys_wcstombs() function that specifically excludes that file name
conversion magic.
The proper solution is to change sys_wcstombs() to assume that it is not
a path that wants to be converted, and introduce sys_wcstombs_path()
that does, but that is a bigger task which we leave for another patch.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2015-12-17 23:22:49 +08:00
|
|
|
sys_wcstombs_no_path (p + namelen, totlen, valbuf);
|
2008-02-26 02:32:23 +08:00
|
|
|
debug_printf ("using value from GetEnvironmentVariable for '%W'", name0);
|
|
|
|
return p;
|
2002-06-29 10:36:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
debug_printf ("warning: %s not present in environment", name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-12 13:13:54 +08:00
|
|
|
struct spenv
|
|
|
|
{
|
|
|
|
const char *name;
|
2002-06-16 13:45:37 +08:00
|
|
|
size_t namelen;
|
2005-05-28 02:26:19 +08:00
|
|
|
bool force_into_environment; /* If true, always add to env if missing */
|
2005-05-01 01:07:05 +08:00
|
|
|
bool add_if_exists; /* if true, retrieve value from cache */
|
2002-06-29 10:36:08 +08:00
|
|
|
const char * (cygheap_user::*from_cygheap) (const char *, size_t);
|
2004-10-08 05:28:57 +08:00
|
|
|
|
2013-01-21 12:34:52 +08:00
|
|
|
char __reg3 *retrieve (bool, const char * const = NULL);
|
2002-06-12 13:13:54 +08:00
|
|
|
};
|
|
|
|
|
2002-06-28 04:44:27 +08:00
|
|
|
#define env_dontadd almost_null
|
2002-06-17 07:34:43 +08:00
|
|
|
|
2001-05-11 11:27:22 +08:00
|
|
|
/* Keep this list in upper case and sorted */
|
2002-06-12 13:13:54 +08:00
|
|
|
static NO_COPY spenv spenvs[] =
|
|
|
|
{
|
2005-05-28 02:26:19 +08:00
|
|
|
#ifdef DEBUGGING
|
2005-06-08 23:42:58 +08:00
|
|
|
{NL ("CYGWIN_DEBUG="), false, true, NULL},
|
2005-05-28 02:26:19 +08:00
|
|
|
#endif
|
2005-03-30 23:54:28 +08:00
|
|
|
{NL ("HOMEDRIVE="), false, false, &cygheap_user::env_homedrive},
|
|
|
|
{NL ("HOMEPATH="), false, false, &cygheap_user::env_homepath},
|
|
|
|
{NL ("LOGONSERVER="), false, false, &cygheap_user::env_logsrv},
|
|
|
|
{NL ("PATH="), false, true, NULL},
|
2005-11-09 00:32:09 +08:00
|
|
|
{NL ("SYSTEMDRIVE="), false, true, NULL},
|
2005-03-31 06:06:32 +08:00
|
|
|
{NL ("SYSTEMROOT="), true, true, &cygheap_user::env_systemroot},
|
2005-03-30 23:54:28 +08:00
|
|
|
{NL ("USERDOMAIN="), false, false, &cygheap_user::env_domain},
|
|
|
|
{NL ("USERNAME="), false, false, &cygheap_user::env_name},
|
2005-12-05 05:47:03 +08:00
|
|
|
{NL ("USERPROFILE="), false, false, &cygheap_user::env_userprofile},
|
2006-01-09 04:10:51 +08:00
|
|
|
{NL ("WINDIR="), true, true, &cygheap_user::env_systemroot}
|
2002-06-12 13:13:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
char *
|
2005-03-30 23:54:28 +08:00
|
|
|
spenv::retrieve (bool no_envblock, const char *const env)
|
2002-06-12 13:13:54 +08:00
|
|
|
{
|
2008-02-01 20:37:51 +08:00
|
|
|
if (env && !ascii_strncasematch (env, name, namelen))
|
2002-06-12 13:13:54 +08:00
|
|
|
return NULL;
|
2002-06-19 23:27:27 +08:00
|
|
|
|
|
|
|
debug_printf ("no_envblock %d", no_envblock);
|
|
|
|
|
2002-06-12 13:13:54 +08:00
|
|
|
if (from_cygheap)
|
|
|
|
{
|
|
|
|
const char *p;
|
2005-03-30 23:54:28 +08:00
|
|
|
if (env && !cygheap->user.issetuid ())
|
2002-06-17 07:34:43 +08:00
|
|
|
{
|
2002-06-29 10:36:08 +08:00
|
|
|
debug_printf ("duping existing value for '%s'", name);
|
2005-03-30 23:54:28 +08:00
|
|
|
/* Don't really care what it's set to if we're calling a cygwin program */
|
2005-03-31 05:35:31 +08:00
|
|
|
return cstrdup1 (env);
|
2002-06-17 07:34:43 +08:00
|
|
|
}
|
2002-06-12 13:13:54 +08:00
|
|
|
|
2002-06-17 07:34:43 +08:00
|
|
|
/* Calculate (potentially) value for given environment variable. */
|
2002-06-29 10:36:08 +08:00
|
|
|
p = (cygheap->user.*from_cygheap) (name, namelen);
|
2005-03-31 05:35:31 +08:00
|
|
|
if (!p || (no_envblock && !env) || (p == env_dontadd))
|
2002-06-21 13:01:17 +08:00
|
|
|
return env_dontadd;
|
2007-11-27 05:30:49 +08:00
|
|
|
char *s = (char *) cmalloc_abort (HEAP_1_STR, namelen + strlen (p) + 1);
|
2002-06-12 13:13:54 +08:00
|
|
|
strcpy (s, name);
|
2005-07-07 04:05:03 +08:00
|
|
|
strcpy (s + namelen, p);
|
2002-06-19 23:27:27 +08:00
|
|
|
debug_printf ("using computed value for '%s'", name);
|
2002-06-12 13:13:54 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2005-03-30 23:54:28 +08:00
|
|
|
if (env)
|
|
|
|
return cstrdup1 (env);
|
2002-06-12 13:13:54 +08:00
|
|
|
|
2002-06-29 10:36:08 +08:00
|
|
|
return getwinenveq (name, namelen, HEAP_1_STR);
|
2002-06-12 13:13:54 +08:00
|
|
|
}
|
2001-05-11 11:27:22 +08:00
|
|
|
|
2014-10-27 19:33:53 +08:00
|
|
|
static inline int
|
|
|
|
raise_envblock (int new_tl, PWCHAR &envblock, PWCHAR &s)
|
|
|
|
{
|
|
|
|
int tl = new_tl + 100;
|
|
|
|
PWCHAR new_envblock =
|
|
|
|
(PWCHAR) realloc (envblock, (2 + tl) * sizeof (WCHAR));
|
|
|
|
/* If realloc moves the block, move `s' with it. */
|
|
|
|
if (new_envblock != envblock)
|
|
|
|
{
|
|
|
|
s += new_envblock - envblock;
|
|
|
|
envblock = new_envblock;
|
|
|
|
}
|
|
|
|
return tl;
|
|
|
|
}
|
|
|
|
|
2002-06-12 13:13:54 +08:00
|
|
|
#define SPENVS_SIZE (sizeof (spenvs) / sizeof (spenvs[0]))
|
2001-05-11 11:27:22 +08:00
|
|
|
|
2014-12-02 18:16:03 +08:00
|
|
|
int
|
|
|
|
env_compare (const void *key, const void *memb)
|
|
|
|
{
|
|
|
|
const char *k = *(const char **) key;
|
|
|
|
const char *m = *(const char **) memb;
|
|
|
|
|
|
|
|
char *ke = strchr (k, '=');
|
|
|
|
char *me = strchr (m, '=');
|
|
|
|
if (ke == NULL || me == NULL)
|
|
|
|
return strcasecmp (k, m);
|
|
|
|
int ret = strncasecmp (k, m, MIN (ke - k, me - m));
|
|
|
|
if (!ret)
|
|
|
|
ret = (ke - k) - (me - m);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Create a Windows-style environment block, i.e. a typical character buffer
|
2000-12-10 08:45:12 +08:00
|
|
|
filled with null terminated strings, terminated by double null characters.
|
|
|
|
Converts environment variables noted in conv_envvars into win32 form
|
2014-12-02 18:16:03 +08:00
|
|
|
prior to placing them in the string.
|
|
|
|
|
|
|
|
If new_token is set, we're going to switch the user account in
|
|
|
|
child_info_spawn::worker. If so, we're also fetching the Windows default
|
|
|
|
environment for the new user, and merge it into the environment we propage
|
|
|
|
to the child. */
|
2013-01-21 12:34:52 +08:00
|
|
|
char ** __reg3
|
2008-03-07 19:24:51 +08:00
|
|
|
build_env (const char * const *envp, PWCHAR &envblock, int &envc,
|
2014-12-02 18:16:03 +08:00
|
|
|
bool no_envblock, HANDLE new_token)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2014-12-02 18:16:03 +08:00
|
|
|
PWCHAR cwinenv = NULL;
|
|
|
|
size_t winnum = 0;
|
|
|
|
char **winenv = NULL;
|
|
|
|
|
2002-06-13 09:28:51 +08:00
|
|
|
int len, n;
|
2000-02-18 03:38:33 +08:00
|
|
|
const char * const *srcp;
|
2002-06-12 13:13:54 +08:00
|
|
|
char **dstp;
|
|
|
|
bool saw_spenv[SPENVS_SIZE] = {0};
|
2001-05-11 11:27:22 +08:00
|
|
|
|
2018-06-06 23:45:55 +08:00
|
|
|
static char *const empty_env[] = { NULL };
|
|
|
|
|
2002-06-12 13:13:54 +08:00
|
|
|
debug_printf ("envp %p", envp);
|
2001-05-11 11:27:22 +08:00
|
|
|
|
2018-06-06 23:45:55 +08:00
|
|
|
if (!envp)
|
|
|
|
envp = empty_env;
|
|
|
|
|
2002-06-13 09:28:51 +08:00
|
|
|
/* How many elements? */
|
2000-02-18 03:38:33 +08:00
|
|
|
for (n = 0; envp[n]; n++)
|
|
|
|
continue;
|
|
|
|
|
2014-12-02 18:16:03 +08:00
|
|
|
/* Fetch windows env and convert to POSIX-style env. */
|
|
|
|
if (new_token
|
|
|
|
&& CreateEnvironmentBlock ((LPVOID *) &cwinenv, new_token, FALSE))
|
|
|
|
{
|
|
|
|
PWCHAR var = cwinenv;
|
|
|
|
while (*var)
|
|
|
|
{
|
|
|
|
++winnum;
|
|
|
|
var = wcschr (var, L'\0') + 1;
|
|
|
|
}
|
|
|
|
winenv = (char **) calloc (winnum + 1, sizeof (char *));
|
|
|
|
if (winenv)
|
|
|
|
{
|
|
|
|
for (winnum = 0, var = cwinenv;
|
|
|
|
*var;
|
|
|
|
++winnum, var = wcschr (var, L'\0') + 1)
|
Do not treat the command line or environment like paths
* dcrt0.cc (dll_crt0_1), environ.cc (environ_init, getwinenveq,
build_env), strfuncs.cc (sys_wcstombs, sys_wcstombs_alloc),
wchar.c (sys_wcstombs, sys_wcstombs_alloc): avoid mis-conversions
of text that does not, actually, refer to a path or file name
Detailed explanation:
Our WCS -> UTF conversion handles the private Unicode page specially
to allow for otherwise invalid file names. However, this handling makes
no sense for command-lines, nor environment variables, which we would
rather convert verbatim.
As a stop-gap solution, let's just introduce a version of the
sys_wcstombs() function that specifically excludes that file name
conversion magic.
The proper solution is to change sys_wcstombs() to assume that it is not
a path that wants to be converted, and introduce sys_wcstombs_path()
that does, but that is a bigger task which we leave for another patch.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2015-12-17 23:22:49 +08:00
|
|
|
sys_wcstombs_alloc_no_path (&winenv[winnum], HEAP_NOTHEAP, var);
|
2014-12-02 18:16:03 +08:00
|
|
|
}
|
|
|
|
DestroyEnvironmentBlock (cwinenv);
|
2015-01-14 18:31:14 +08:00
|
|
|
/* Eliminate variables which are already available in envp, as well as
|
|
|
|
the small set of crucial variables needing POSIX conversion and
|
|
|
|
potentially collide. The windows env is sorted, so we can use
|
|
|
|
bsearch. We're doing this first step, so the following code doesn't
|
|
|
|
allocate too much memory. */
|
2014-12-02 18:16:03 +08:00
|
|
|
if (winenv)
|
|
|
|
{
|
|
|
|
for (srcp = envp; *srcp; srcp++)
|
|
|
|
{
|
|
|
|
char **elem = (char **) bsearch (srcp, winenv, winnum,
|
|
|
|
sizeof *winenv, env_compare);
|
|
|
|
if (elem)
|
|
|
|
{
|
|
|
|
free (*elem);
|
|
|
|
/* Use memmove to keep array sorted.
|
|
|
|
winnum - (elem - winenv) copies all elements following
|
|
|
|
elem, including the trailing NULL pointer. */
|
|
|
|
memmove (elem, elem + 1,
|
|
|
|
(winnum - (elem - winenv)) * sizeof *elem);
|
|
|
|
--winnum;
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 18:31:14 +08:00
|
|
|
for (char **elem = winenv; *elem; elem++)
|
|
|
|
{
|
|
|
|
if (match_first_char (*elem, WC))
|
|
|
|
for (int i = 0; conv_envvars[i].name != NULL; i++)
|
|
|
|
if (strncmp (*elem, conv_envvars[i].name,
|
|
|
|
conv_envvars[i].namelen) == 0)
|
|
|
|
{
|
|
|
|
free (*elem);
|
|
|
|
memmove (elem, elem + 1,
|
|
|
|
(winnum - (elem - winenv)) * sizeof *elem);
|
|
|
|
--winnum;
|
|
|
|
--elem;
|
|
|
|
}
|
|
|
|
}
|
2014-12-02 18:16:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-13 09:28:51 +08:00
|
|
|
/* Allocate a new "argv-style" environ list with room for extra stuff. */
|
2007-11-27 05:30:49 +08:00
|
|
|
char **newenv = (char **) cmalloc_abort (HEAP_1_ARGV, sizeof (char *) *
|
2014-12-02 18:16:03 +08:00
|
|
|
(n + winnum + SPENVS_SIZE + 1));
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-06-13 09:28:51 +08:00
|
|
|
int tl = 0;
|
2005-03-31 05:35:31 +08:00
|
|
|
char **pass_dstp;
|
2014-12-02 18:16:03 +08:00
|
|
|
char **pass_env = (char **) alloca (sizeof (char *)
|
|
|
|
* (n + winnum + SPENVS_SIZE + 1));
|
2002-06-13 09:28:51 +08:00
|
|
|
/* Iterate over input list, generating a new environment list and refreshing
|
|
|
|
"special" entries, if necessary. */
|
2005-03-31 05:35:31 +08:00
|
|
|
for (srcp = envp, dstp = newenv, pass_dstp = pass_env; *srcp; srcp++)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2005-03-31 05:35:31 +08:00
|
|
|
bool calc_tl = !no_envblock;
|
2002-06-13 09:28:51 +08:00
|
|
|
/* Look for entries that require special attention */
|
2002-06-12 13:13:54 +08:00
|
|
|
for (unsigned i = 0; i < SPENVS_SIZE; i++)
|
2002-06-16 13:45:37 +08:00
|
|
|
if (!saw_spenv[i] && (*dstp = spenvs[i].retrieve (no_envblock, *srcp)))
|
2002-06-12 13:13:54 +08:00
|
|
|
{
|
|
|
|
saw_spenv[i] = 1;
|
2005-03-31 05:35:31 +08:00
|
|
|
if (*dstp == env_dontadd)
|
|
|
|
goto next1;
|
2005-05-01 01:07:05 +08:00
|
|
|
if (spenvs[i].add_if_exists)
|
2005-03-31 05:35:31 +08:00
|
|
|
calc_tl = true;
|
|
|
|
goto next0;
|
2002-06-12 13:13:54 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-06-16 13:45:37 +08:00
|
|
|
/* Add entry to new environment */
|
2002-06-13 09:28:51 +08:00
|
|
|
*dstp = cstrdup1 (*srcp);
|
2001-05-11 11:27:22 +08:00
|
|
|
|
2002-06-17 07:34:43 +08:00
|
|
|
next0:
|
2005-03-31 05:35:31 +08:00
|
|
|
if (calc_tl)
|
|
|
|
{
|
|
|
|
*pass_dstp++ = *dstp;
|
|
|
|
tl += strlen (*dstp) + 1;
|
|
|
|
}
|
2002-06-17 07:34:43 +08:00
|
|
|
dstp++;
|
|
|
|
next1:
|
|
|
|
continue;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-07-01 10:11:30 +08:00
|
|
|
assert ((srcp - envp) == n);
|
2002-06-13 09:28:51 +08:00
|
|
|
/* Fill in any required-but-missing environment variables. */
|
2004-10-08 05:28:57 +08:00
|
|
|
for (unsigned i = 0; i < SPENVS_SIZE; i++)
|
2014-12-02 18:16:03 +08:00
|
|
|
if (!saw_spenv[i] && (spenvs[i].force_into_environment
|
|
|
|
|| cygheap->user.issetuid ()))
|
2004-05-29 03:50:07 +08:00
|
|
|
{
|
2014-10-27 19:33:53 +08:00
|
|
|
*dstp = spenvs[i].retrieve (false);
|
|
|
|
if (*dstp && *dstp != env_dontadd)
|
|
|
|
{
|
|
|
|
*pass_dstp++ = *dstp;
|
|
|
|
tl += strlen (*dstp) + 1;
|
2014-12-02 18:16:03 +08:00
|
|
|
/* Eliminate from winenv. */
|
|
|
|
if (winenv)
|
|
|
|
{
|
|
|
|
char **elem = (char **) bsearch (dstp, winenv, winnum,
|
|
|
|
sizeof *winenv, env_compare);
|
|
|
|
if (elem)
|
|
|
|
{
|
|
|
|
free (*elem);
|
|
|
|
memmove (elem, elem + 1,
|
|
|
|
(winnum - (elem - winenv)) * sizeof *elem);
|
|
|
|
--winnum;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 19:33:53 +08:00
|
|
|
dstp++;
|
|
|
|
}
|
|
|
|
}
|
2004-05-29 03:50:07 +08:00
|
|
|
|
2014-12-02 18:16:03 +08:00
|
|
|
/* Fill in any Windows environment vars still missing. */
|
|
|
|
if (winenv)
|
|
|
|
{
|
|
|
|
char **elem;
|
|
|
|
for (elem = winenv; *elem; ++elem)
|
|
|
|
{
|
|
|
|
*dstp = cstrdup1 (*elem);
|
|
|
|
free (*elem);
|
|
|
|
*pass_dstp++ = *dstp;
|
|
|
|
tl += strlen (*dstp) + 1;
|
|
|
|
++dstp;
|
|
|
|
}
|
|
|
|
free (winenv);
|
|
|
|
}
|
|
|
|
|
2002-06-16 13:45:37 +08:00
|
|
|
envc = dstp - newenv; /* Number of entries in newenv */
|
2014-12-02 18:16:03 +08:00
|
|
|
assert ((size_t) envc <= (n + winnum + SPENVS_SIZE));
|
2002-06-12 13:13:54 +08:00
|
|
|
*dstp = NULL; /* Terminate */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2005-03-31 05:35:31 +08:00
|
|
|
size_t pass_envc = pass_dstp - pass_env;
|
|
|
|
if (!pass_envc)
|
2002-06-12 13:13:54 +08:00
|
|
|
envblock = NULL;
|
|
|
|
else
|
|
|
|
{
|
2005-03-31 05:35:31 +08:00
|
|
|
*pass_dstp = NULL;
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("env count %ld, bytes %d", pass_envc, tl);
|
2005-03-30 23:54:28 +08:00
|
|
|
win_env temp;
|
|
|
|
temp.reset ();
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-06-12 13:13:54 +08:00
|
|
|
/* Windows programs expect the environment block to be sorted. */
|
2005-03-31 05:35:31 +08:00
|
|
|
qsort (pass_env, pass_envc, sizeof (char *), env_sort);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-06-12 13:13:54 +08:00
|
|
|
/* Create an environment block suitable for passing to CreateProcess. */
|
2008-03-07 19:24:51 +08:00
|
|
|
PWCHAR s;
|
|
|
|
envblock = (PWCHAR) malloc ((2 + tl) * sizeof (WCHAR));
|
2002-06-13 09:28:51 +08:00
|
|
|
int new_tl = 0;
|
2014-10-27 19:33:53 +08:00
|
|
|
bool saw_PATH = false;
|
2005-03-31 05:35:31 +08:00
|
|
|
for (srcp = pass_env, s = envblock; *srcp; srcp++)
|
2002-06-12 13:13:54 +08:00
|
|
|
{
|
2002-06-13 09:28:51 +08:00
|
|
|
const char *p;
|
|
|
|
win_env *conv;
|
|
|
|
len = strcspn (*srcp, "=") + 1;
|
2006-09-08 04:42:53 +08:00
|
|
|
const char *rest = *srcp + len;
|
2002-06-13 09:28:51 +08:00
|
|
|
|
2005-01-11 20:42:35 +08:00
|
|
|
/* Check for a bad entry. This is necessary to get rid of empty
|
|
|
|
strings, induced by putenv and changing the string afterwards.
|
|
|
|
Note that this doesn't stop invalid strings without '=' in it
|
|
|
|
etc., but we're opting for speed here for now. Adding complete
|
|
|
|
checking would be pretty expensive. */
|
2006-09-08 04:42:53 +08:00
|
|
|
if (len == 1 || !*rest)
|
2005-01-11 20:42:35 +08:00
|
|
|
continue;
|
|
|
|
|
2002-06-13 09:28:51 +08:00
|
|
|
/* See if this entry requires posix->win32 conversion. */
|
2006-09-08 04:42:53 +08:00
|
|
|
conv = getwinenv (*srcp, rest, &temp);
|
2002-06-13 09:28:51 +08:00
|
|
|
if (conv)
|
2014-10-27 19:33:53 +08:00
|
|
|
{
|
|
|
|
p = conv->native; /* Use win32 path */
|
|
|
|
/* Does PATH exist in the environment? */
|
|
|
|
if (**srcp == 'P')
|
|
|
|
{
|
|
|
|
/* And is it non-empty? */
|
|
|
|
if (!conv->native || !conv->native[0])
|
|
|
|
continue;
|
|
|
|
saw_PATH = true;
|
|
|
|
}
|
|
|
|
}
|
2002-06-13 09:28:51 +08:00
|
|
|
else
|
|
|
|
p = *srcp; /* Don't worry about it */
|
|
|
|
|
2009-04-03 01:44:01 +08:00
|
|
|
len = sys_mbstowcs (NULL, 0, p);
|
2006-09-08 04:42:53 +08:00
|
|
|
new_tl += len; /* Keep running total of block length so far */
|
2002-06-13 09:28:51 +08:00
|
|
|
|
|
|
|
/* See if we need to increase the size of the block. */
|
|
|
|
if (new_tl > tl)
|
2014-10-27 19:33:53 +08:00
|
|
|
tl = raise_envblock (new_tl, envblock, s);
|
2002-06-13 09:28:51 +08:00
|
|
|
|
2014-10-27 19:33:53 +08:00
|
|
|
len = sys_mbstowcs (s, len, p);
|
2002-06-13 09:28:51 +08:00
|
|
|
|
|
|
|
/* See if environment variable is "special" in a Windows sense.
|
|
|
|
Under NT, the current directories for visited drives are stored
|
|
|
|
as =C:=\bar. Cygwin converts the '=' to '!' for hopefully obvious
|
|
|
|
reasons. We need to convert it back when building the envblock */
|
2008-03-07 19:24:51 +08:00
|
|
|
if (s[0] == L'!' && (iswdrive (s + 1) || (s[1] == L':' && s[2] == L':'))
|
|
|
|
&& s[3] == L'=')
|
|
|
|
*s = L'=';
|
2014-10-27 19:33:53 +08:00
|
|
|
s += len + 1;
|
|
|
|
}
|
|
|
|
/* If PATH doesn't exist in the environment, add a PATH with just
|
|
|
|
Cygwin's bin dir to the Windows env to allow loading system DLLs
|
|
|
|
during execve. */
|
|
|
|
if (!saw_PATH)
|
|
|
|
{
|
2019-08-07 16:51:16 +08:00
|
|
|
new_tl += cygheap->installation_dir.Length / sizeof (WCHAR) + 5 + 1;
|
2014-10-27 19:33:53 +08:00
|
|
|
if (new_tl > tl)
|
|
|
|
tl = raise_envblock (new_tl, envblock, s);
|
2018-12-25 08:06:34 +08:00
|
|
|
s = wcpcpy (wcpcpy (s, L"PATH="),
|
|
|
|
cygheap->installation_dir.Buffer) + 1;
|
2002-06-12 13:13:54 +08:00
|
|
|
}
|
2008-03-07 19:24:51 +08:00
|
|
|
*s = L'\0'; /* Two null bytes at the end */
|
2002-06-13 09:28:51 +08:00
|
|
|
assert ((s - envblock) <= tl); /* Detect if we somehow ran over end
|
|
|
|
of buffer */
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-06-15 05:46:19 +08:00
|
|
|
debug_printf ("envp %p, envc %d", newenv, envc);
|
2002-06-12 13:13:54 +08:00
|
|
|
return newenv;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
2000-07-17 04:06:11 +08:00
|
|
|
|
2017-11-27 21:36:06 +08:00
|
|
|
#ifdef __i386__
|
2000-07-17 04:06:11 +08:00
|
|
|
/* This idiocy is necessary because the early implementers of cygwin
|
|
|
|
did not seem to know about importing data variables from the DLL.
|
|
|
|
So, we have to synchronize cygwin's idea of the environment with the
|
|
|
|
main program's with each reference to the environment. */
|
2000-07-30 00:24:59 +08:00
|
|
|
extern "C" char ** __stdcall
|
2000-07-17 04:06:11 +08:00
|
|
|
cur_environ ()
|
|
|
|
{
|
|
|
|
if (*main_environ != __cygwin_environ)
|
|
|
|
{
|
|
|
|
__cygwin_environ = *main_environ;
|
|
|
|
update_envptrs ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return __cygwin_environ;
|
|
|
|
}
|
2013-04-23 17:44:36 +08:00
|
|
|
#endif
|