From 9f396787cbf94c68677d1f70d1b3d408b47c6a2e Mon Sep 17 00:00:00 2001
From: Buster <rcopley@gmail.com>
Date: Sat, 26 Oct 2019 12:15:37 +0100
Subject: [PATCH] W32: Always check $USERPROFILE if $HOME is not set

---
 gdb/auto-load.c         |  4 ++++
 gdb/main.c              |  4 ++++
 gdb/windows-nat.c       |  4 ++++
 gdbsupport/pathstuff.cc |  4 ++++
 gnulib/import/glob.c    | 18 +-----------------
 5 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/gdb/auto-load.c b/gdb/auto-load.c
index c967261925..72c37f0590 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -499,6 +499,10 @@ file_is_auto_load_safe (const char *filename, const char *debug_fmt, ...)
   if (!advice_printed)
     {
       const char *homedir = getenv ("HOME");
+#ifdef _WIN32
+      if (homedir == NULL)
+        homedir = getenv ("USERPROFILE");
+#endif
 
       if (homedir == NULL)
 	homedir = "$HOME";
diff --git a/gdb/main.c b/gdb/main.c
index 19bbb92388..6976c30763 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -302,6 +302,10 @@ get_init_files (std::vector<std::string> *system_gdbinit,
 	}
 
       const char *homedir = getenv ("HOME");
+#ifdef _WIN32
+      if (homedir == NULL)
+        homedir = getenv ("USERPROFILE");
+#endif
 
       /* If the .gdbinit file in the current directory is the same as
 	 the $HOME/.gdbinit file, it should not be sourced.  homebuf
diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index f9172c3a7f..ce2f00fd0c 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -3439,6 +3439,10 @@ _initialize_check_for_gdb_ini ()
     return;
 
   homedir = getenv ("HOME");
+#ifdef _WIN32
+  if (homedir == NULL)
+    homedir = getenv ("USERPROFILE");
+#endif
   if (homedir)
     {
       char *p;
diff --git a/gdbsupport/pathstuff.cc b/gdbsupport/pathstuff.cc
index 1f60fd0c98..eb7b3d4e95 100644
--- a/gdbsupport/pathstuff.cc
+++ b/gdbsupport/pathstuff.cc
@@ -231,6 +231,10 @@ get_standard_cache_dir ()
 #endif
 
   const char *home = getenv ("HOME");
+#ifdef _WIN32
+  if (home == NULL)
+    home = getenv ("USERPROFILE");
+#endif
   if (home != NULL)
     {
       /* Make sure the path is absolute and tilde-expanded.  */
diff --git a/gnulib/import/glob.c b/gnulib/import/glob.c
index f1b20d4869..45af4b06cc 100644
--- a/gnulib/import/glob.c
+++ b/gnulib/import/glob.c
@@ -626,23 +626,7 @@ __glob (const char *pattern, int flags, int (*errfunc) (const char *, int),
           if (home_dir == NULL || home_dir[0] == '\0')
             {
 #ifdef WINDOWS32
-              /* Windows NT defines HOMEDRIVE and HOMEPATH.  But give
-                 preference to HOME, because the user can change HOME.  */
-              const char *home_drive = getenv ("HOMEDRIVE");
-              const char *home_path = getenv ("HOMEPATH");
-
-              if (home_drive != NULL && home_path != NULL)
-                {
-                  size_t home_drive_len = strlen (home_drive);
-                  size_t home_path_len = strlen (home_path);
-                  char *mem = alloca (home_drive_len + home_path_len + 1);
-
-                  memcpy (mem, home_drive, home_drive_len);
-                  memcpy (mem + home_drive_len, home_path, home_path_len + 1);
-                  home_dir = mem;
-                }
-              else
-                home_dir = "c:/users/default"; /* poor default */
+              home_dir = getenv ("USERPROFILE");
 #else
               int err;
               struct passwd *p;
-- 
2.29.2.windows.1

