From 5f9f21d16f83d5e13d60f76b5ff898cef6d64426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Sun, 11 Feb 2018 20:17:53 +0100 Subject: [PATCH 1/3] Added userName and fullUserName to ProcessInfo Marked NSFullUserName() as Unimplemented --- Foundation/NSPathUtilities.swift | 4 ++++ Foundation/ProcessInfo.swift | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/Foundation/NSPathUtilities.swift b/Foundation/NSPathUtilities.swift index 36632b404b..e96baf3c20 100755 --- a/Foundation/NSPathUtilities.swift +++ b/Foundation/NSPathUtilities.swift @@ -588,6 +588,10 @@ public func NSUserName() -> String { return userName._swiftObject } +public func NSFullUserName() -> String { + NSUnimplemented() +} + internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, String) { let template = "." + filePath + ".tmp.XXXXXX" let maxLength = Int(PATH_MAX) + 1 diff --git a/Foundation/ProcessInfo.swift b/Foundation/ProcessInfo.swift index f7e5dcac8f..af3f9aac15 100644 --- a/Foundation/ProcessInfo.swift +++ b/Foundation/ProcessInfo.swift @@ -140,4 +140,12 @@ open class ProcessInfo: NSObject { open var systemUptime: TimeInterval { return CFGetSystemUptime() } + + open var userName: String { + return NSUserName() + } + + open var fullUserName: String { + return NSFullUserName() + } } From 35b3f36cacd82ba4936a59edb3ee105646941053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Mon, 12 Feb 2018 17:59:53 +0100 Subject: [PATCH 2/3] Added implementation of NSFullUserName and CFCopyFullUserName CFCopyFullUserName implemented for POSIX OS. TODO: Add Windows implementation. --- CoreFoundation/Base.subproj/CFPlatform.c | 18 ++++++++++++++++++ CoreFoundation/Base.subproj/CFPriv.h | 3 +++ Foundation/FileManager.swift | 2 +- Foundation/NSPathUtilities.swift | 3 ++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CoreFoundation/Base.subproj/CFPlatform.c b/CoreFoundation/Base.subproj/CFPlatform.c index 9b74d07e82..6e7a2a56d8 100644 --- a/CoreFoundation/Base.subproj/CFPlatform.c +++ b/CoreFoundation/Base.subproj/CFPlatform.c @@ -289,6 +289,24 @@ CF_EXPORT CFStringRef CFCopyUserName(void) { return result; } +CF_EXPORT CFStringRef CFCopyFullUserName(void) { + CFStringRef result = NULL; +#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD + uid_t euid; + __CFGetUGIDs(&euid, NULL); + struct passwd *upwd = getpwuid(euid ? euid : getuid()); + if (upwd && upwd->pw_gecos) { + result = CFStringCreateWithCString(kCFAllocatorSystemDefault, upwd->pw_gecos, kCFPlatformInterfaceStringEncoding); + } +#else +#error Don't know how to compute full user name on this platform +#endif + if (!result) + result = (CFStringRef)CFRetain(CFSTR("")); + + return result; +} + CFURLRef CFCopyHomeDirectoryURL(void) { #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD return _CFCopyHomeDirURLForUser(NULL, true); diff --git a/CoreFoundation/Base.subproj/CFPriv.h b/CoreFoundation/Base.subproj/CFPriv.h index 60c02a9863..bec593c338 100644 --- a/CoreFoundation/Base.subproj/CFPriv.h +++ b/CoreFoundation/Base.subproj/CFPriv.h @@ -159,6 +159,9 @@ CFStringRef CFGetUserName(void); CF_EXPORT CFStringRef CFCopyUserName(void); +CF_EXPORT +CFStringRef CFCopyFullUserName(void); + CF_EXPORT CFURLRef CFCopyHomeDirectoryURLForUser(CFStringRef uName); /* Pass NULL for the current user's home directory */ diff --git a/Foundation/FileManager.swift b/Foundation/FileManager.swift index 1e4f046b4c..ce6b43a52f 100644 --- a/Foundation/FileManager.swift +++ b/Foundation/FileManager.swift @@ -746,7 +746,7 @@ extension FileManager { extension FileManager { open var homeDirectoryForCurrentUser: URL { - return homeDirectory(forUser: CFCopyUserName().takeRetainedValue()._swiftObject)! + return homeDirectory(forUser: NSUserName())! } open var temporaryDirectory: URL { diff --git a/Foundation/NSPathUtilities.swift b/Foundation/NSPathUtilities.swift index e96baf3c20..92808ada55 100755 --- a/Foundation/NSPathUtilities.swift +++ b/Foundation/NSPathUtilities.swift @@ -589,7 +589,8 @@ public func NSUserName() -> String { } public func NSFullUserName() -> String { - NSUnimplemented() + let userName = CFCopyFullUserName().takeRetainedValue() + return userName._swiftObject } internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, String) { From f62eced71fa2e0b0bb9ba29758d590cacaaf9580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Jose=CC=81=20Pereira=20Vieito?= Date: Mon, 12 Feb 2018 19:49:11 +0100 Subject: [PATCH 3/3] Updated style with braces on if statements --- CoreFoundation/Base.subproj/CFPlatform.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CoreFoundation/Base.subproj/CFPlatform.c b/CoreFoundation/Base.subproj/CFPlatform.c index 6e7a2a56d8..52363ea6c2 100644 --- a/CoreFoundation/Base.subproj/CFPlatform.c +++ b/CoreFoundation/Base.subproj/CFPlatform.c @@ -284,8 +284,10 @@ CF_EXPORT CFStringRef CFCopyUserName(void) { #else #error Dont know how to compute user name on this platform #endif - if (!result) + if (!result) { result = (CFStringRef)CFRetain(CFSTR("")); + } + return result; } @@ -301,8 +303,9 @@ CF_EXPORT CFStringRef CFCopyFullUserName(void) { #else #error Don't know how to compute full user name on this platform #endif - if (!result) + if (!result) { result = (CFStringRef)CFRetain(CFSTR("")); + } return result; }