[GRASS5] cuserid alternative?

Glynn Clements glynn.clements at virgin.net
Sun Aug 1 22:19:57 EDT 2004


William K wrote:

> I checked the Mac OS X UNIX porting list archives and found a 
> suggestion to use getlogin() instead of cuserid (at least for the case 
> of passing a null pointer to cuserid).  Any thoughts?  I tried it by 
> changing dbmi_base/whoami.c to use that instead of cuserid - compiles 
> OK.
> 
> Is db_whoami() even used anywhere?  I didn't see it used in any of the 
> source.  Without it being used anywhere, I can't really say if 
> replacing cuserid() with getlogin() works (it's beyond my C knowledge 
> to say if it should work).

cuserid() returns the username corresponding to the effective UID of
the calling process, while getlogin returns the username corresponding
to the utmp entry for the TTY associated with descriptor 0 (standard
input).

They may not produce the same result, e.g. if the user has switched
UIDs with su. Also, getlogin() will fail if stdin doesn't refer to a
TTY, or if no utmp entry exists for the TTY (this is quite common for
xterms and similar).

In all probability, db_whoami() (and, for that matter, G_whoami())
should be using getenv("LOGNAME") rather than either cuserid() or
getlogin().

> FYI, this is my new whoami.c (let me know if anything is wrong with it):
> 
> char *
> db_whoami()
> {
>      char *getlogin();

Don't provide your own prototypes for library functions; include the
appropriate header instead.

-- 
Glynn Clements <glynn.clements at virgin.net>




More information about the grass-dev mailing list