问题
I am writing a login item and I am trying to find if its possible to get the current user.
Lets say, I have logged in with user "Test" and when I execute the exe with root privileges and if I use
char *user = getenv("USER");
user is "root".
My expected answer is "Test".
How can I get it?
I don't know if I can put objective c code in login item? Is it possible to NSUserName in login item.
NSString *user = NSUserName();
回答1:
You want the SCDynamicStoreCopyConsoleUser function.
QA1133 gives some relevant details and caveats.
回答2:
Login items can be Cocoa applications, so you can use NSUserName()
(which is in Foundation btw).
Maybe also look at getuid() / geteuid()
来源:https://stackoverflow.com/questions/7583338/get-current-user-from-root-mode