public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: rename __cygwin_environ and drop env redirection via cur_environ()
Date: Thu, 28 Jul 2022 20:00:51 +0000 (GMT)	[thread overview]
Message-ID: <20220728200051.0788C3858D33@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=7f42498be6cd47e9d3d46ec82374d703e3275ddf

commit 7f42498be6cd47e9d3d46ec82374d703e3275ddf
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Thu Jul 28 22:00:40 2022 +0200

    Cygwin: rename __cygwin_environ and drop env redirection via cur_environ()
    
    Back in early Cygwin development a function based access to the
    environment was exported, the internal environ in Cygwin was called
    __cygwin_environ and cur_environ() was used to access the environment
    indirectly .  The history of that necessity is not documented,
    but kept in i686 for backward compatibility.
    
    The x86_64 port eventually used __cygwin_environ directly and exported
    it as DATA under the usual name environ.
    
    We don't need the i686 workaround anymore, so just rename
    __cygwin_environ to environ, drop the cur_environ() macro and
    simply export environ under its own name.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/common.din  |  1 +
 winsup/cygwin/dcrt0.cc    |  2 +-
 winsup/cygwin/environ.cc  | 30 +++++++++++++++---------------
 winsup/cygwin/environ.h   |  2 --
 winsup/cygwin/exec.cc     |  8 ++++----
 winsup/cygwin/external.cc |  2 +-
 winsup/cygwin/globals.cc  |  4 +---
 winsup/cygwin/pinfo.cc    |  4 ++--
 winsup/cygwin/spawn.cc    |  8 ++++----
 winsup/cygwin/syscalls.cc |  2 +-
 winsup/cygwin/x86_64.din  |  3 ---
 11 files changed, 30 insertions(+), 36 deletions(-)

diff --git a/winsup/cygwin/common.din b/winsup/cygwin/common.din
index 751ab3754..afcb71725 100644
--- a/winsup/cygwin/common.din
+++ b/winsup/cygwin/common.din
@@ -18,6 +18,7 @@ _sys_errlist DATA
 _sys_nerr DATA
 _timezone DATA
 _tzname DATA
+environ DATA
 error_message_count DATA
 error_one_per_line DATA
 error_print_progname DATA
diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc
index 97eefa19d..57694bcda 100644
--- a/winsup/cygwin/dcrt0.cc
+++ b/winsup/cygwin/dcrt0.cc
@@ -1002,7 +1002,7 @@ dll_crt0_1 (void *)
       sig_dispatch_pending (false);
       _my_tls.call_signal_handler ();
       _my_tls.incyg--;	/* Not in Cygwin anymore */
