? NSK1KENNYKB03
? generic/.new.regc_nfa.c
Index: unix/tclUnixSock.c
===================================================================
RCS file: /cvsroot/tcl/tcl/unix/tclUnixSock.c,v
retrieving revision 1.11
diff -u -r1.11 tclUnixSock.c
--- unix/tclUnixSock.c	10 May 2005 18:35:29 -0000	1.11
+++ unix/tclUnixSock.c	10 Jul 2005 02:07:59 -0000
@@ -14,72 +14,39 @@
 #include "tclInt.h"
 
 /*
- * There is no portable macro for the maximum length
- * of host names returned by gethostbyname().  We should only
- * trust SYS_NMLN if it is at least 255 + 1 bytes to comply with DNS
- * host name limits.
- *
- * Note:  SYS_NMLN is a restriction on "uname" not on gethostbyname!
- *
- * For example HP-UX 10.20 has SYS_NMLN == 9,  while gethostbyname()
- * can return a fully qualified name from DNS of up to 255 bytes.
- *
- * Fix suggested by Viktor Dukhovni (viktor@esm.com)
- */
-
-#if defined(SYS_NMLN) && SYS_NMLEN >= 256
-#define TCL_HOSTNAME_LEN SYS_NMLEN
-#else
-#define TCL_HOSTNAME_LEN 256
-#endif
-
-
-/*
  * The following variable holds the network name of this host.
  */
 
-static char hostname[TCL_HOSTNAME_LEN + 1];
-static int  hostnameInited = 0;
-TCL_DECLARE_MUTEX(hostMutex)
+static TclInitProcessGlobalValueProc	InitializeHostName;
+static ProcessGlobalValue hostName =
+	{0, 0, NULL, NULL, InitializeHostName, NULL, NULL};
 
 
 /*
  *----------------------------------------------------------------------
  *
- * Tcl_GetHostName --
+ * InitializeHostName --
  *
- *	Returns the name of the local host.
+ * 	This routine sets the process global value of the name of
+ * 	the local host on which the process is running.
  *
  * Results:
- *	A string containing the network name for this machine, or
- *	an empty string if we can't figure out the name.  The caller 
- *	must not modify or free this string.
- *
- * Side effects:
  *	None.
  *
  *----------------------------------------------------------------------
  */
 
-CONST char *
-Tcl_GetHostName()
+void
+InitializeHostName(valuePtr, lengthPtr, encodingPtr)
+    char **valuePtr;
+    int *lengthPtr;
+    Tcl_Encoding *encodingPtr;
 {
+    CONST char *native = NULL;
+
 #ifndef NO_UNAME
     struct utsname u;
     struct hostent *hp;
-#else
-    char buffer[sizeof(hostname)];
-#endif
-    CONST char *native;
-
-    Tcl_MutexLock(&hostMutex);
-    if (hostnameInited) {
-	Tcl_MutexUnlock(&hostMutex);
-        return hostname;
-    }
-
-    native = NULL;
-#ifndef NO_UNAME
     (VOID *) memset((VOID *) &u, (int) 0, sizeof(struct utsname));
     if (uname(&u) > -1) {				/* INTL: Native. */
         hp = gethostbyname(u.nodename);			/* INTL: Native. */
@@ -104,25 +71,64 @@
 	    native = u.nodename;
         }
     }
+    if (native == NULL) {
+	native = tclEmptyStringRep;
+    }
 #else
     /*
      * Uname doesn't exist; try gethostname instead.
+     *
+     * There is no portable macro for the maximum length
+     * of host names returned by gethostbyname().  We should only
+     * trust SYS_NMLN if it is at least 255 + 1 bytes to comply with DNS
+     * host name limits.
+     *
+     * Note:  SYS_NMLN is a restriction on "uname" not on gethostbyname!
+     *
+     * For example HP-UX 10.20 has SYS_NMLN == 9,  while gethostbyname()
+     * can return a fully qualified name from DNS of up to 255 bytes.
+     *
+     * Fix suggested by Viktor Dukhovni (viktor@esm.com)
      */
+#    if defined(SYS_NMLN) && SYS_NMLEN >= 256
+    char buffer[SYS_NMLEN];
+#    else
+    char buffer[256];
+#    endif
 
     if (gethostname(buffer, sizeof(buffer)) > -1) {	/* INTL: Native. */
 	native = buffer;
     }
 #endif
 
-    if (native == NULL) {
-	hostname[0] = 0;
-    } else {
-	Tcl_ExternalToUtf(NULL, NULL, native, -1, 0, NULL, hostname,
-		sizeof(hostname), NULL, NULL, NULL);
-    }
-    hostnameInited = 1;
-    Tcl_MutexUnlock(&hostMutex);
-    return hostname;
+    *encodingPtr = Tcl_GetEncoding(NULL, NULL);
+    *lengthPtr = strlen(native);
+    *valuePtr = ckalloc((unsigned int) (*lengthPtr)+1);
+    memcpy((VOID *) *valuePtr, (VOID *) native, (size_t)(*lengthPtr)+1);
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * Tcl_GetHostName --
+ *
+ *	Returns the name of the local host.
+ *
+ * Results:
+ *	A string containing the network name for this machine, or
+ *	an empty string if we can't figure out the name.  The caller 
+ *	must not modify or free this string.
+ *
+ * Side effects:
+ *	Caches the name to return for future calls.
+ *
+ *----------------------------------------------------------------------
+ */
+
+CONST char *
+Tcl_GetHostName()
+{
+    return Tcl_GetString(TclGetProcessGlobalValue(&hostName));
 }
 
 /*
Index: win/tclWinSock.c
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinSock.c,v
retrieving revision 1.46
diff -u -r1.46 tclWinSock.c
--- win/tclWinSock.c	10 May 2005 18:35:41 -0000	1.46
+++ win/tclWinSock.c	10 Jul 2005 02:08:01 -0000
@@ -28,13 +28,15 @@
  */
 
 static int initialized = 0;
