euser - display the effective user name, SysV-ish

Warren Tucker wht at tridom.uucp
Sun Jul 9 03:58:02 AEST 1989


In article <14448 at bfmny0.UUCP>, tneff at bfmny0.UUCP (Tom Neff) writes:
> If you need a FAST way to generate the name of your current effective
> user (including the effects of su(1) etc), this works.  It's not fancy.
> Obviously it has to read /etc/passwd.  Feel free to hack and pass
> around.

Try this:

#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  whoami.c
#
sed 's/^X//' << 'SHAR_EOF' > whoami.c &&
X/*+-------------------------------------------------------------------------
X	whoami.c - System V 'whoami' - prints effective user name (euid)
X	...!gatech!emory!tridom!wht
X
XSystem V ain't got one of these goodies
X--------------------------------------------------------------------------*/
X/*+:EDITS:*/
X/*:01-27-1989-17:16-wht-creation */
X
X#include <stdio.h>
X#include <pwd.h>
X
X/*+-------------------------------------------------------------------------
X	who_am_i() - return first username in /etc/passwd that matches euid
X--------------------------------------------------------------------------*/
Xchar *
Xwho_am_i()
X{
Xstruct passwd *passwd;
Xstruct passwd *getpwuid();
X
X	passwd = getpwuid(geteuid());    /* gotta succeed unless MUCH is wrong */
X	endpwent();
X	if(passwd == (struct passwd *)0) /* much wrong has been known to happen */
X		return("???");
X	return(passwd->pw_name);
X
X}	/* end of who_am_i */
X
X/*+-------------------------------------------------------------------------
X	main(argc,argv,envp)
X--------------------------------------------------------------------------*/
Xmain(argc,argv,envp)
Xint argc;
Xchar **argv;
Xchar **envp;
X{
X	printf("%s\n",who_am_i());
X	exit(0);
X}	/* end of main */
X
X/* vi: set tabstop=4 shiftwidth=4: */
X/* end of whoami.c */
SHAR_EOF
chmod 0644 whoami.c || echo "restore of whoami.c fails"
exit 0
-- 
-------------------------------------------------------------------
Warren Tucker, Tridom Corporation       ...!gatech!emory!tridom!wht 
Sforzando (It., sfohr-tsahn'-doh).  A direction to perform the tone
or chord with special stress, or marked and sudden emphasis.



More information about the Alt.sources mailing list