Web lists-archives.com

[Mingw-msys] (no subject)




When an (msys) application launches another program via exec() or
spawn(), it uses the msys implementation of those functions. These
implementations are smart, and can detect when the to-be-launched
application is a non-msys (that is, native w32) app, and translates all
the environment variables (like PATH, etc) from unix to win32 format.

However, if you have an (msys) application that launches another app via
the Windows CreateProcess API (like, for instance, run.exe or
cygstart.exe), then the launcher app needs to manually synchronize the
win32 environment with the msys environment -- and translate those env
vars.  The details of this task really belong inside msys, not in a
bunch of separate applications.  Cygwin added the CW_SYNC_WINENV
cygwin_internal function four years ago; the attached patch implements
it for MSYS.

The patch also (a) adds a few additional vars to MSYS's list of "make
sure these are always defined" variables, based on today's cygwin
implementation, (b) cleans up the implementation of cygwin_internal to
ensure that va_end() is always called, and sets errno to ENOSYS for bad
CW_ values.


2010.09.04  Charles Wilson  <...>

       * environ.cc (forced_winenv_vars): Ensure that HOMEDRIVE,
       HOMEPATH, PATH, USERDOMAIN, USERNAME, USERPROFILE, and WINDIR
       environment variables exist.
       * external.cc (sync_winenv): New function.
       (cygwin_internal): Ensure that va_arg is cleaned up before
       return. Set errno on bad call. Support new CW_SYNC_WINENV option.
       * include/cygwin/version.h: Bump API version number.
       * include/sys/cygwin.h: Define CW_SYNC_WINENV.

Ok to commit?

--
Chuck
Index: winsup/cygwin/environ.cc
===================================================================
RCS file: /cvsroot/mingw/msys/rt/src/winsup/cygwin/environ.cc,v
retrieving revision 1.12
diff -u -p -r1.12 environ.cc
--- winsup/cygwin/environ.cc	27 Apr 2006 15:40:27 -0000	1.12
+++ winsup/cygwin/environ.cc	5 Sep 2010 01:59:11 -0000
@@ -787,11 +787,18 @@ env_sort (const void *a, const void *b)
 /* Keep this list in upper case and sorted */
 static const NO_COPY char* forced_winenv_vars [] =
   {
+    "HOMEDRIVE",
+    "HOMEPATH",
+    "PATH",
 #if __MSYS__
     "MSYSTEM",
 #endif /* __MSYS__ */
     "SYSTEMDRIVE",
     "SYSTEMROOT",
+    "USERDOMAIN",
+    "USERNAME",
+    "USERPROFILE",
+    "WINDIR",
     NULL
   };
 
Index: winsup/cygwin/external.cc
===================================================================
RCS file: /cvsroot/mingw/msys/rt/src/winsup/cygwin/external.cc,v
retrieving revision 1.3
diff -u -p -r1.3 external.cc
--- winsup/cygwin/external.cc	14 Jan 2010 04:16:37 -0000	1.3
+++ winsup/cygwin/external.cc	5 Sep 2010 01:59:11 -0000
@@ -18,8 +18,11 @@ details. */
 #include "pinfo.h"
 #include <exceptions.h>
 #include "shared_info.h"
+#include "cygerrno.h"
 #include "cygwin_version.h"
 #include "perprocess.h"
+#include "environ.h"
+#include <stdlib.h>
 
 static external_pinfo *
 fillout_pinfo (pid_t pid, int winpid)
@@ -107,6 +110,31 @@ get_cygdrive_prefixes (char *user, char 
   return res;
 }
 
