dshb icon indicating copy to clipboard operation
dshb copied to clipboard

Slightly cleaner way to do getUsername()

Open bryanc-nz opened this issue 6 years ago • 0 comments

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

}`

bryanc-nz avatar May 16 '18 04:05 bryanc-nz