[alt.sources] id command for non-BSD people without it

jfh@rpp386.cactus.org (John F. Haugh II) (03/02/90)

I added a Makefile and put the authors original comments in a README.
I also put conditional compilation statements around the BSD group
set code.  You only get it if NGROUPS is defined.

I compiled it just fine on SCO Xenix [ which already has an id command,
but so what ... ]
--
#! /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:
#	README
#	id.c
#	Makefile
# This archive created: Fri Mar  2 07:51:42 1990
# By:	John F. Haugh II (River Parishes Programming, Austin TX)
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'README'
then
	echo shar: "will not over-write existing file 'README'"
else
cat << \SHAR_EOF > 'README'
[ I added conditional compilation code for BSD, just in case someone
  wasn't running BSD and wanted to use this.  - John. ]

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

SHAR_EOF
fi
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 <alfie@cs.warwick.ac.uk>
 * Date:	August 17th 1989
 * Place:	University of Warwick, Coventry, UK
 */

#ifdef	__STDC__
extern	struct	passwd	*getpwuid (uid_t);
extern	struct	group	*getgrgid (gid_t);
#else
extern	struct	passwd	*getpwuid();
extern	struct	group	*getgrgid();
#endif

# 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 */
#ifdef	NGROUPS
    int  ng, gidset [ NGROUPS ];/* current group access list   */
#endif
    int  i;			/* 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 ();
#ifdef	NGROUPS
    ng = getgroups ( sizeof ( gidset ), gidset );
#endif

    /* --- 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 );
    }

#ifdef	NGROUPS
    /* --- 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 );
	}
    }
#endif

    (void) putc ( '\n', stdout );
}
SHAR_EOF
fi
if test -f 'Makefile'
then
	echo shar: "will not over-write existing file 'Makefile'"
else
cat << \SHAR_EOF > 'Makefile'
SHELL = /bin/sh
FILES = README id.c Makefile
BINS = id
OBJS = id.o
SHAR = id.shar
BINDIR = /usr/local/bin
BINUID = bin
BINGID = bin

all: $(BINS)

install: all
	cp $(BINS) $(BINDIR)
	for FILE in $(BINS) ; do \
		chmod 555 $(BINDIR)/$$FILE ;\
		chgrp $(BINGID) $(BINDIR)/$$FILE ;\
		chown $(BINUID) $(BINDIR)/$$FILE ;\
	done

clean:
	rm -f a.out core $(OBJS)

clobber: clean
	rm -f $(BINS)

shar:	$(FILES)
	shar $(FILES) > $(SHAR)
SHAR_EOF
fi
exit 0
#	End of shell archive
-- 
John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                           Domain: jfh@rpp386.cactus.org