+/* Copy cygwin environment variables to the Windows environment. */
+static void
+sync_winenv ()
+{
+  char *envblock = NULL;
+
+  envblock = winenv (__cygwin_environ, 0);
+  char *p = envblock;
+
+  if (!p)
+    return;
+  while (*p)
+    {
+      char *eq = strchr (p, '=');
+      if (eq)
+       {
+         *eq = '\0';
+         SetEnvironmentVariable (p, ++eq);
+         p = eq;
+       }
+      p = strchr (p, '\0') + 1;
+    }
+  free (envblock);
+}
+
 /*
  * Cygwin-specific wrapper for win32 ExitProcess and TerminateProcess.
  * It ensures that the correct exit code, derived from the specified
@@ -145,52 +173,64 @@ extern "C" DWORD
 cygwin_internal (cygwin_getinfo_types t, ...)
 {
   va_list arg;
+  DWORD res = (DWORD) -1;
   va_start (arg, t);
 
   switch (t)
     {
       case CW_LOCK_PINFO:
-	return 1;
+	res = 1;
+	break;
 
       case CW_UNLOCK_PINFO:
-	return 1;
+	res = 1;
+	break;
 
       case CW_GETTHREADNAME:
-	return (DWORD) threadname (va_arg (arg, DWORD));
+	res = (DWORD) threadname (va_arg (arg, DWORD));
+	break;
 
       case CW_SETTHREADNAME:
 	{
 	  char *name = va_arg (arg, char *);
 	  regthread (name, va_arg (arg, DWORD));
-	  return 1;
+	  res = 1;
 	}
+	break;
 
       case CW_GETPINFO:
-	return (DWORD) fillout_pinfo (va_arg (arg, DWORD), 0);
+	res = (DWORD) fillout_pinfo (va_arg (arg, DWORD), 0);
+	break;
 
       case CW_GETVERSIONINFO:
-	return (DWORD) cygwin_version_strings;
+	res = (DWORD) cygwin_version_strings;
+	break;
 
       case CW_USER_DATA:
-	return (DWORD) &__cygwin_user_data;
+	res = (DWORD) &__cygwin_user_data;
+	break;
 
       case CW_PERFILE:
 	perfile_table = va_arg (arg, struct __cygwin_perfile *);
-	return 0;
+	res = 0;
+	break;
 
       case CW_GET_CYGDRIVE_PREFIXES:
 	{
 	  char *user = va_arg (arg, char *);
 	  char *system = va_arg (arg, char *);
-	  return get_cygdrive_prefixes (user, system);
+	  res = get_cygdrive_prefixes (user, system);
 	}
+	break;
 
       case CW_GETPINFO_FULL:
-	return (DWORD) fillout_pinfo (va_arg (arg, pid_t), 1);
+	res = (DWORD) fillout_pinfo (va_arg (arg, pid_t), 1);
+	break;
 
       case CW_INIT_EXCEPTIONS:
 	init_exceptions ((exception_list *) arg);
-	return 0;
+	res = 0;
+	break;
 
       case CW_GET_CYGDRIVE_INFO:
 	{
@@ -198,8 +238,9 @@ cygwin_internal (cygwin_getinfo_types t,
 	  char *system = va_arg (arg, char *);
 	  char *user_flags = va_arg (arg, char *);
 	  char *system_flags = va_arg (arg, char *);
-	  return get_cygdrive_info (user, system, user_flags, system_flags);
+	  res = get_cygdrive_info (user, system, user_flags, system_flags);
 	}
+	break;
 
       case CW_EXIT_PROCESS:
         {
@@ -208,7 +249,16 @@ cygwin_internal (cygwin_getinfo_types t,
           exit_process (status, !!useTerminateProcess); /* no return */
         }
 
+      case CW_SYNC_WINENV:
+        sync_winenv ();
+        res = 0;
+	break;
+
       default:
-	return (DWORD) -1;
+	res = (DWORD) -1;
+	set_errno (ENOSYS);
+	break;
     }
+  va_end (arg);
+  return res;
 }
Index: winsup/cygwin/include/cygwin/version.h
===================================================================
RCS file: /cvsroot/mingw/msys/rt/src/winsup/cygwin/include/cygwin/version.h,v
retrieving revision 1.17
diff -u -p -r1.17 version.h
--- winsup/cygwin/include/cygwin/version.h	18 Mar 2010 01:16:35 -0000	1.17
+++ winsup/cygwin/include/cygwin/version.h	5 Sep 2010 01:59:11 -0000
@@ -42,7 +42,7 @@ details. */
 	 changes to the DLL and is mainly informative in nature. */
 
   /* The current cygwin version is 1.3.3 */
-  /* The current msys version is 1.0.13 */
+  /* The current msys version is 1.0.14 */
 
 #define CYGWIN_VERSION_DLL_MAJOR 1000
 #define CYGWIN_VERSION_DLL_MINOR 14
@@ -148,11 +148,12 @@ details. */
        45: perprocess change, gamma_r, gammaf_r, lgamma_r, lgammaf_r
        46: Remove cygwin_getshared
        47: CW_EXIT_PROCESS added.
+       48: CW_SYNC_WINENV added.
 
      */
 
 #define CYGWIN_VERSION_API_MAJOR 0
-#define CYGWIN_VERSION_API_MINOR 47
+#define CYGWIN_VERSION_API_MINOR 48
 
      /* There is also a compatibity version number associated with the
 	shared memory regions.  It is incremented when incompatible
Index: winsup/cygwin/include/sys/cygwin.h
===================================================================
RCS file: /cvsroot/mingw/msys/rt/src/winsup/cygwin/include/sys/cygwin.h,v
retrieving revision 1.6
diff -u -p -r1.6 cygwin.h
--- winsup/cygwin/include/sys/cygwin.h	14 Jan 2010 04:16:37 -0000	1.6
+++ winsup/cygwin/include/sys/cygwin.h	5 Sep 2010 01:59:11 -0000
@@ -66,7 +66,8 @@ typedef enum
     CW_GETPINFO_FULL,
     CW_INIT_EXCEPTIONS,
     CW_GET_CYGDRIVE_INFO,
-    CW_EXIT_PROCESS
+    CW_EXIT_PROCESS,
+    CW_SYNC_WINENV
   } cygwin_getinfo_types;
 
 #define CW_NEXTPID	0x80000000	// or with pid to get next one
------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Mingw-msys mailing list
Mingw-msys@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/mingw-msys