How to get user information

M

Mark McIntyre

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
 
K

Kenny McCormack

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.
 
F

Flash Gordon

msc said:
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.
 
R

Richard Heathfield

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.
 
S

Stan Milam

Flash said:
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.
 
K

Keith Thompson

Stan Milam said:
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.
 
K

Kenneth Brody

Stan said:
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:[email protected]>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top