--- kinit/wrapper.c.sav	2006-02-06 19:33:52.000000000 +0100
+++ kinit/wrapper.c	2006-02-06 19:35:06.000000000 +0100
@@ -164,7 +164,9 @@
      sock_file[strlen(sock_file)-1] = 0;
   
   strncat(sock_file, "/socket-", MAX_SOCK_FILE - strlen(sock_file));
-  if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
+  if( getenv("XAUTHLOCALHOSTNAME"))
+      strncat(sock_file, getenv("XAUTHLOCALHOSTNAME"), MAX_SOCK_FILE - strlen(sock_file) - 1);
+  else if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
   {
      perror("Warning: Could not determine hostname: ");
      return -1;
--- kinit/lnusertemp.c.sav	2006-02-06 19:33:52.000000000 +0100
+++ kinit/lnusertemp.c	2006-02-06 19:35:06.000000000 +0100
@@ -39,7 +39,7 @@
 
 int check_tmp_dir(const char *tmp_dir);
 int create_link(const char *file, const char *tmp_dir);
-int build_link(const char *tmp_prefix, const char *kde_prefix);
+int build_link(const char *tmp_prefix, const char *kde_prefix, int kdehostname);
 
 int check_tmp_dir(const char *tmp_dir)
 {
@@ -89,7 +89,7 @@
 }
 
 
-int build_link(const char *tmp_prefix, const char *kde_prefix)
+int build_link(const char *tmp_prefix, const char *kde_prefix, int kdehostname)
 {
   struct passwd *pw_ent;
   char kde_tmp_dir[PATH_MAX+1];
@@ -156,10 +156,21 @@
   }  
 
   strncat(kde_tmp_dir, kde_prefix, PATH_MAX - strlen(kde_tmp_dir));
-  if (gethostname(kde_tmp_dir+strlen(kde_tmp_dir), PATH_MAX - strlen(kde_tmp_dir) - 1) != 0)
+
+  if( kdehostname )
+  {
+      if( getenv("XAUTHLOCALHOSTNAME"))
+          strncat(kde_tmp_dir+strlen(kde_tmp_dir), getenv("XAUTHLOCALHOSTNAME"), PATH_MAX - strlen(kde_tmp_dir) - 1);
+      else
+          return 0;
+  }
+  else
   {
-     perror("Aborting. Could not determine hostname: ");
-     exit(255);
+     if (gethostname(kde_tmp_dir+strlen(kde_tmp_dir), PATH_MAX - strlen(kde_tmp_dir) - 1) != 0)
+     {
+        perror("Could not determine hostname: ");
+        return 1;
+     }
   }
   kde_tmp_dir[sizeof(kde_tmp_dir)-1] = '\0';
 
@@ -266,7 +277,9 @@
     kde_prefix = "/cache-"; 
   }
 
-  res = build_link(tmp_prefix, kde_prefix); 
+  res = build_link(tmp_prefix, kde_prefix, 1); 
+  if( build_link(tmp_prefix, kde_prefix, 0))
+    res = 1;
     
   free(tmp_prefix);
 