-      cygwin_exit (user_data->main (__argc, newargv, __cygwin_environ));
+      cygwin_exit (user_data->main (__argc, newargv, environ));
     }
   __asm__ ("				\n\
 	.global _cygwin_exit_return	\n\
diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc
index ca60bd8d4..0ac1acc41 100644
--- a/winsup/cygwin/environ.cc
+++ b/winsup/cygwin/environ.cc
@@ -422,7 +422,7 @@ getwinenv (const char *env, const char *in_posix, win_env *temp)
       {
 	win_env *we = conv_envvars + i;
 	const char *val;
-	if (!cur_environ () || !(val = in_posix ?: getenv (we->name)))
+	if (!environ || !(val = in_posix ?: getenv (we->name)))
 	  debug_printf ("can't set native for %s since no environ yet",
 			we->name);
 	else if (!we->posix || strcmp (val, we->posix) != 0)
@@ -486,7 +486,7 @@ my_findenv (const char *name, int *offset)
   char **p;
   const char *c;
 
-  if (cur_environ () == NULL)
+  if (!environ)
     return NULL;
 
   c = name;
@@ -497,11 +497,11 @@ my_findenv (const char *name, int *offset)
       len++;
     }
 
-  for (p = cur_environ (); *p; ++p)
+  for (p = environ; *p; ++p)
     if (!strncmp (*p, name, len))
       if (*(c = *p + len) == '=')
 	{
-	  *offset = p - cur_environ ();
+	  *offset = p - environ;
 	  return (char *) (++c);
 	}
   return NULL;
@@ -602,7 +602,7 @@ _addenv (const char *name, const char *value, int overwrite)
     }
   else
     {				/* Create new slot. */
-      int sz = envsize (cur_environ ());
+      int sz = envsize (environ);
 
       /* If sz == 0, we need two new slots, one for the terminating NULL. */
       int newsz = sz == 0 ? 2 : sz + 1;
@@ -611,11 +611,11 @@ _addenv (const char *name, const char *value, int overwrite)
       offset = newsz - 2;
 
       /* Allocate space for additional element. */
-      if (cur_environ () == lastenviron)
-	lastenviron = __cygwin_environ = (char **) realloc (lastenviron,
+      if (environ == lastenviron)
+	lastenviron = environ = (char **) realloc (lastenviron,
 							    allocsz);
       else if ((lastenviron = (char **) realloc (lastenviron, allocsz)) != NULL)
-	__cygwin_environ = (char **) memcpy (lastenviron, __cygwin_environ,
+	environ = (char **) memcpy (lastenviron, environ,
 					     sz * sizeof (char *));
       if (!lastenviron)
 	{
@@ -625,13 +625,13 @@ _addenv (const char *name, const char *value, int overwrite)
 	  return -1;				/* Oops.  No more memory. */
 	}
 
-      __cygwin_environ[offset + 1] = NULL;	/* NULL terminate. */
+      environ[offset + 1] = NULL;	/* NULL terminate. */
     }
 
   char *envhere;
   if (!issetenv)
     /* Not setenv. Just overwrite existing. */
-    envhere = cur_environ ()[offset] = (char *) (ENVMALLOC ? strdup (name) : name);
+    envhere = environ[offset] = (char *) (ENVMALLOC ? strdup (name) : name);
   else
     {				/* setenv */
       /* Look for an '=' in the name and ignore anything after that if found. */
@@ -640,7 +640,7 @@ _addenv (const char *name, const char *value, int overwrite)
 
       int namelen = p - name;	/* Length of name. */
       /* Allocate enough space for name + '=' + value + '\0' */
-      envhere = cur_environ ()[offset] = (char *) malloc (namelen + valuelen + 2);
+      envhere = environ[offset] = (char *) malloc (namelen + valuelen + 2);
       if (!envhere)
 	return -1;		/* Oops.  No more memory. */
 
@@ -718,7 +718,7 @@ unsetenv (const char *name)
 
       while (my_findenv (name, &offset))	/* if set multiple times */
 	/* Move up the rest of the array */
-	for (e = cur_environ () + offset; ; e++)
+	for (e = environ + offset; ; e++)
 	  if (!(*e = *(e + 1)))
 	    break;
 
@@ -735,12 +735,12 @@ clearenv (void)
 {
   __try
     {
-      if (cur_environ () == lastenviron)
+      if (environ == lastenviron)
 	{
 	  free (lastenviron);
 	  lastenviron = NULL;
 	}
-      __cygwin_environ = NULL;
+      environ = NULL;
       return 0;
     }
   __except (EFAULT) {}
@@ -842,7 +842,7 @@ environ_init (char **envp, int envc)
 
     out:
       findenv_func = (char * (*)(const char*, int*)) my_findenv;
-      __cygwin_environ = envp;
+      environ = envp;
       if (envp_passed_in)
 	{
 	  p = getenv ("CYGWIN");
diff --git a/winsup/cygwin/environ.h b/winsup/cygwin/environ.h
index 3980d6b5a..86e64a72f 100644
--- a/winsup/cygwin/environ.h
+++ b/winsup/cygwin/environ.h
@@ -33,8 +33,6 @@ struct win_env
 win_env *getwinenv (const char *name, const char *posix = NULL, win_env * = NULL);
 char *getwinenveq (const char *name, size_t len, int);
 
-extern "C" char **__cygwin_environ;
-#define cur_environ()  __cygwin_environ
 char **build_env (const char * const *envp, PWCHAR &envblock,
 			  int &envc, bool need_envblock, HANDLE new_token);
 
diff --git a/winsup/cygwin/exec.cc b/winsup/cygwin/exec.cc
index 6ed714d6a..2f4041329 100644
--- a/winsup/cygwin/exec.cc
+++ b/winsup/cygwin/exec.cc
@@ -32,7 +32,7 @@ execl (const char *path, const char *arg0, ...)
       argv[i] = va_arg (args, const char *);
   while (argv[i++] != NULL);
   va_end (args);
-  return spawnve (_P_OVERLAY, path, (char * const  *) argv, cur_environ ());
+  return spawnve (_P_OVERLAY, path, (char * const  *) argv, environ);
 }
 
 extern "C" int
@@ -71,13 +71,13 @@ execlp (const char *file, const char *arg0, ...)
   va_end (args);
   return spawnve (_P_OVERLAY | _P_PATH_TYPE_EXEC,
 		  find_exec (file, buf, "PATH", FE_NNF) ?: "",
-		  (char * const  *) argv, cur_environ ());
+		  (char * const  *) argv, environ);
 }
 
 extern "C" int
 execv (const char *path, char * const *argv)
 {
-  return spawnve (_P_OVERLAY, path, argv, cur_environ ());
+  return spawnve (_P_OVERLAY, path, argv, environ);
 }
 
 extern "C" int
@@ -94,7 +94,7 @@ execvp (const char *file, char * const *argv)
 
   return spawnve (_P_OVERLAY | _P_PATH_TYPE_EXEC,
 		  find_exec (file, buf, "PATH", FE_NNF) ?: "",
-		  argv, cur_environ ());
+		  argv, environ);
 }
 
 extern "C" int
