From a9a3fa7d3f1d0a1ebdac43d783e729141d00f84d Mon Sep 17 00:00:00 2001 From: Richard Frith-MacDonald Date: Sun, 13 Feb 2011 18:54:53 +0000 Subject: [PATCH] tweak full user name git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@32151 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 +++++ Source/NSPathUtilities.m | 33 ++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index e0c380451..d5016ad62 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2011-02-13 Richard Frith-Macdonald + + * Source/NSPathUtilities.m: NSFullUserName() ... use short name if + no long one is available. + 2011-02-13 Philippe Roussel * Source/NSPointerArray.m: replace memcpy with memmove for safety. diff --git a/Source/NSPathUtilities.m b/Source/NSPathUtilities.m index a44a167f4..b3bb7b681 100644 --- a/Source/NSPathUtilities.m +++ b/Source/NSPathUtilities.m @@ -1627,15 +1627,21 @@ NSFullUserName(void) { if (theFullUserName == nil) { - NSString *userName = nil; + NSString *userName = NSUserName(); #if defined(__MINGW__) struct _USER_INFO_2 *userInfo; - if (NetUserGetInfo(NULL, (unichar*)[NSUserName() cStringUsingEncoding: + if (NetUserGetInfo(NULL, (unichar*)[userName cStringUsingEncoding: NSUnicodeStringEncoding], 2, (LPBYTE*)&userInfo) == 0) { - userName = [NSString stringWithCharacters: userInfo->usri2_full_name - length: wcslen(userInfo->usri2_full_name)]; + int length = wcslen(userInfo->usri2_full_name); + + if (length > 0) + { + userName = [NSString + stringWithCharacters: userInfo->usri2_full_name + length: length]; + } } #else #ifdef HAVE_PWD_H @@ -1644,27 +1650,28 @@ NSFullUserName(void) struct passwd *p; char buf[BUFSIZ*10]; - if (getpwnam_r([NSUserName() cString], &pw, buf, sizeof(buf), &p) == 0) + if (getpwnam_r([userName cString], &pw, buf, sizeof(buf), &p) == 0) { - userName = [NSString stringWithUTF8String: pw.pw_gecos]; + if (*pw.pw_gecos) + { + userName = [NSString stringWithUTF8String: pw.pw_gecos]; + } } #else #if defined(HAVE_GETPWNAM) struct passwd *pw; [gnustep_global_lock lock]; - pw = getpwnam([NSUserName() cString]); - userName = [NSString stringWithUTF8String: pw->pw_gecos]; + pw = getpwnam([userName cString]); + if (*pw.pw_gecos) + { + userName = [NSString stringWithUTF8String: pw->pw_gecos]; + } [gnustep_global_lock lock]; #endif /* HAVE_GETPWNAM */ #endif /* HAVE_GETPWNAM_R */ #endif /* HAVE_PWD_H */ #endif /* defined(__Win32__) else */ - if (userName == nil) - { - NSLog(@"Warning: NSFullUserName not implemented\n"); - userName = NSUserName(); - } ASSIGN(theFullUserName, userName); } return theFullUserName;