Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Version of id(1).

164 views
Skip to first unread message

Mr N W Holloway

unread,
Mar 1, 1990, 8:34:32 AM3/1/90
to
I saw a request somewhere for a program like the SysV id(1). I got used
to it under SunOS, and so I wrote this one to use on our BSD machines.
As far as I can tell, they act identically - though I can't check this,
since I do not have access to source. The only area they may differ is
if the user or group name can not be determined.

There is no man page - the program is very simple, it justs prints out
info on your real & effective user & group id, and your current group
access list. Example (mythical) outputs are:

uid=0(root) gid=0(wheel) groups=0(wheel),1(daemon)
uid=1074(alfie) gid=1(general) euid=1(daemon) groups=1(general)

To compile, no magic flags needed, just "cc -o id id.c".

--
Nick Holloway | `O O' | al...@cs.warwick.ac.uk, al...@warwick.UUCP,
[aka `Alfie'] | // ^ \\ | ..!uunet!mcsun!ukc!warwick!alfie

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
# id.c
# This archive created: Thu Mar 1 13:15:51 1990
export PATH; PATH=/bin:$PATH
if test -f 'id.c'
then
echo shar: will not over-write existing file "'id.c'"
else
cat << \SHAR_EOF > 'id.c'
/* My own version of `id' for those systems that do not support it
* Displays uid, gid, euid, egid (if different to real), and groups
*
* Author: Nick Holloway <al...@cs.warwick.ac.uk>
* Date: August 17th 1989
* Place: University of Warwick, Coventry, UK
*/
# include <stdio.h>
# include <sys/param.h>
# include <pwd.h>
# include <grp.h>

main ()
{
int uid, euid; /* real and effective user-id */
int gid, egid; /* real and effective group-id */
int gidset [ NGROUPS ]; /* current group access list */
int ng, i; /* number of groups, index var */
struct passwd *pwd; /* used to retrieve user name */
struct group *grp; /* used to retrieve group name */

/* --- collect information --- */
uid = getuid ();
gid = getgid ();
euid = geteuid ();
egid = getegid ();
ng = getgroups ( sizeof ( gidset ), gidset );

/* --- print real user-id --- */
(void) printf ( "uid=%d", uid );
if ( ( pwd = getpwuid ( uid ) ) != NULL )
(void) printf ( "(%s)", pwd->pw_name );

/* --- print real group-id --- */
(void) printf ( " gid=%d", gid );
if ( ( grp = getgrgid ( gid ) ) != NULL )
(void) printf ( "(%s)", grp->gr_name );

/* --- print effective user-id (if different to real) --- */
if ( euid != uid ) {
(void) printf ( " euid=%d", euid );
if ( ( pwd = getpwuid ( euid ) ) != NULL )
(void) printf ( "(%s)", pwd->pw_name );
}

/* --- print effective group-id (if different to real) --- */
if ( egid != gid ) {
(void) printf ( " egid=%d", egid );
if ( ( grp = getgrgid ( egid ) ) != NULL )
(void) printf ( "(%s)", grp->gr_name );
}

/* --- print current group access list --- */
if ( ng > 0 ) {
(void) fputs ( " groups=", stdout );
for ( i = 0; i < ng; i++ ) {
if ( i != 0 )
(void) putc ( ',', stdout );
(void) printf ( "%d", gidset [ i ] );
if ( ( grp = getgrgid ( gidset [ i ] ) ) != NULL )
(void) printf ( "(%s)", grp->gr_name );
}
}

(void) putc ( '\n', stdout );
}
SHAR_EOF
fi # end of overwriting check
# End of shell archive
exit 0

Larry Wall

unread,
Mar 2, 1990, 3:23:50 AM3/2/90
to
In article <4...@clover.warwick.ac.uk> al...@cs.warwick.ac.uk (Nick Holloway) writes:
: I saw a request somewhere for a program like the SysV id(1). I got used

: to it under SunOS, and so I wrote this one to use on our BSD machines.
: As far as I can tell, they act identically - though I can't check this,
: since I do not have access to source. The only area they may differ is
: if the user or group name can not be determined.

For those of you who can't remember where you keep .c files, here's the
one I use. It differs slightly in that it sorts the groups.

