How to get user information

Discussion in 'C Programming' started by msc, Dec 27, 2005.

  1. msc

    msc Guest

    Hi guys,
    How to get the user information like userid written in C
    language.
    msc, Dec 27, 2005
    #1
    1. Advertising

  2. On 27 Dec 2005 04:11:29 -0800, in comp.lang.c , "msc"
    <> wrote:

    >Hi guys,
    > How to get the user information like userid written in C
    >language.


    You can't do this in standard C - you'd have to use some operating
    system specific extension.
    Ask again in a group specialising in your compiler or OS, and consider
    whether your OS stores this in the user's environment or something.
    Mark McIntyre
    --

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Dec 27, 2005
    #2
    1. Advertising

  3. In article <>,
    Mark McIntyre <> wrote:
    >On 27 Dec 2005 04:11:29 -0800, in comp.lang.c , "msc"
    ><> wrote:
    >
    >>Hi guys,
    >> How to get the user information like userid written in C
    >>language.

    >
    >You can't do this in standard C - you'd have to use some operating
    >system specific extension.
    >Ask again in a group specialising in your compiler or OS, and consider
    >whether your OS stores this in the user's environment or something.


    I.e.:

    Not portable. Can't discuss it here. Blah, blah, blah.
    Kenny McCormack, Dec 27, 2005
    #3
  4. msc

    Flash Gordon Guest

    msc wrote:
    > Hi guys,
    > How to get the user information like userid written in C
    > language.


    You can't in standard C so you will have to ask on a group dedicated to
    your system.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Dec 27, 2005
    #4
  5. msc said:

    > Hi guys,
    > How to get the user information like userid written in C
    > language.


    If the information you seek is stored in environment variables, you can get
    it using the ISO C standard function, getenv().

    For example:

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    const char *user = getenv("USER");
    if(user != NULL)
    {
    printf("User name: [%s]\n", user);
    }
    return 0;
    }

    The C Standard does not, of course, guarantee the existence or semantics of
    particular environment variables.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 27, 2005
    #5
  6. msc

    Stan Milam Guest

    Flash Gordon wrote:

    > msc wrote:
    >
    >> Hi guys,
    >> How to get the user information like userid written in C
    >> language.

    >
    >
    > You can't in standard C so you will have to ask on a group dedicated to
    > your system.


    True. Usually the operating system has some environment variable used
    to identify the user. Under Windoze it is "username" and with UNIX it
    is usually "LOGNAME." So, look around on your operating system and
    determine what environment variable is.
    Stan Milam, Dec 27, 2005
    #6
  7. Stan Milam <> writes:
    > Flash Gordon wrote:
    >> msc wrote:
    >>> Hi guys,
    >>> How to get the user information like userid written in C
    >>> language.

    >> You can't in standard C so you will have to ask on a group dedicated
    >> to your system.

    >
    > True. Usually the operating system has some environment variable used
    > to identify the user. Under Windoze it is "username" and with UNIX it
    > is usually "LOGNAME." So, look around on your operating system and
    > determine what environment variable is.


    This illustrates the dangers of posting partial system-specific
    answers.

    Though standard C provides no way to set or modify an environment
    variable, many systems do. On such a system, using an environment
    variable to identify a user allows a user to masquerade as someone
    else. Whether this is a problem depends on the application.

    Many systems provide ways to identify a user (whatever that happens to
    mean) in a more reliable manner. See a system-specific newsgroup (or
    your documentation) for more details.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 27, 2005
    #7
  8. Stan Milam wrote:
    >
    > Flash Gordon wrote:
    >
    > > msc wrote:
    > >
    > >> Hi guys,
    > >> How to get the user information like userid written in C
    > >> language.

    > >
    > >
    > > You can't in standard C so you will have to ask on a group dedicated to
    > > your system.

    >
    > True. Usually the operating system has some environment variable used
    > to identify the user. Under Windoze it is "username" and with UNIX it
    > is usually "LOGNAME." So, look around on your operating system and
    > determine what environment variable is.


    Ooh, goodie!

    LOGNAME=root /path/to/program
    or
    set username=administrator
    \path\to\program

    :)


    You need to use a system-specific method to return this information, which
    may or may not actually be available. (How does one get the username under
    MS-DOS, for example?)

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Jan 3, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. z. f.
    Replies:
    0
    Views:
    670
    z. f.
    Feb 3, 2005
  2. subrato
    Replies:
    8
    Views:
    909
    Juan T. Llibre
    Mar 14, 2006
  3. clercmedia
    Replies:
    2
    Views:
    32,254
    clercmedia
    Dec 9, 2005
  4. lerenard001
    Replies:
    0
    Views:
    416
    lerenard001
    Jul 7, 2008
  5. lerenard001
    Replies:
    0
    Views:
    460
    lerenard001
    Jul 9, 2008
Loading...

Share This Page