diff --git a/winsup/cygwin/external.cc b/winsup/cygwin/external.cc
index a9a5b6432..bc6a3139d 100644
--- a/winsup/cygwin/external.cc
+++ b/winsup/cygwin/external.cc
@@ -140,7 +140,7 @@ create_winenv (const char * const *env)
 {
   int unused_envc;
   PWCHAR envblock = NULL;
-  char **envp = build_env (env ?: cur_environ (), envblock, unused_envc, false,
+  char **envp = build_env (env ?: environ, envblock, unused_envc, false,
 			   NULL);
   PWCHAR p = envblock;
 
diff --git a/winsup/cygwin/globals.cc b/winsup/cygwin/globals.cc
index 294a67fe8..e8147cb5c 100644
--- a/winsup/cygwin/globals.cc
+++ b/winsup/cygwin/globals.cc
@@ -155,9 +155,7 @@ const int __collate_load_error = 0;
   extern UNICODE_STRING _RDATA ro_u_mq_suffix = _ROU (L":mqueue");
   #undef _ROU
 
-  /* This is an exported copy of environ which can be used by DLLs
-     which use cygwin.dll.  */
-  char **__cygwin_environ;
+  char **environ;
   /* __progname used in getopt error message */
   char *__progname;
   char *program_invocation_name;
diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc
index 832a3e6bc..2e336c1a3 100644
--- a/winsup/cygwin/pinfo.cc
+++ b/winsup/cygwin/pinfo.cc
@@ -769,7 +769,7 @@ commune_process (void *arg)
       {
 	sigproc_printf ("processing PICOM_ENVIRON");
 	unsigned n = 0;
-	char **env = cur_environ ();
+	char **env = environ;
 	if (env)
 	  for (char **e = env; *e; e++)
 	    n += strlen (*e) + 1;
@@ -1210,7 +1210,7 @@ _pinfo::environ (size_t& n)
       return cr.s;
     }
   else
-    env = cur_environ ();
+    env = ::environ;
 
   if (env == NULL)
     return NULL;
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 277f0d1b3..2f745e14a 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -1130,7 +1130,7 @@ spawnl (int mode, const char *path, const char *arg0, ...)
 
   va_end (args);
 
-  return spawnve (mode, path, (char * const  *) argv, cur_environ ());
+  return spawnve (mode, path, (char * const  *) argv, environ);
 }
 
 extern "C" int
@@ -1174,7 +1174,7 @@ spawnlp (int mode, const char *file, const char *arg0, ...)
   va_end (args);
 
   return spawnve (mode | _P_PATH_TYPE_EXEC, find_exec (file, buf),
-		  (char * const *) argv, cur_environ ());
+		  (char * const *) argv, environ);
 }
 
 extern "C" int
@@ -1204,7 +1204,7 @@ spawnlpe (int mode, const char *file, const char *arg0, ...)
 extern "C" int
 spawnv (int mode, const char *path, const char * const *argv)
 {
-  return spawnve (mode, path, argv, cur_environ ());
+  return spawnve (mode, path, argv, environ);
 }
 
 extern "C" int
@@ -1213,7 +1213,7 @@ spawnvp (int mode, const char *file, const char * const *argv)
   path_conv buf;
   return spawnve (mode | _P_PATH_TYPE_EXEC,
 		  find_exec (file, buf, "PATH", FE_NNF) ?: "",
-		  argv, cur_environ ());
+		  argv, environ);
 }
 
 extern "C" int
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index da8779743..6455f25ab 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -4372,7 +4372,7 @@ popen (const char *command, const char *in_type)
       fcntl (stdchild, F_SETFD, stdchild_state | FD_CLOEXEC);
 
       /* Start a shell process to run the given command without forking. */
-      pid_t pid = ch_spawn.worker ("/bin/sh", argv, cur_environ (), _P_NOWAIT,
+      pid_t pid = ch_spawn.worker ("/bin/sh", argv, environ, _P_NOWAIT,
 				   __std[0], __std[1]);
 
       /* Reinstate the close-on-exec state */
diff --git a/winsup/cygwin/x86_64.din b/winsup/cygwin/x86_64.din
index e1896bf8a..b0b6f6b78 100644
--- a/winsup/cygwin/x86_64.din
+++ b/winsup/cygwin/x86_64.din
@@ -1,9 +1,6 @@
 LIBRARY "cygwin1.dll" BASE=0x180040000
 
 EXPORTS
-#Exported variables
-environ = __cygwin_environ DATA
-
 #Exported functions
 __wrap__Znam NOSIGFE                # void *operator new[](std::size_t sz) throw (std::bad_alloc)
 __wrap__ZnamRKSt9nothrow_t NOSIGFE  # void *operator new[](std::size_t sz, const std::nothrow_t &nt) throw()


                 reply	other threads:[~2022-07-28 20:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220728200051.0788C3858D33@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).