From 3ad0023f591b1c912bddc402f5667bfffe8c7b8e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Karsten=20Ball=C3=BCder?= Date: Sun, 4 Oct 1998 14:30:15 +0000 Subject: [PATCH] wxGetUserName() now returns only the user name, not the comments wxGetHostName() returns host.domain now git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@801 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/gtk/utilsgtk.cpp | 29 ++++++++++++++++++++++++----- src/gtk1/utilsgtk.cpp | 29 ++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/gtk/utilsgtk.cpp b/src/gtk/utilsgtk.cpp index 0199458470..0f9aa9547a 100644 --- a/src/gtk/utilsgtk.cpp +++ b/src/gtk/utilsgtk.cpp @@ -120,15 +120,31 @@ bool wxGetHostName(char *buf, int sz) { *buf = '\0'; #if defined(__SVR4__) && !defined(__sgi) + //KB: does this return the fully qualified host.domain name? return (sysinfo(SI_HOSTNAME, buf, sz) != -1); #else /* BSD Sockets */ - char name[255]; - struct hostent *h; + char name[255], domain[255]; + //struct hostent *h; // Get hostname if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1) return FALSE; + if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1) + return FALSE; // Get official full name of host - strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1); + // doesn't return the full qualified name, replaced by following + // code (KB) + // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1); + if((unsigned)sz > strlen(name)+strlen(domain)+1) + { + strcpy(buf, name); + if(strcmp(domain,"(none)") == 0) // standalone machine + { + strcat(buf,"."); + strcat(buf,domain); + } + } + else + return FALSE; return TRUE; #endif } @@ -148,10 +164,13 @@ bool wxGetUserId(char *buf, int sz) bool wxGetUserName(char *buf, int sz) { struct passwd *who; - + char *comma; + *buf = '\0'; if ((who = getpwuid (getuid ())) != NULL) { - strncpy (buf, who->pw_gecos, sz - 1); + comma = strchr(who->pw_gecos,'c'); + if(comma) *comma = '\0'; // cut off non-name comment fields + strncpy (buf, who->pw_gecos, sz - 1); return TRUE; } return FALSE; diff --git a/src/gtk1/utilsgtk.cpp b/src/gtk1/utilsgtk.cpp index 0199458470..0f9aa9547a 100644 --- a/src/gtk1/utilsgtk.cpp +++ b/src/gtk1/utilsgtk.cpp @@ -120,15 +120,31 @@ bool wxGetHostName(char *buf, int sz) { *buf = '\0'; #if defined(__SVR4__) && !defined(__sgi) + //KB: does this return the fully qualified host.domain name? return (sysinfo(SI_HOSTNAME, buf, sz) != -1); #else /* BSD Sockets */ - char name[255]; - struct hostent *h; + char name[255], domain[255]; + //struct hostent *h; // Get hostname if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1) return FALSE; + if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1) + return FALSE; // Get official full name of host - strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1); + // doesn't return the full qualified name, replaced by following + // code (KB) + // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1); + if((unsigned)sz > strlen(name)+strlen(domain)+1) + { + strcpy(buf, name); + if(strcmp(domain,"(none)") == 0) // standalone machine + { + strcat(buf,"."); + strcat(buf,domain); + } + } + else + return FALSE; return TRUE; #endif } @@ -148,10 +164,13 @@ bool wxGetUserId(char *buf, int sz) bool wxGetUserName(char *buf, int sz) { struct passwd *who; - + char *comma; + *buf = '\0'; if ((who = getpwuid (getuid ())) != NULL) { - strncpy (buf, who->pw_gecos, sz - 1); + comma = strchr(who->pw_gecos,'c'); + if(comma) *comma = '\0'; // cut off non-name comment fields + strncpy (buf, who->pw_gecos, sz - 1); return TRUE; } return FALSE; -- 2.45.2