Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

obtaining the current user id?

1,492 views
Skip to first unread message

Ralf Fassel

unread,
Jun 5, 1997, 3:00:00 AM6/5/97
to

What is the Right Way to obtain the current user id, either numerically or
as string (other than something like [exec whoami] or creating a file in
/tmp and checking who owns it)? I thought this should be easy, and after
half an hour scanning the net I thought maybe this isn't FAQ...

Regards
R'
--
Unix is user-friendly, its just picky about who it is friends with.

John E Koontz

unread,
Jun 6, 1997, 3:00:00 AM6/6/97
to

In article <ygawwo9...@atg.venture.de>, Ralf Fassel <ra...@atg.venture.de> writes:
|> What is the Right Way to obtain the current user id, either numerically or
|> as string (other than something like [exec whoami] or creating a file in
|> /tmp and checking who owns it)? I thought this should be easy, and after
|> half an hour scanning the net I thought maybe this isn't FAQ...

This is something I've been wrestling with, too.

1) Avoid whoami, since not all systems have it!

2) You can try who am i, but it's harder to parse, since different systems
respond differently!

3) I've settled on parsing the output of the tcl pwd command, but for
that to be feasible you have to be in the user's home directory or
a known descendent of it!

4) You also can't count on $env(USER), and in some contexts (e.g.,
in a cgi script) you can't count on $env(HOME) either.

I'd be interested to hear others' views on this!

----
John E. Koontz (koo...@bldr.nist.gov)

Disclaimer: Views and recommendations, express or implied, are my own, and
do not reflect the opinion or policy of my employers.

JeroenHoppenbrouwers

unread,
Jun 9, 1997, 3:00:00 AM6/9/97
to

I try the environment variables USER and USERNAME (for NT). There might
be a few others worth trying as well.

--
Jeroen Hoppenbrouwers, Senior Researcher at | Stop connecting computers;
Infolab, Tilburg University, The Netherlands | start connecting people!
http://infolabwww.kub.nl:2080/infolab/people/hoppie

Christophe Muller

unread,
Jun 10, 1997, 3:00:00 AM6/10/97
to

In article <5n9b4p$fk...@news.nist.gov> koo...@cam.nist.gov (John E Koontz) writes:

> 4) You also can't count on $env(USER), and in some contexts (e.g.,
> in a cgi script) you can't count on $env(HOME) either.

$USER is not always defined on all Unix systems: in this case, you can do
something like :

if $LOGNAME is defined
then user_name = $LOGNAME
else user_name = $USER
endif

I don't know about NT..

Cheers,
Christophe.

= "Unix is simple, but it takes a genius to understand the simplicity." =
= --Dennis Ritchie =

Klaus ter Fehn

unread,
Jun 11, 1997, 3:00:00 AM6/11/97
to

I've searched the docs but haven't found a proper tcl command for it, so
I suggest the following extensions to Tcl (in peticular to the info
command, because the hostname can be obtained here already):

info username
unix: returns the login-name for the current user ID
Programming: POSIX function getpwuid()

win: returns the login-name of the current user or 'nouser'
Programming: ?!?

mac: ?!?
Programming: ?!?

info userid
unix: returns the current user ID
Programming: POSIX function getuid()

win: ?!?
Programming: ?!?

mac: ?!?
Programming: ?!?

info groupname
unix: returns the name for the current group ID
Programming: POSIX function getgrgid()

win: returns the Workgroup name the user belongs to
or 'nogroup'
Programming: ?!?

mac: ?!?
Programming: ?!?

info groupid
unix: returns the current group ID
Programming: POSIX function getgid()

win: ?!?
Programming: ?!?

mac: ?!?
Programming: ?!?

info realusername:
unix: returns the user's real name
Programming: POSIX function getpwuid()

win: ?!?
Programming: ?!?

mac: ?!?
Programming: ?!?

