Skip to content

Slightly cleaner way to do getUsername() #42

@bryanc-nz

Description

@bryanc-nz

I had a look at getUsername and saw that it could be a little cleaner - in fact I suspect the current version has a small memory leak. My version below for what its worth.
`
func getUsername(_ uid: uid_t) -> String
{
let username: String
var passwdInfo = passwd()
var result: UnsafeMutablePointer? = nil

let bufferSize = sysconf(_SC_GETPW_R_SIZE_MAX)
var buffer = [Int8](repeating: 0, count: bufferSize)

if getpwuid_r(uid, &passwdInfo, &buffer, bufferSize, &result) == 0,
   result != nil {
	username = String(cString: passwdInfo.pw_name)
} else {
	username = String(Int(uid))
}

return username

}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions