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 2 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
18 changes: 18 additions & 0 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(""));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit here: please always use { } or make one line for if statements.


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);
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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use something like the following to extract the gecos info?

public func NSFullUserName() -> String {
    let euid = _CFGetEUID()
    let uid = euid != 0 ? euid : getuid()
    var pwd = passwd()
    let maxSize = sysconf(Int32(_SC_GETPW_R_SIZE_MAX))
    let buf = UnsafeMutablePointer<Int8>.allocate(capacity: maxSize)
    defer { buf.deallocate() }
    var ptr: UnsafeMutablePointer<passwd>? = nil
    if getpwuid_r(uid, &pwd, buf, maxSize, &ptr) == 0 {
        if let gecos = pwd.pw_gecos {
            return String(cString: gecos)
        }
    }

    return ""
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to leave the NSFullUserName implementation for other Pull Request/Contributor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this, shouldn't this code be implemented in Core Foundation in a function similar to CFCopyUserName and made portable between multiple platforms?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably keep it in Swift and only add it into CF if the multi-platform support became too messy and a lot easier to write using the C preprocessor.


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()
}
}