How to get current username

This is a discussion on How to get current username within the C Programming forums, part of the General Programming Boards category; Hi, How do get the current username who is running the current process. I can get the cuurent user ID ...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

    How to get current username

    Hi, How do get the current username who is running the current process. I can get the cuurent user ID through getuid(). But how do i get the real name of the user?

    i am using gcc 4.1 on ubuntu 7.05 environment.

    thank you

    ssharish

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,699
    Start here. See the "See Also" section.

    http://www.opengroup.org/onlinepubs/.../getlogin.html

    gg

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Codeplug View Post
    Start here. See the "See Also" section.

    http://www.opengroup.org/onlinepubs/.../getlogin.html

    gg
    Thanks a lot CodePlug, for the link. Just got sorted my username suff. Thanks again.

    ssharish

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    Quote Originally Posted by ssharish2005 View Post
    Hi, How do get the current username who is running the current process. I can get the cuurent user ID through getuid(). But how do i get the real name of the user?

    i am using gcc 4.1 on ubuntu 7.05 environment.

    thank you

    ssharish
    There is no "real name" of a user, there is just a UID. It's impossible in general to determine the username from the UID, although getlogin() attempts to do so.

    For instance, there might be multiple entries in /etc/passwd which all have the same UID. Or, there could be no entry at all for that UID -- that doesn't mean the user doesn't exist, just that it has no username or other associated information.

    The username is, of course, useful, but don't design your program around the username as some kind of unique identifier -- only the UID should be used for that.

    Also, you should probably use geteuid(), not getuid(), otherwise you will get the wrong username if the process is running as setuid to some other user.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 09:39 AM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM
  5. HelpMePlease
    By Prncess100 in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21