From b37e2d99550cbf816b57fac4130f355479511f25 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Sun, 3 Jul 2022 22:39:32 +0200
Subject: [PATCH 31/N] uname: allow setting the system name to CYGWIN

We are currently trying to move our cygwin build environment closer
to cygwin and some autotools/bash based build systems call "uname -s"
to figure out the OS and in many cases only handle the cygwin case, so
we have to patch them.

With this instead of patching we can set MSYSTEM=CYGWIN and change
uname output that way.

The next step would be to always output CYGWIN in an msys env by default,
but for now this allows us to get rid of all the patches without
affecting users.
---
 winsup/cygwin/uname.cc | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/winsup/cygwin/uname.cc b/winsup/cygwin/uname.cc
index 0d0c5aa..a4ac0e3 100644
--- a/winsup/cygwin/uname.cc
+++ b/winsup/cygwin/uname.cc
@@ -24,6 +24,24 @@ extern "C" int getdomainname (char *__name, size_t __len);
 #define ATTRIBUTE_NONSTRING
 #endif
 
+static const char*
+get_sysname()
+{
+#ifdef __MSYS__
+  char* msystem = getenv("MSYSTEM");
+  if (!msystem || strcmp(msystem, "MSYS") == 0)
+    return "MSYS";
+  else if (strcmp(msystem, "CYGWIN") == 0)
+    return "CYGWIN";
+  else if (strstr(msystem, "32") != NULL)
+    return "MINGW32";
+  else
+    return "MINGW64";
+#else
+  return "CYGWIN";
+#endif
+}
+
 /* uname: POSIX 4.4.1.1 */
 
 /* New entrypoint for applications since API 335 */
@@ -36,12 +54,9 @@ uname_x (struct utsname *name)
 
       memset (name, 0, sizeof (*name));
       /* sysname */
-      char* msystem = getenv("MSYSTEM");
-      const char* msystem_sysname = "MSYS";
-      if (msystem != NULL && *msystem && strcmp(msystem, "MSYS") != 0)
-        msystem_sysname = (strstr(msystem, "32") != NULL) ? "MINGW32" : "MINGW64";;
+      const char* sysname = get_sysname();
       __small_sprintf (name->sysname, "%s_%s-%u",
-		       msystem_sysname,
+		       sysname,
 		       wincap.osname (), wincap.build_number ());
       /* nodename */
       memset (buf, 0, sizeof buf);
@@ -107,15 +122,8 @@ uname (struct utsname *in_name)
   __try
     {
       memset (name, 0, sizeof (*name));
-#ifdef __MSYS__
-      char* msystem = getenv("MSYSTEM");
-      const char* msystem_sysname = "MSYS";
-      if (msystem != NULL && *msystem && strcmp(msystem, "MSYS") != 0)
-        msystem_sysname = (strstr(msystem, "32") != NULL) ? "MINGW32" : "MINGW64";
-      __small_sprintf (name->sysname, "%s_%s", msystem_sysname, wincap.osname ());
-#else
-      __small_sprintf (name->sysname, "CYGWIN_%s", wincap.osname ());
-#endif
+      const char* sysname = get_sysname();
+      __small_sprintf (name->sysname, "%s_%s", sysname, wincap.osname ());
 
       /* Computer name */
       cygwin_gethostname (name->nodename, sizeof (name->nodename) - 1);
