Skip to content

Added userName and fullUserName to ProcessInfo #1431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,29 @@ 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;
}

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;
}

Expand Down
3 changes: 3 additions & 0 deletions CoreFoundation/Base.subproj/CFPriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
2 changes: 1 addition & 1 deletion Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ public func NSUserName() -> String {
return userName._swiftObject
}

public func NSFullUserName() -> String {
let userName = CFCopyFullUserName().takeRetainedValue()
return userName._swiftObject
}

internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, String) {
let template = "." + filePath + ".tmp.XXXXXX"
let maxLength = Int(PATH_MAX) + 1
Expand Down
8 changes: 8 additions & 0 deletions Foundation/ProcessInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}