Retrieving user name with carbon.

Apprentice
Posts: 11
Joined: 2006.08
Post: #1
Hi!

I'm a bit new on Mac development.
I would like to know if there is a way to retrieve the current user name with Carbon?

I'm porting an application from Windows to Mac, and so far I couldn't find a way to do it.

Thanks!
Quote this message in a reply
Moderator
Posts: 623
Joined: 2007.09
Post: #2
In the words of the wise OneSadCookie: Carbon is all but dead. Use Cocoa instead.

- Lincoln Green
http://www.binkworks.com/
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #3
Are you looking for the user's short name, or their full name? If you just want short name, this appears to work:
Code:
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    struct passwd * passwd;
    
    passwd = getpwuid(getuid());
    printf("Name: %s\n", passwd->pw_name);
    
    return EXIT_SUCCESS;
}

See also whoami() in http://www.opensource.apple.com/darwinso.../who/who.c (requires ADC login).
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
NSUserName() / NSFullUserName() may also be useful. I thought there were CF equivalents but I can't see them :/
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2006.08
Post: #5
Thanks.
Now I'll have to code and try.
Quote this message in a reply
Member
Posts: 204
Joined: 2002.09
Apprentice
Posts: 11
Joined: 2006.08
Post: #7
completeness, yeah, I would say ready for copy and paste ... thanks!
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2006.10
Post: #8
Here's the CF way:

Quote:char buffer[256];
Boolean success = CFStringGetCString(CSCopyUserName(true), buffer, sizeof(buffer), kCFStringEncodingUTF8);
Quote this message in a reply
Post Reply