--- kdecore/kstandarddirs.cpp.sav	2006-02-06 19:33:52.000000000 +0100
+++ kdecore/kstandarddirs.cpp	2006-02-06 19:35:06.000000000 +0100
@@ -718,7 +718,10 @@
 {
    char hostname[256];
    hostname[0] = 0;
-   gethostname(hostname, 255);
+   if( getenv("XAUTHLOCALHOSTNAME"))
+       strlcpy(hostname, getenv("XAUTHLOCALHOSTNAME"), 255 );
+   else
+       gethostname(hostname, 255);
    QString dir = QString("%1%2-%3").arg(localkdedir()).arg(type).arg(hostname);
    char link[1024];
    link[1023] = 0;
--- kdecore/kcrash.cpp.sav	2006-02-06 19:33:52.000000000 +0100
+++ kdecore/kcrash.cpp	2006-02-06 19:35:06.000000000 +0100
@@ -461,7 +461,9 @@
      sock_file[strlen(sock_file)-1] = 0;
   
   strncat(sock_file, "/socket-", MAX_SOCK_FILE - strlen(sock_file));
-  if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
+  if( getenv("XAUTHLOCALHOSTNAME"))
+      strncat(sock_file, getenv("XAUTHLOCALHOSTNAME"), MAX_SOCK_FILE - strlen(sock_file) - 1);
+  else if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
   {
      perror("Warning: Could not determine hostname: ");
      return -1;
--- dcop/dcopserver_shutdown.c.sav	2006-02-06 19:33:52.000000000 +0100
+++ dcop/dcopserver_shutdown.c	2006-02-06 19:35:06.000000000 +0100
@@ -95,7 +95,9 @@
   strncat(dcop_file, "/.DCOPserver_", n);
   n -= strlen("/.DCOPserver_");
 
-  if (gethostname(dcop_file+strlen(dcop_file), n) != 0)
+  if (getenv("XAUTHLOCALHOSTNAME"))
+     strncat(dcop_file+strlen(dcop_file), getenv("XAUTHLOCALHOSTNAME"), n);
+  else if (gethostname(dcop_file+strlen(dcop_file), n) != 0)
   {
      perror("Error. Could not determine hostname: ");
      dcop_file[0] = '\0';
--- dcop/dcopc.c.sav	2006-02-06 19:33:52.000000000 +0100
+++ dcop/dcopc.c	2006-02-06 19:35:06.000000000 +0100
@@ -684,7 +684,9 @@
     }
 
     hostName[0] = '\0';
-    if (gethostname(hostName, sizeof(hostName)))
+    if (getenv("XAUTHLOCALHOSTNAME"))
+            strlcpy(hostName, getenv("XAUTHLOCALHOSTNAME"),sizeof(hostName)-1);
+    else if (gethostname(hostName, sizeof(hostName)))
 	    strcpy(hostName, "localhost");
     else
 	    hostName[sizeof(hostName)-1] = '\0';
--- dcop/dcopclient.cpp.sav	2006-02-06 19:33:52.000000000 +0100
+++ dcop/dcopclient.cpp	2006-02-06 19:35:06.000000000 +0100
@@ -275,7 +275,9 @@
     {
         char hostName[256];
         hostName[0] = '\0';
-        if (gethostname(hostName, sizeof(hostName)))
+        if (getenv("XAUTHLOCALHOSTNAME"))
+            fName += getenv("XAUTHLOCALHOSTNAME");
+        else if (gethostname(hostName, sizeof(hostName)))
         {
             fName += "localhost";
         }
Index: dcop/KDE-ICE/Xtranssock.c
===================================================================
--- dcop/KDE-ICE/Xtranssock.c	(revision 523215)
+++ dcop/KDE-ICE/Xtranssock.c	(working copy)
@@ -1444,6 +1444,7 @@ UnixHostReallyLocal (char *host)
 
 {
     char hostnamebuf[256];
+    char* xauthlocalname = getenv("XAUTHLOCALHOSTNAME");
 
     TRANS(GetHostname) (hostnamebuf, sizeof (hostnamebuf));
 
@@ -1451,6 +1452,10 @@ UnixHostReallyLocal (char *host)
     {
 	return (1);
     }
+    else if(xauthlocalname && strcmp (xauthlocalname, host) == 0)
+    {
+       return (1);
+    }
     else
     {
 	/*
--- kded/khostname.cpp	(revision 523215)
+++ kded/khostname.cpp	(working copy)
@@ -111,7 +111,8 @@ static QCStringList split(const QCString
 
 void KHostName::changeX()
 {
-   QString cmd = "xauth list";
+   const char* xauthlocalhostname = getenv("XAUTHLOCALHOSTNAME");
+   QString cmd = "xauth -n list";
    FILE *xFile = popen(QFile::encodeName(cmd), "r");
    if (!xFile)
    {
@@ -123,6 +124,7 @@ void KHostName::changeX()
       char buf[1024+1];
       while (!feof(xFile))
       {
+         buf[1024]='\0';
          QCString line = fgets(buf, 1024, xFile);
          if (line.length())
             line.truncate(line.length()-1); // Strip LF.
@@ -159,12 +159,17 @@
       QCString newNetId = newName+netId.mid(i);
       QCString oldNetId = netId.left(i);

-      if(oldNetId != oldName)
-         continue;
+      if(oldNetId != oldName
+        && (!xauthlocalhostname || strcmp(xauthlocalhostname, oldNetId.data()) != 0))
+        continue;

-      cmd = "xauth remove "+KProcess::quote(netId);
-      system(QFile::encodeName(cmd));
-      cmd = "xauth add ";
+      // don't nuke the xauth when XAUTHLOCALHOSTNAME points to it
+      if (!xauthlocalhostname || oldNetId != xauthlocalhostname)
+      {
+        cmd = "xauth -n remove "+KProcess::quote(netId);
+        system(QFile::encodeName(cmd));
+      }
+      cmd = "xauth -n add ";
       cmd += KProcess::quote(newNetId);
       cmd += " ";
       cmd += KProcess::quote(authName);
@@ -272,7 +283,10 @@ void KHostName::changeDcop()
       }
    }
 
-   // Remove old entries
+   // Remove old entries, but only if XAUTHLOCALHOSTNAME doesn't point
+   // to it
+   char* xauthlocalhostname = getenv("XAUTHLOCALHOSTNAME");
+   if (!xauthlocalhostname || !oldNetId.contains(xauthlocalhostname))
    {
       QString cmd = "iceauth remove "+KProcess::quote("netid="+oldNetId);
       system(QFile::encodeName(cmd));
@@ -364,9 +378,7 @@ int main(int argc, char **argv)
 
    KHostName hn;
 
-   if(!getenv("XAUTHLOCALHOSTNAME"))
-       hn.changeX();
-
+   hn.changeX();
    hn.changeDcop();
    hn.changeStdDirs("socket");
    hn.changeStdDirs("tmp");