-
-static int  hostnameInitialized = 0;
-static char hostname[255];	/* This buffer should be big enough for
-                                 * hostname plus domain name. */
-
 TCL_DECLARE_MUTEX(socketMutex)
 
+/*
+ * The following variable holds the network name of this host.
+ */
+
+static TclInitProcessGlobalValueProc    InitializeHostName;
+static ProcessGlobalValue hostName =
+	{0, 0, NULL, NULL, InitializeHostName, NULL, NULL};
 
 /*
  * Mingw, Cygwin and OpenWatcom may not have LPFN_* typedefs.
@@ -598,7 +600,6 @@
 	winSock.hModule = NULL;
     }
     initialized = 0;
-    hostnameInitialized = 0;
     Tcl_MutexUnlock(&socketMutex);
 }
 
@@ -2520,12 +2521,11 @@
  *	Returns the name of the local host.
  *
  * Results:
- *	A string containing the network name for this machine, or
- *	an empty string if we can't figure out the name.  The caller 
- *	must not modify or free this string.
+ *	A string containing the network name for this machine.
+ *	The caller must not modify or free this string.
  *
  * Side effects:
- *	None.
+ *	Caches the name to return for future calls.
  *
  *----------------------------------------------------------------------
  */
@@ -2533,49 +2533,58 @@
 CONST char *
 Tcl_GetHostName()
 {
-    DWORD length;
-    WCHAR wbuf[MAX_COMPUTERNAME_LENGTH + 1];
-
-    Tcl_MutexLock(&socketMutex);
-    InitSockets();
+    return Tcl_GetString(TclGetProcessGlobalValue(&hostName));
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * InitializeHostName --
+ *
+ *      This routine sets the process global value of the name of
+ *      the local host on which the process is running.
+ *
+ * Results:
+ *      None.
+ *
+ *----------------------------------------------------------------------
+ */
 
-    if (hostnameInitialized) {
-	Tcl_MutexUnlock(&socketMutex);
-        return hostname;
-    }
-    Tcl_MutexUnlock(&socketMutex);
-	
-    if (TclpHasSockets(NULL) == TCL_OK) {
-	/*
-	 * INTL: bug
-	 */
+void
+InitializeHostName(valuePtr, lengthPtr, encodingPtr)
+    char **valuePtr;
+    int *lengthPtr;
+    Tcl_Encoding *encodingPtr;
+{
+    WCHAR wbuf[MAX_COMPUTERNAME_LENGTH + 1];
+    DWORD length = sizeof(wbuf) / sizeof(WCHAR);
+    Tcl_DString ds;
 
-	if (winSock.gethostname(hostname, sizeof(hostname)) == 0) {
-	    Tcl_MutexLock(&socketMutex);
-	    hostnameInitialized = 1;
-	    Tcl_MutexUnlock(&socketMutex);
-	    return hostname;
-	}
-    }
-    Tcl_MutexLock(&socketMutex);
-    length = sizeof(hostname);
     if ((*tclWinProcs->getComputerNameProc)(wbuf, &length) != 0) {
 	/*
 	 * Convert string from native to UTF then change to lowercase.
 	 */
-
-	Tcl_DString ds;
-
-	lstrcpynA(hostname, Tcl_WinTCharToUtf((TCHAR *) wbuf, -1, &ds),
-		sizeof(hostname));
-	Tcl_DStringFree(&ds);
-	Tcl_UtfToLower(hostname);
-    } else {
-	hostname[0] = '\0';
-    }
-    hostnameInitialized = 1;
-    Tcl_MutexUnlock(&socketMutex);
-    return hostname;
+	Tcl_UtfToLower(Tcl_WinTCharToUtf((TCHAR *) wbuf, -1, &ds));
+    } else if (TclpHasSockets(NULL) == TCL_OK) {
+	/*
+	 * Buffer length of 255 copied slavishly from previous version
+	 * of this routine.  Presumably there's a more "correct" macro
+	 * value for a properly sized buffer for a gethostname() call.
+	 * Maintainers are welcome to supply it.
+	 */
+	Tcl_DStringInit(&ds);
+	Tcl_DStringSetLength(&ds, 255);
+	if (winSock.gethostname(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds))
+		== 0) {
+	    Tcl_DStringSetLength(&ds, 0);
+	}
+    }
+    *encodingPtr = Tcl_GetEncoding(NULL, "utf-8");
+    *lengthPtr = Tcl_DStringLength(&ds);
+    *valuePtr = ckalloc((unsigned int) (*lengthPtr)+1);
+    memcpy((VOID *) *valuePtr, (VOID *) Tcl_DStringValue(&ds),
+	    (size_t)(*lengthPtr)+1);
+    Tcl_DStringFree(&ds);
 }
 
 /*
