Search code examples
objective-cmacosmacos-carbonbsd

Get Current User from root mode?


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();

Solution

  • You want the SCDynamicStoreCopyConsoleUser function.

    QA1133 gives some relevant details and caveats.