Complete.Org: Mailing Lists: Archives: freeciv-dev: August 1999:
[Freeciv-Dev] patch: getting username (PR#109)
Home

[Freeciv-Dev] patch: getting username (PR#109)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Cc: bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] patch: getting username (PR#109)
From: David Pfitzner <dwp@xxxxxxxxxxxxxx>
Date: Mon, 16 Aug 1999 05:34:33 -0700 (PDT)

This patch moves the check for the user's username into 
a function user_username() in shared.c.  It also removes
the "please report this" note if getpwuid() fails, and
also tries getenv $USER (actually, now before getpwuid()).

(Also small reorder in gui-gtk to call user_username()
only after log_init().)

Questions:
- would it be useful to check for getpwuid() in configure?  
  (Eg, on Win/Mac?)
- should $USER be checked before or after getpwuid()?
- should getpwuid() also be used in user_home_dir(), 
  and if so, before or after $HOME?
- should we check eg $FREECIV_USER to allow user to set 
  prefered freeciv username?  (Or put preferred username 
  in .civclientrc, which incidently should probably be
  moved to ~/.freeciv/clientrc since we now use ~/.freeciv
  for data path.)

Regards,
-- David
diff -u -r --exclude-from exclude freeciv-cvs/client/gui-gtk/gui_main.c 
freeciv-mod/client/gui-gtk/gui_main.c
--- freeciv-cvs/client/gui-gtk/gui_main.c       Sat Aug 14 13:04:55 1999
+++ freeciv-mod/client/gui-gtk/gui_main.c       Mon Aug 16 17:42:02 1999
@@ -19,9 +19,6 @@
 #include <string.h>
 #include <time.h>
 
-#ifdef HAVE_PWD_H
-#include <pwd.h>
-#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -190,23 +187,9 @@
 
   /* set default argument values */
   loglevel=LOG_NORMAL;
-
-  {
-    struct passwd *password=getpwuid(getuid());
-
-    if (password)
-      strcpy(name, password->pw_name);
-    else {
-      /* freelog not yet initialised here --dwp */
-      fprintf(stderr, "Your getpwuid call failed.  Please report this.");
-      strcpy(name, "operator 00000");
-      sprintf(name+9, "%05i",(int)getuid());
-   }
-  }
-
   server_port=DEFAULT_SOCK_PORT;
-
   strcpy(server_host, "localhost");
+  name[0] = '\0';
 
   i = 1;
   while (i < argc)
@@ -242,6 +225,11 @@
   }
 
   log_init(logfile, loglevel, NULL);
+
+  /* after log_init: */
+  if (name[0] == '\0') {
+    strcpy(name, user_username());
+  }
 }
 
 
diff -u -r --exclude-from exclude freeciv-cvs/client/gui-xaw/gui_main.c 
freeciv-mod/client/gui-xaw/gui_main.c
--- freeciv-cvs/client/gui-xaw/gui_main.c       Sat Aug 14 13:04:55 1999
+++ freeciv-mod/client/gui-xaw/gui_main.c       Mon Aug 16 17:42:02 1999
@@ -18,10 +18,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#ifdef HAVE_PWD_H
-#include <pwd.h>
-#endif
-
 #include <X11/Intrinsic.h>
 #include <X11/StringDefs.h>
 
@@ -333,17 +329,8 @@
 
   if(appResources.name) {
     strcpy(name, appResources.name);
-  }
-  else {
-    struct passwd *password;
-    password=getpwuid(getuid());
-    if (password)
-      strcpy(name, password->pw_name);
-    else {
-      freelog(LOG_NORMAL, "Your getpwuid call failed.  Please report this.");
-      strcpy(name, "operator 00000");
-      sprintf(name+9, "%05i", (int)getuid());
-    }
+  } else {
+    strcpy(name, user_username());
   }
 
   if(appResources.server)
diff -u -r --exclude-from exclude freeciv-cvs/common/shared.c 
freeciv-mod/common/shared.c
--- freeciv-cvs/common/shared.c Fri Aug 13 21:18:30 1999
+++ freeciv-mod/common/shared.c Mon Aug 16 17:42:35 1999
@@ -23,6 +23,10 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
+
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
@@ -490,6 +494,42 @@
     init = 1;
   }
   return home_dir;
+}
+
+/***************************************************************************
+  Returns string which gives user's username, as specified by $USER or
+  as given in password file for this user's uid, or a made up name if
+  we can't get either of the above. 
+  Gets value once, and then caches result.
+  Note the caller should not mess with returned string.
+***************************************************************************/
+char *user_username(void)
+{
+  static char *username = NULL;          /* allocated once, never free()d */
+
+  if (username)
+    return username;
+
+  {
+    char *env = getenv("USER");
+    if (env) {
+      username = mystrdup(env);
+      freelog(LOG_VERBOSE, "USER username is %s", username);
+      return username;
+    }
+  }
+  {
+    struct passwd *pwent = getpwuid(getuid());
+    if (pwent) {
+      username = mystrdup(pwent->pw_name);
+      freelog(LOG_VERBOSE, "getpwuid username is %s", username);
+      return username;
+    }
+  }
+  username = fc_malloc(30);
+  sprintf(username, "name%d", (int)getuid());
+  freelog(LOG_VERBOSE, "fake username is %s", username);
+  return username;
 }
 
 /***************************************************************************
diff -u -r --exclude-from exclude freeciv-cvs/common/shared.h 
freeciv-mod/common/shared.h
--- freeciv-cvs/common/shared.h Tue Aug 10 23:08:58 1999
+++ freeciv-mod/common/shared.h Mon Aug 16 17:42:03 1999
@@ -67,6 +67,7 @@
 int wordwrap_string(char *s, int len);
 
 char *user_home_dir(void);
+char *user_username(void);
 char *datafilename(char *filename);
 char *datafilename_required(char *filename);
 

[Prev in Thread] Current Thread [Next in Thread]