Any suggestions to this? The '?!?' are because I don't know much about
Windows and _really nothing_ about MacOS. I could write the extensions
for the unix environment easily but others must take care for the
Windows and MacIntosh environments. (If there is no such thing like an
user ID or group ID I'd suggest a returnvalue of '-1')

Also others may wish to have access to the supplementary group IDs as
returned by getgroups() or the effective user/group IDs
(geteuid()/getegid()) and the login name (getlogin()).

Please post/mail suggestions 'bout this. I will start programming as
soon as I've found at least one Windows and one MacOS programmer.

--
Klaus ter Fehn Wagnerstr. 4 Mobile: +49-172-2529379
40212 Duesseldorf Phone: +49-211-356880
k...@gun.de FRG/Germany Fax: +49-211-356881

Gerald W. Lester

unread,
Jun 11, 1997, 3:00:00 AM6/11/97
to

Klaus ter Fehn <k...@bc3.gun.de> wrote in article <EBLCJ...@bc3.gun.de>...

> I've searched the docs but haven't found a proper tcl command for it, so
> I suggest the following extensions to Tcl (in peticular to the info
> command, because the hostname can be obtained here already):

Why don't you write it and submit it to the archive.

BTW, the TclX extension implements this functionality on Unix.


--
****************************************************************************

* Gerald W. Lester | Voice: +1 (504)-889-2784
*
* Computerized Processes Unlimited, Inc. | FAX: +1 (504)-889-2799
*
* 4200 S. I-10 Service Rd., Suite 205 | E-Mail: Gerald...@cpu.com
*
* Metairie, LA 70001 | Web: http://www.cpu.com
*
****************************************************************************

lvi...@cas.org

unread,
Jun 11, 1997, 3:00:00 AM6/11/97
to

According to Klaus ter Fehn <k...@bc3.gun.de>:
:I've searched the docs but haven't found a proper tcl command for it, so


:I suggest the following extensions to Tcl (in peticular to the info
:command, because the hostname can be obtained here already):

These all sound similar to the TclX id command:

id options
This command provides a means of getting, setting and
converting user, group and process ids. The id command
has the following options:

id user ?name?

id userid ?uid?
Set the real and effective user ID to name or uid,
if the name (or uid) is valid and permissions
allow it. If the name (or uid) is not specified,
the current name (or uid) is returned.

id convert userid uid

id convert user name
Convert a user ID number to a user name, or vice
versa.

id group ?name?

id groupid ?gid?
Set the real and effective group ID to name or
gid, if the name (or gid) is valid and permissions
allow it. If the group name (or gid) is not
specified, the current group name (or gid) is
returned.

id groups

id groupids
Return the current group access list of the pro-
cess. The option groups returns group names and
groupids returns id numbers.

id convert groupid gid

id convert group name
Convert a group ID number to a group name, or vice
versa.

id effective user

id effective userid
Return the effective user name, or effective user
ID number, respectively.

id effective group

id effective groupid
Return the effective group name, or effective
group ID number, respectively.

id effective groupids
Return all of the groupids the user is a member
of.

id host
Return the hostname of the system the program is
running on.

id process
Return the process ID of the current process.

id process parent
Return the process ID of the parent of the current
process.

id process group
Return the process group ID of the current pro-
cess.

id process group set
Set the process group ID of the current process to
its process ID.

id host
Returns the standard host name of the machine the
process is executing on.
On Windows 95/NT, only the host and process options are
implemented.


Please folk, don't keep asking for the core of Tcl to get larger. The
heart of Tcl is the use of extensions. With dynamic loading, one seldom
has to recompile a Tcl enhanced application to add new capabilities.

Tcl and Tk's _second_ goal is to remain as general as possible. Adding
stuff like process ids, user ids, hostnames, etc. are not generic; frankly,
I think adding the socket code was a step too far and should be yanked out
and placed into an extension which can be loaded if needed.

--
Larry W. Virden INET: lvi...@cas.org
<URL:http://www.teraform.com/%7Elvirden/> <*> O- "We are all Kosh."
Unless explicitly stated to the contrary, nothing in this posting should
be construed as representing my employer's opinions.

Mo

unread,
Jun 11, 1997, 3:00:00 AM6/11/97
to

How about this.

set file [file join ~ core]
set fd [open $file w]
close $fd
file stat $file stat
set uid $stat(uid)
set attrs [file attributes $file]
set index [lsearch $attrs -owner]
if {$index == -1} {error "-owner not found"}
set s_uid [lindex $attrs [expr $index + 1]]
puts "user id is $s_uid, numerical value is $uid"


mo

Klaus ter Fehn

unread,
Jun 14, 1997, 3:00:00 AM6/14/97
to

I'm still waiting for Windows and MacOS Programmer to join the work.
Also noone has sent any comments on this yet - maybe this posting was
dropped somewhere?

I wrote:
> I've searched the docs but haven't found a proper tcl command for it, so
> I suggest the following extensions to Tcl (in peticular to the info
> command, because the hostname can be obtained here already):

> info username

Andreas Kupries

unread,
Jun 15, 1997, 3:00:00 AM6/15/97
to

k...@bc3.gun.de (Klaus ter Fehn) writes:
>
> I'm still waiting for Windows and MacOS Programmer to join the work.
> Also noone has sent any comments on this yet - maybe this posting was
> dropped somewhere?
>
> I wrote:
> > I've searched the docs but haven't found a proper tcl command for it, so
> > I suggest the following extensions to Tcl (in peticular to the info
> > command, because the hostname can be obtained here already):
>

You might want to take a look at the TclX extension in general, and
the 'id' command in particular.

See ftp://ftp.meosoft.com/pub/tcl/TclX/

--
Sincerely,
Andreas Kupries <a.ku...@westend.com>
<http://www.westend.com/~kupries/>
-------------------------------------------------------------------------------

Andreas Kupries

unread,
Jun 15, 1997, 3:00:00 AM6/15/97
to

k...@bc3.gun.de (Klaus ter Fehn) writes:
>
> I'm still waiting for Windows and MacOS Programmer to join the work.
> Also noone has sent any comments on this yet - maybe this posting was
> dropped somewhere?
>
> I wrote:
> > I've searched the docs but haven't found a proper tcl command for it, so
> > I suggest the following extensions to Tcl (in peticular to the info
> > command, because the hostname can be obtained here already):
>

You might want to take a look at the TclX extension in general, and
the 'id' command in particular.

See ftp://ftp.neosoft.com/pub/tcl/TclX/
~ this is the right letter!

Mark Weissman

unread,
Jun 17, 1997, 3:00:00 AM6/17/97
to

I was looking at the new font stuff in tk
that lets you name fonts and use those names
in widgets. Changing the named font changes
all the widgets that use it. It would be
really nice to do that with colors too.
--
Mark Weissman
weis...@gte.com

John Ousterhout

unread,
Jun 18, 1997, 3:00:00 AM6/18/97
to

In article <33A6CA...@gte.com>, Mark Weissman <weis...@gte.com> writes:
|> I was looking at the new font stuff in tk
|> that lets you name fonts and use those names
|> in widgets. Changing the named font changes
|> all the widgets that use it. It would be
|> really nice to do that with colors too.

It's on the list of things to do, but not at the top of the list.

John Haxby

unread,
Jun 18, 1997, 3:00:00 AM6/18/97
to

John E Koontz wrote:
> In article <ygawwo9...@atg.venture.de>, Ralf Fassel
> <ra...@atg.venture.de> writes: |> What is the Right Way to obtain
> the current user id, either numerically or |> as string (other than
> something like [exec whoami] or creating a file in |> /tmp and
> checking who owns it)? I thought this should be easy, and after |>
> half an hour scanning the net I thought maybe this isn't FAQ...
>
> This is something I've been wrestling with, too.
>
> 1) Avoid whoami, since not all systems have it!
>
> 2) You can try who am i, but it's harder to parse, since different
> systems respond differently!
>
> 3) I've settled on parsing the output of the tcl pwd command, but for
> that to be feasible you have to be in the user's home directory or a
> known descendent of it!
>
> 4) You also can't count on $env(USER), and in some contexts (e.g., in
> a cgi script) you can't count on $env(HOME) either.

$HOME, $LOGNAME, and $USER are all good candidates for guessing a user's
identity, but none of them are definitive.

You can try "who am i" to find out you are logged in as, but for
programs that aren't under the control of someone who has logged in,
this is less than useful. It's also UNIX specific.


"whoami" and "id" will both give useful information, although apart from
being UNIX specific, "whoami" isn't always available.

A definitive way to find the effective user-ID of a process is to create
a file and then see who owns it: for example, under Tcl8:

close [open "u[pid].tmp" "w"]
set user [file attributes "u[pid].tmp" -owner]
or
file stat "u[pid].tmp" stat
set uid $stat(uid)

file delete u[pid].tmp

If you don't want a Tcl-only solution, of course, you could wrap
getuid() up and stick it in a small extension. You still have to decide
whether you want the real or the effective user ID, of course. And I've
no idea what NT does ...
--
John Haxby
j...@pwd.hp.com http://www.ice.hp.com/
----------------------------------------------------------------
These are my opinions, not my employer's.

0 new messages