#!/usr/bin/perl
print "uid=$<";
print "($name)" if ($name) = getpwuid($<);
print " gid=", $( + 0;
print "($name)" if ($name) = getgrgid($();
if ($> != $<) {
print " euid=$>";
print "($name)" if ($name) = getpwuid($>);
}
if ($) != $() {
print " egid=", $) + 0;
print "($name)" if ($name) = getgrgid($));
}
@groups = split(' ',$();
shift(@groups);
for (@groups) {
$_ .= "($name)" if ($name) = getgrgid($_);
}
print " groups=", join(',',sort bynum @groups) if @groups;
print "\n";
sub bynum { $a - $b; }

No doubt Randal could write it shorter...

Larry

Randal Schwartz

unread,
Mar 2, 1990, 4:02:09 PM3/2/90
to
In article <29...@jato.Jpl.Nasa.Gov>, lwall@jato (Larry Wall) writes:
[21 lines of code deleted...]

| No doubt Randal could write it shorter...

Well, OK, taking on the challenge, how about:

#!/usr/bin/perl
sub u { local($name)=getpwuid($_[0]); $name && "($name)";}
sub g { local($name)=getgrgid($_[0]); $name && "($name)";}


sub bynum { $a - $b; }

print "uid=$<",&u($<);
print " gid=", $(+0,&g($();
print " euid=$>",&u($>) if $> != $<;
print " egid=", $)+0,&g($)) if $) != $(;
@groups=split(' ',$(); shift(@groups);
print " groups=", join(',',sort bynum grep(($_ .= &g($_)) || 1, @groups))
unless $#groups < $[;
print "\n";

Something like that, Larry?

print pack('C25',grep($_ = 32 + y/ / /, split(/\n/, <<END)));
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
END
--
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III |
| mer...@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

Larry Wall

unread,
Mar 2, 1990, 5:50:23 PM3/2/90
to
In article <1990Mar2.2...@iwarp.intel.com> mer...@iwarp.intel.com (Randal Schwartz) writes:

: In article <29...@jato.Jpl.Nasa.Gov>, lwall@jato (Larry Wall) writes:
: [21 lines of code deleted...]
: | No doubt Randal could write it shorter...
:
: Well, OK, taking on the challenge, how about:
:
: #!/usr/bin/perl
: sub u { local($name)=getpwuid($_[0]); $name && "($name)";}
: sub g { local($name)=getgrgid($_[0]); $name && "($name)";}
: sub bynum { $a - $b; }
: print "uid=$<",&u($<);
: print " gid=", $(+0,&g($();
: print " euid=$>",&u($>) if $> != $<;
: print " egid=", $)+0,&g($)) if $) != $(;
: @groups=split(' ',$(); shift(@groups);
: print " groups=", join(',',sort bynum grep(($_ .= &g($_)) || 1, @groups))
: unless $#groups < $[;
: print "\n";
:
: Something like that, Larry?

That's not short. This is short: :-)

#!/usr/bin/perl
@n=('pwu','grg');sub n{local($n)=eval"get$n[$_[1]]id(\$_[0])";$n&&"($n)";}sub nm
{$a-$b;}@gr=split(' ',$();$g=shift(@gr);$\="\n";print"uid=$<",&n($<)," gid=",$g,
&n($(,1),(" euid=$>".&n($>))x($<!=$>),(" egid=".$)+0 .&n($),1))x($(!=$)),
(" groups=".join(',',sort nm grep(($_.=&n($_,1))||1,@gr)))x($#gr>=0);

Though I'll admit readability suffers slightly...

Larry

Jay you ignorant splut! Maynard

unread,
Mar 6, 1990, 2:31:47 AM3/6/90
to
In article <29...@jato.Jpl.Nasa.Gov> lw...@jato.Jpl.Nasa.Gov (Larry Wall) writes:
>That's not short. This is short: :-)
>
>#!/usr/bin/perl
>@n=('pwu','grg');sub n{local($n)=eval"get$n[$_[1]]id(\$_[0])";$n&&"($n)";}sub nm
>{$a-$b;}@gr=split(' ',$();$g=shift(@gr);$\="\n";print"uid=$<",&n($<)," gid=",$g,
>&n($(,1),(" euid=$>".&n($>))x($<!=$>),(" egid=".$)+0 .&n($),1))x($(!=$)),
>(" groups=".join(',',sort nm grep(($_.=&n($_,1))||1,@gr)))x($#gr>=0);
>
>Though I'll admit readability suffers slightly...

main() {
puts("Good God. What next - an Obfuscated Perl Contest??");
}

--
Jay Maynard, EMT-P, K5ZC, PP-ASEL | Never ascribe to malice that which can
j...@splut.conmicro.com (eieio)| adequately be explained by stupidity.
{attctc,bellcore}!texbell!splut!jay +----------------------------------------
"Klein bottle for sale. Inquire within." - Charles Hannum

Brandon S. Allbery

unread,
Mar 9, 1990, 9:53:30 PM3/9/90
to
As quoted from <0-H...@splut.conmicro.com> by j...@splut.conmicro.com (Jay "you ignorant splut!" Maynard):
+---------------

| main() {
| puts("Good God. What next - an Obfuscated Perl Contest??");
| }
+---------------

main() {
cout << "That's what the signature scripts are, silly! ;-)\n";
}
--
Brandon S. Allbery (human), all...@NCoast.ORG (Inet), BALLBERY (MCI Mail)
ALLBERY (Delphi), uunet!cwjcc.cwru.edu!ncoast!allbery (UUCP), B.ALLBERY (GEnie)
BrandonA (A-Online) ("...and a partridge in a pear tree!" ;-)

0 new messages