Tcl Source Code

Artifact [bd869ae4b7]
Login

Artifact bd869ae4b7ecc8fab59582f3d32d1ae1838effea:

Attachment "user.diff" to ticket [681877ffff] added by dkf 2004-09-10 22:04:44.
Index: unix/tclUnixInit.c
===================================================================
RCS file: /cvsroot/tcl/tcl/unix/tclUnixInit.c,v
retrieving revision 1.47
diff -u -r1.47 tclUnixInit.c
--- unix/tclUnixInit.c	23 Jun 2004 03:49:59 -0000	1.47
+++ unix/tclUnixInit.c	10 Sep 2004 15:03:41 -0000
@@ -30,6 +30,9 @@
 #   endif
 #endif
 
+#include <pwd.h>
+#include <sys/types.h>
+
 /*
  * Define this if you want to revert to the old behavior of
  * never checking the stack.
@@ -805,13 +808,13 @@
     int unameOK;
     CONST char *user;
     Tcl_DString ds;
+    struct passwd *pwPtr;
 
 #ifdef HAVE_CFBUNDLE
     char tclLibPath[MAXPATHLEN + 1];
 
     if (MacOSXGetLibraryPath(interp, MAXPATHLEN, tclLibPath) == TCL_OK) {
         CONST char *str;
-        Tcl_DString ds;
         CFBundleRef bundleRef;
 
         Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath,
@@ -929,20 +932,30 @@
     }
 
     /*
-     * Copy USER or LOGNAME environment variable into tcl_platform(user)
+     * Find out what the user's real username is and copy it
+     * tcl_platform(user).
      */
 
-    Tcl_DStringInit(&ds);
-    user = TclGetEnv("USER", &ds);
-    if (user == NULL) {
-	user = TclGetEnv("LOGNAME", &ds);
+    pwPtr = getpwuid(getuid());
+    if (pwPtr != NULL) {
+	user = Tcl_ExternalToUtfDString(NULL, pwPtr->pw_name, -1, &ds);
+    } else {
+	/*
+	 * Fall back to copying USER or LOGNAME environment variable
+	 * into tcl_platform(user)
+	 */
+
+	Tcl_DStringInit(&ds);
+	user = TclGetEnv("USER", &ds);
 	if (user == NULL) {
-	    user = "";
+	    user = TclGetEnv("LOGNAME", &ds);
+	    if (user == NULL) {
+		user = "";
+	    }
 	}
     }
     Tcl_SetVar2(interp, "tcl_platform", "user", user, TCL_GLOBAL_ONLY);
     Tcl_DStringFree(&ds);
-
 }
 
 /*