dshb
dshb copied to clipboard
Slightly cleaner way to do getUsername()
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
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
}`