Skip to content

Instantly share code, notes, and snippets.

@joncardasis
Created December 5, 2017 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joncardasis/2c46c062f8450b96bb1e571950b26bf7 to your computer and use it in GitHub Desktop.
Save joncardasis/2c46c062f8450b96bb1e571950b26bf7 to your computer and use it in GitHub Desktop.
Retrieve the console username of the user running a Swift program
import SystemConfiguration
/**
Retrieve the console username of the user running the program.
Will be the same result as a `whoami` command.
*/
public func systemConsoleUsername() -> String? {
var uid: uid_t = 0
var gid: gid_t = 0
return SCDynamicStoreCopyConsoleUser(nil, &uid, &gid) as String?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment