By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,146 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

get current user name?

P: n/a
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.

Nov 15 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
dj*********@snowboard.com wrote:
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

Nov 15 '05 #2

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

Nov 15 '05 #3

P: n/a
dj*********@snowboard.com wrote:
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

Nov 15 '05 #4

P: n/a
<dj*********@snowboard.com> wrote:
Sorry, I thought there might be an easy C function that accomplishes
this, but I haven't been able to find one yet.


There isn't.
Nov 15 '05 #5

P: n/a
dj*********@snowboard.com wrote:
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, which is declared in <winbase.h>...
I though about using the LOGNAME environment variable, but if a regular
user used su root,


....oops.

Richard
Nov 15 '05 #6

P: n/a
On Fri, 19 Aug 2005 06:33:21 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
dj*********@snowboard.com wrote:
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, 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?
I though about using the LOGNAME environment variable, but if a regular
user used su root,


...oops.

Richard

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #7

P: n/a

"Alan Balmer" <al******@att.net> wrote in message
news:d5********************************@4ax.com...
On Fri, 19 Aug 2005 06:33:21 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
dj*********@snowboard.com wrote:
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, 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.


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' :-)

I though about using the LOGNAME environment variable, but if a regular
user used su root,


...oops.

Richard

--
Al Balmer
Balmer Consulting
re************************@att.net

Nov 15 '05 #8

P: n/a
Eric wrote:

<dj*********@snowboard.com> wrote:
Sorry, I thought there might be an easy C function that accomplishes
this, but I haven't been able to find one yet.


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:Th*************@gmail.com>
Nov 15 '05 #9

P: n/a
Richard Bos wrote:

dj*********@snowboard.com wrote:
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, 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
#
I though about using the LOGNAME environment variable, but if a regular
user used su root,


...oops.


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:Th*************@gmail.com>
Nov 15 '05 #10

P: n/a
On Fri, 19 Aug 2005 17:11:17 GMT, "Mark" <so***@localbar.com> wrote:

"Alan Balmer" <al******@att.net> wrote in message
news:d5********************************@4ax.com.. .
On Fri, 19 Aug 2005 06:33:21 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
dj*********@snowboard.com wrote:

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


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' :-)

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 though about using the LOGNAME environment variable, but if a regular
user used su root,

...oops.

Richard

--
Al Balmer
Balmer Consulting
re************************@att.net

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #11

P: n/a
Alan Balmer <al******@att.net> writes:
On Fri, 19 Aug 2005 06:33:21 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
dj*********@snowboard.com wrote:
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, 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?


Yes, he's sure he's in the right newsgroup; that's the point he was
trying to make. See the "oops" below.
I though about using the LOGNAME environment variable, but if a regular
user used su root,


...oops.


--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #12

P: n/a

"Alan Balmer" <al******@att.net> wrote in message
news:j6********************************@4ax.com...
On Fri, 19 Aug 2005 17:11:17 GMT, "Mark" <so***@localbar.com> wrote:

"Alan Balmer" <al******@att.net> wrote in message
news:d5********************************@4ax.com. ..
On Fri, 19 Aug 2005 06:33:21 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:

dj*********@snowboard.com wrote:

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


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' :-)

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

Nov 15 '05 #13

P: n/a
I apologize in advance... but I couldn't resist :-)
"Kenneth Brody" <ke******@spamcop.net> wrote in message
news:43***************@spamcop.net...
Richard Bos wrote:

dj*********@snowboard.com wrote:
> 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, 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.
> I though about using the LOGNAME environment variable, but if a regular
> user used su root,


...oops.


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>
Nov 15 '05 #14

P: n/a
"Mark" <so***@localbar.com> wrote:
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:
dr***@localbar.com ;)

--
http://members.dodo.com.au/~netocrat

Nov 15 '05 #15

P: n/a
Alan Balmer <al******@att.net> writes:
On Fri, 19 Aug 2005 17:11:17 GMT, "Mark" <so***@localbar.com> wrote:
"Alan Balmer" <al******@att.net> wrote in message
news:d5********************************@4ax.com. .. [...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #16

P: n/a
On Fri, 19 Aug 2005 19:29:26 GMT, Keith Thompson <ks***@mib.org>
wrote:
Alan Balmer <al******@att.net> writes:
On Fri, 19 Aug 2005 17:11:17 GMT, "Mark" <so***@localbar.com> wrote:
"Alan Balmer" <al******@att.net> wrote in message
news:d5********************************@4ax.com ...[...] 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 :-)
--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #17

P: n/a

In article <j6********************************@4ax.com>, Alan Balmer <al******@att.net> writes:
On Fri, 19 Aug 2005 17:11:17 GMT, "Mark" <so***@localbar.com> wrote:
"Alan Balmer" <al******@att.net> wrote in message
news:d5********************************@4ax.com.. .
On Fri, 19 Aug 2005 06:33:21 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
dj*********@snowboard.com wrote:

> How do I retrieve the username of the currently logged in user?

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.


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' :-)

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 mi************@microfocus.com

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
Nov 15 '05 #18

P: n/a
mw*****@newsguy.com (Michael Wojcik) wrote:
In article <j6********************************@4ax.com>, Alan Balmer <al******@att.net> writes:
On Fri, 19 Aug 2005 17:11:17 GMT, "Mark" <so***@localbar.com> wrote:
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?


Many implementations do not support sarcasm.


Quite; but I keep forgetting that.

"Welcome to Usenet. Please provide your own sledgehammer."

Richard
Nov 15 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.