Backport a Cygwin fix about a startup delay for domain users.
Fixes issue [#2344].

2001-10-22  Corinna Vinschen  <corinna@vinschen.de>
Patch suggested by Ian Ray <ian.ray@nokia.com>:
* syscalls.cc (seteuid): Unset environment variables HOMEDRIVE and
HOMEPATH before calling internal_getlogin().
* uinfo.cc (internal_getlogin): Use default HOMEPATH and HOMEDRIVE
from environment if both are present, else query NetUserGetInfo().

diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 36abfc0..ddcaae8 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -2237,6 +2237,11 @@ seteuid (uid_t uid)
 	 retrieving user's SID. */
       user.token = cygheap->user.impersonated ? cygheap->user.token
 					      : INVALID_HANDLE_VALUE;
+      /* Unsetting these both env vars is necessary to get NetUserGetInfo()
+         called in internal_getlogin ().  Otherwise the wrong path is used
+         after a user switch, probably. */
+      unsetenv ("HOMEDRIVE");
+      unsetenv ("HOMEPATH");
       struct passwd *pw_cur = internal_getlogin (user);
       if (pw_cur != pw_new)
 	{
diff --git a/winsup/cygwin/uinfo.cc b/winsup/cygwin/uinfo.cc
index 80ee0e4..77caf8a 100644
--- a/winsup/cygwin/uinfo.cc
+++ b/winsup/cygwin/uinfo.cc
@@ -75,45 +75,50 @@ internal_getlogin (cygheap_user &user)
 	  user.set_logsrv (buf + 2);
 	  setenv ("LOGONSERVER", buf, 1);
 	}
-      LPUSER_INFO_3 ui = NULL;
-      WCHAR wuser[UNLEN + 1];
-      WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
-
-      /* HOMEDRIVE and HOMEPATH are wrong most of the time, too,
-	 after changing user context! */
-      sys_mbstowcs (wuser, user.name (), UNLEN + 1);
-      wlogsrv[0] = '\0';
-      if (user.logsrv ())
-	{
-	  strcat (strcpy (buf, "\\\\"), user.logsrv ());
-	  sys_mbstowcs (wlogsrv, buf, INTERNET_MAX_HOST_NAME_LENGTH + 3);
-	}
-      if (!NetUserGetInfo (NULL, wuser, 3, (LPBYTE *)&ui)
-	  || (wlogsrv[0] && !NetUserGetInfo (wlogsrv, wuser, 3,(LPBYTE *)&ui)))
-	{
-	  sys_wcstombs (buf, ui->usri3_home_dir, MAX_PATH);
-	  if (!buf[0])
-	    {
-	      sys_wcstombs (buf, ui->usri3_home_dir_drive, MAX_PATH);
-	      if (buf[0])
-		strcat (buf, "\\");
-	      else
-		{
-		  env = getenv ("SYSTEMDRIVE");
-		  if (env && *env)
-		    strcat (strcpy (buf, env), "\\");
-		  else
-		    GetSystemDirectoryA (buf, MAX_PATH);
-		}
-	    }
-	  setenv ("HOMEPATH", buf + 2, 1);
-	  buf[2] = '\0';
-	  setenv ("HOMEDRIVE", buf, 1);
-	  NetApiBufferFree (ui);
-	}
       debug_printf ("Domain: %s, Logon Server: %s, Windows Username: %s",
 		    user.domain (), user.logsrv (), user.name ());
 
+      /* NetUserGetInfo() can be slow in NT domain environment, thus we
+       * only obtain HOMEDRIVE and HOMEPATH if they are not already set
+       * in the environment. */
+      if (!getenv ("HOMEPATH") || !getenv ("HOMEDRIVE"))
+        {
+          LPUSER_INFO_3 ui = NULL;
+          WCHAR wuser[UNLEN + 1];
+          WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
+
+          sys_mbstowcs (wuser, user.name (), UNLEN + 1);
+          wlogsrv[0] = '\0';
+          if (user.logsrv ())
+            {
+              strcat (strcpy (buf, "\\\\"), user.logsrv ());
+              sys_mbstowcs (wlogsrv, buf, INTERNET_MAX_HOST_NAME_LENGTH + 3);
+            }
+          if (!NetUserGetInfo (NULL, wuser, 3, (LPBYTE *)&ui)
+              || (wlogsrv[0] && !NetUserGetInfo (wlogsrv, wuser, 3,(LPBYTE *)&ui)))
+            {
+              sys_wcstombs (buf, ui->usri3_home_dir, MAX_PATH);
+              if (!buf[0])
+                {
+                  sys_wcstombs (buf, ui->usri3_home_dir_drive, MAX_PATH);
+                  if (buf[0])
+                    strcat (buf, "\\");
+                  else
+                    {
+                      env = getenv ("SYSTEMDRIVE");
+                      if (env && *env)
+                        strcat (strcpy (buf, env), "\\");
+                      else
+                        GetSystemDirectoryA (buf, MAX_PATH);
+                    }
+                }
+              setenv ("HOMEPATH", buf + 2, 1);
+              buf[2] = '\0';
+              setenv ("HOMEDRIVE", buf, 1);
+              NetApiBufferFree (ui);
+            }
+        }
+
       if (allow_ntsec)
 	{
 	  HANDLE ptok = user.token; /* Which is INVALID_HANDLE_VALUE if no
