From f991d0e53ebed74e93a7d0ff4c878ed0f37f2742 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Mon, 9 May 2005 01:56:34 +0000 Subject: [PATCH] * cygcheck.cc (nuke): New function. (load_cygwin): New function. (main): Use load_cygwin to load argv/envp from cygwin environment, if appropriate. --- winsup/utils/ChangeLog | 7 +++++++ winsup/utils/cygcheck.cc | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/winsup/utils/ChangeLog b/winsup/utils/ChangeLog index 9438a5586..27477891e 100644 --- a/winsup/utils/ChangeLog +++ b/winsup/utils/ChangeLog @@ -1,3 +1,10 @@ +2005-05-08 Christopher Faylor + + * cygcheck.cc (nuke): New function. + (load_cygwin): New function. + (main): Use load_cygwin to load argv/envp from cygwin environment, if + appropriate. + 2005-05-08 Christopher Faylor * strace.cc (attach_process): Don't call load_cygwin(). Assume that diff --git a/winsup/utils/cygcheck.cc b/winsup/utils/cygcheck.cc index c8bcff600..dd815b9ed 100644 --- a/winsup/utils/cygcheck.cc +++ b/winsup/utils/cygcheck.cc @@ -8,6 +8,7 @@ Cygwin license. Please consult the file "CYGWIN_LICENSE" for details. */ +#define cygwin_internal cygwin_internal_dontuse #include #include #include @@ -19,6 +20,7 @@ #include #include "cygwin/include/sys/cygwin.h" #include "cygwin/include/mntent.h" +#undef cygwin_internal #define alloca __builtin_alloca @@ -1427,10 +1429,48 @@ Compiled on %s\n\ ", len, v, __DATE__); } +void +nuke (char *ev) +{ + int n = 1 + strchr (*_environ, '=') - ev; + char *s = (char *) alloca (n); + memcpy (s, ev, n); + s[n] = '\0'; + putenv (s); +} + +DWORD (*cygwin_internal) (int, ...); + +static void +load_cygwin (int& argc, char **&argv) +{ + HMODULE h; + + if (!(h = LoadLibrary ("cygwin1.dll"))) + return; + if (!(cygwin_internal = (DWORD (*) (int, ...)) GetProcAddress (h, "cygwin_internal"))) + return; + + char **av = (char **) cygwin_internal (CW_ARGV); + if (av) + for (argc = 0, argv = av; *av; av++) + argc++; + + char **envp = (char **) cygwin_internal (CW_ENVP); + if (envp) + { + while (*_environ) + nuke (*_environ); + for (char **ev = envp; *ev; ev++) + putenv (*ev); + } +} + int main (int argc, char **argv) { int i; + load_cygwin (argc, argv); (void) putenv("POSIXLY_CORRECT=1"); while ((i = getopt_long (argc, argv, opts, longopts, NULL)) != EOF)