Re: [RFA] Environment variables passed to inferior by MinGW build (PR 10989)

Eli Zaretskii
2011-09-28T09:15:43Z

> Date: Mon, 26 Sep 2011 20:21:24 +0300
> From: Eli Zaretskii <eliz@gnu.org>
> Cc: gdb-patches@sourceware.org
>
> > Date: Mon, 26 Sep 2011 10:15:11 -0700
> > From: Joel Brobecker <brobecker@adacore.com>
> > Cc: gdb-patches@sourceware.org
> >
> > Just a few comments:
>
> Thanks, I will fix all of these. (They came from the original code.)

Done. The actual patch I committed is below.

I replaced env_sort with envvar_cmp.

The last comment, viz.:

> > >>>+ for (envlen = 0, i = 0; in_env[i]&& *in_env[i]; i++)
> ^^^ formatting of the `&&' ?
>

didn't need any change, because there's no such problem in the patch I
sent. Joel, you were replying to Stan's mail, so I'm guessing that
Stan's mail software tried to be overly smart about the formatting of
the patch.

Thanks for the review and the approval.

Could someone with the appropriate authority please close bug #10989,
or instruct me how to do that?

Index: gdb/ChangeLog
===================================================================
RCS file: /cvs/src/src/gdb/ChangeLog,v
retrieving revision 1.13362
retrieving revision 1.13363
diff -u -r1.13362 -r1.13363
--- gdb/ChangeLog 27 Sep 2011 15:30:12 -0000 1.13362
+++ gdb/ChangeLog 28 Sep 2011 09:07:54 -0000 1.13363
@@ -1,3 +1,11 @@
+2011-09-28 Eli Zaretskii <eliz@gnu.org>
+
+ * windows-nat.c (env_sort) [!__CYGWIN__]: Function restored from
+ before the change on 2006-12-09.
+ (windows_create_inferior) [!__CYGWIN__]: Restore code that
+ generates the environment block for CreateProcessA, modulo the
+ Cygwin-specific parts that are not needed here.
+
2011-09-27 Tristan Gingold <gingold@adacore.com>

* target.h (enum target_object): Add TARGET_OBJECT_DARWIN_DYLD_INFO.

Index: gdb/windows-nat.c
===================================================================
RCS file: /cvs/src/src/gdb/windows-nat.c,v
retrieving revision 1.218
retrieving revision 1.219
diff -u -p -r1.218 -r1.219
--- gdb/windows-nat.c 9 May 2011 14:25:37 -0000 1.218
+++ gdb/windows-nat.c 28 Sep 2011 09:07:54 -0000 1.219
@@ -1951,6 +1951,18 @@ windows_set_console_info (STARTUPINFO *s
*flags |= CREATE_NEW_CONSOLE;
}

+#ifndef __CYGWIN__
+/* Function called by qsort to sort environment strings. */
+
+static int
+envvar_cmp (const void *a, const void *b)
+{
+ const char **p = (const char **) a;
+ const char **q = (const char **) b;
+ return strcasecmp (*p, *q);
+}
+#endif
+
/* Start an inferior windows child process and sets inferior_ptid to its pid.
EXEC_FILE is the file to run.
ALLARGS is a string containing the arguments to the program.
@@ -1977,6 +1989,12 @@ windows_create_inferior (struct target_o
char *toexec;
char *args;
HANDLE tty;
+ char *w32env;
+ char *temp;
+ size_t envlen;
+ int i;
+ size_t envsize;
+ char **env;
#endif
PROCESS_INFORMATION pi;
BOOL ret;
@@ -2124,6 +2142,31 @@ windows_create_inferior (struct target_o
}
}

+ /* CreateProcess takes the environment list as a null terminated set of
+ strings (i.e. two nulls terminate the list). */
+
+ /* Get total size for env strings. */
+ for (envlen = 0, i = 0; in_env[i] && *in_env[i]; i++)
+ envlen += strlen (in_env[i]) + 1;
+
+ envsize = sizeof (in_env[0]) * (i + 1);
+ env = (char **) alloca (envsize);
+ memcpy (env, in_env, envsize);
+ /* Windows programs expect the environment block to be sorted. */
+ qsort (env, i, sizeof (char *), envvar_cmp);
+
+ w32env = alloca (envlen + 1);
+
+ /* Copy env strings into new buffer. */
+ for (temp = w32env, i = 0; env[i] && *env[i]; i++)
+ {
+ strcpy (temp, env[i]);
+ temp += strlen (temp) + 1;
+ }
+
+ /* Final nil string to terminate new env. */
+ *temp = 0;
+
windows_init_thread_list ();
ret = CreateProcessA (0,
args, /* command line */
@@ -2131,7 +2174,7 @@ windows_create_inferior (struct target_o
NULL, /* thread */
TRUE, /* inherit handles */
flags, /* start flags */
- NULL, /* environment */
+ w32env, /* environment */
NULL, /* current directory */
&si,
&pi);