Skip to content

Conversation

@pvieito
Copy link
Contributor

@pvieito pvieito commented Feb 11, 2018

  • Added ProcessInfo.userName and ProcessInfo.fullUserName as in Darwin Foundation
  • Added NSFullUserName() as in Darwin Foundation
  • Added underlying implementation in Core Foundation CFCopyFullUserName() for POSIX OS.

Marked NSFullUserName() as Unimplemented

public func NSFullUserName() -> String {
NSUnimplemented()
}
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.

CFCopyFullUserName implemented for POSIX OS.

TODO: Add Windows implementation.
@pvieito
Copy link
Contributor Author

pvieito commented Feb 12, 2018

Added NSFullUserName() and underlying implementation in CFCopyFullUserName() for POSIX OS.

#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.

@parkera
Copy link
Contributor

parkera commented Feb 12, 2018

Other than minor style point, looks good to me.

@pvieito
Copy link
Contributor Author

pvieito commented Feb 12, 2018

Updated style with braces on if statements.

@parkera
Copy link
Contributor

parkera commented Feb 12, 2018

@swift-ci test and merge

1 similar comment
@spevans
Copy link
Contributor

spevans commented Feb 13, 2018

@swift-ci test and merge

@swift-ci swift-ci merged commit 5873127 into swiftlang:master Feb 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants