get current user name?

D

djbitchpimp

How do I retrieve the username of the currently logged in user? I
basically to check if the user is logged in as root and if not, exit
the program.

I though about using the LOGNAME environment variable, but if a regular
user used su root, the environment variable still has the user's login
name.
 
R

Robert Gamble

How do I retrieve the username of the currently logged in user? I
basically to check if the user is logged in as root and if not, exit
the program.

I though about using the LOGNAME environment variable, but if a regular
user used su root, the environment variable still has the user's login
name.

This is not a C question, try comp.unix.programmer.

Robert Gamble
 
D

djbitchpimp

Sorry, I thought there might be an easy C function that accomplishes
this, but I haven't been able to find one yet.
 
D

David Resnick

How do I retrieve the username of the currently logged in user? I
basically to check if the user is logged in as root and if not, exit
the program.

I though about using the LOGNAME environment variable, but if a regular
user used su root, the environment variable still has the user's login
name.

There is no standard way in C. Your best bet is to ask in
a newsgroup(s) associated with your platform(s) of choice,
such as comp.unix.programmer.

<OT> If you can assume POSIX, look at whether
getpwuid and getuid do what you want. But
discussion on them should be in
comp.unix.programmer </OT>

-David
 
R

Richard Bos

How do I retrieve the username of the currently logged in user? I
basically to check if the user is logged in as root and if not, exit
the program.

You call GetUserName said:
I though about using the LOGNAME environment variable, but if a regular
user used su root,

....oops.

Richard
 
K

Kenneth Brody

Eric said:
There isn't.

However, if his O/S has the concept of "user name", then there is probably
an O/S call which returns it, and it is probably callable from C by means
of a platform-specific library call. If he were to ask a platform-specific
newsgroup, he should be able to get an answer there. Since he never said
what O/S he was on, he'll have to figure that part out himself. (Though,
given some of his terminology, my guess is comp.unix.programmer may be the
place for him.)

--
+-------------------------+--------------------+-----------------------------+
| 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]>
 
K

Kenneth Brody

Richard said:
You call GetUserName, which is declared in <winbase.h>...

# gcc foo.c
foo.c:1: winbase.h: No such file or directory
#
[...remove unknown "winbase.h" include...]
# gcc foo.c
foo.o: In function `main':
foo.o(.text+0x7): undefined reference to `GetUserName'
collect2: ld returned 1 exit status
#

Which is why platform-specific answers don't work here. :)

--
+-------------------------+--------------------+-----------------------------+
| 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]>
 
A

Alan Balmer

You call GetUserName, which is declared in <winbase.h>...

Odd - I can't seem to find either the function or the include file
mentioned in the C standard, or even the POSIX standard. Are you sure
you're in the right newsgroup?
 
M

Mark

Alan Balmer said:
Odd - I can't seem to find either the function or the include file
mentioned in the C standard, or even the POSIX standard.

Why are you mentioning the POSIX standard? This is comp.lang.c
where we deal strictly with the ISO/IEC international standard.
Are you sure you're in the right newsgroup?
Exactly, that was his point... couldn't you tell by the 'oops' :)

 
A

Alan Balmer

Why are you mentioning the POSIX standard? This is comp.lang.c
where we deal strictly with the ISO/IEC international standard.

Exactly, that was his point... couldn't you tell by the 'oops' :)
No. Considering the placement, I assumed that he had just realized the
OP was using Unix ("su root") rather than Windows. If the "oops" meant
he knew it was off-topic, why send it?
 
K

Keith Thompson

Alan Balmer said:
Odd - I can't seem to find either the function or the include file
mentioned in the C standard, or even the POSIX standard. Are you sure
you're in the right newsgroup?

Yes, he's sure he's in the right newsgroup; that's the point he was
trying to make. See the "oops" below.
 
N

Netocrat

Mark said:
I apologize in advance... but I couldn't resist :) [...]
<flame suit on>

I think there's an error in your email address. It should be:
(e-mail address removed) ;)
 
M

Mark

Alan Balmer said:
No. Considering the placement, I assumed that he had just realized the
OP was using Unix ("su root") rather than Windows. If the "oops" meant
he knew it was off-topic, why send it?

Because he's Richard Bos...
surely that isn't the first post of his you've read
 
M

Mark

I apologize in advance... but I couldn't resist :)
Kenneth Brody said:
Richard said:
You call GetUserName, which is declared in <winbase.h>...

# gcc foo.c
foo.c:1: winbase.h: No such file or directory
#
[...remove unknown "winbase.h" include...]

OMG: Your system is missing a header file... luckily I know what's in it.
Just add the following line to your program:
#define GetUserName cuserid
# gcc foo.c
foo.o: In function `main':
foo.o(.text+0x7): undefined reference to `GetUserName'
collect2: ld returned 1 exit status

Duh, you removed the header file... but didn't created the necessary macro!
Of course it didn't work! It's your own fault though.
Which is why platform-specific answers don't work here. :)

No, it didn't work because you removed the header file...
try it again after making the change I suggested. :) :) :)

<flame suit on>
 
K

Keith Thompson

Alan Balmer said:
No. Considering the placement, I assumed that he had just realized the
OP was using Unix ("su root") rather than Windows. If the "oops" meant
he knew it was off-topic, why send it?

I believe he was being subtle. He deliberately gave an answer that
was correct for Windows but useless anywhere else. He then quoted
some more of the OP's question that made it clear he was using a
Unix-like system, followed by the "oops". It was an indirect way of
telling the OP that his question was off-topic.
 
A

Alan Balmer

Alan Balmer said:
news:[email protected]... [...]
Are you sure you're in the right newsgroup?
Exactly, that was his point... couldn't you tell by the 'oops' :)
No. Considering the placement, I assumed that he had just realized the
OP was using Unix ("su root") rather than Windows. If the "oops" meant
he knew it was off-topic, why send it?

I believe he was being subtle. He deliberately gave an answer that
was correct for Windows but useless anywhere else. He then quoted
some more of the OP's question that made it clear he was using a
Unix-like system, followed by the "oops". It was an indirect way of
telling the OP that his question was off-topic.

Too subtle for me. I wouldn't count on so subtle an answer being
effective in redirecting the OP to another newsgroup. Much simpler to
just say "Off-topic here. Try another newsgroup."

But then, I'm just a simple programmer who dislikes having to fix
other people's subtle code. I stopped being clever years ago :)
 
M

Michael Wojcik

No. Considering the placement, I assumed that he had just realized the
OP was using Unix ("su root") rather than Windows. If the "oops" meant
he knew it was off-topic, why send it?

Many implementations do not support sarcasm. It is not defined by
the ISO C standard and so is OT in comp.lang.c. It should be avoided
in portable newsgroup posts.

(If the "oops" meant Richard intended the reference to GetUserName
seriously, then realized that it was probably[1] irrelevant, why
would *that* justify sending the message? Your implied explanation
for Richard's message makes less sense than Mark's.)


1. Of course, there are "su" programs for Windows, and it's perfectly
possible to have a "root" account there; the OP's reference to "su
root" does not prove that he's working on a POSIX system. And there
are POSIX implementations for Windows, including the built-in POSIX
subsystem and such add-ons as Cygwin and SFU.

--
Michael Wojcik (e-mail address removed)

As always, great patience and a clean work area are required for fulfillment
of this diversion, and it should not be attempted if either are compromised.
-- Chris Ware
 

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
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top