awk and shell question

Guy Harris guy at auspex.auspex.com
Wed Sep 20 04:36:26 AEST 1989


>>: >I have to get the user id, and then get the user description from
>>: >/etc/passwd.
>>Again, watch out for Yellow Pages; if you're using that you have to do...

Or, if you want to do something that's independent of the details of how
password file access is implemented, try the attached commands.
"getpwnam" takes one argument, a user name, and prints its password
file entry; "getpwuid" takes one argument, a user ID number, and prints
its password file entry.

This should work fine, and use the most efficient access form available,
if you have:

	a traditional text file "/etc/passwd";

	a "dbm" database, 4.3BSD-style;

	a Yellow Pages map, or some other network server-based map
	   (e.g., Hesiod, if the "optional" implementations of "getpwnam()"
	   and its "inverse counterpart" are present; does the Apollo
	   Registry implementation have "getpwnam()" and "getpwuid()"
	   routines that query the registry?);

	or anything else that has "getpwnam()" and "getpwuid()"
	    routines.

and if your system actually implements the most efficient access form
available in "getpwnam()" and "getpwuid()".

With any luck, these should run on anything V7 or later.  If you have
something later, you can consider other improvements, such as using
"strtol" instead of "atoi" to convert the user ID, and catch non-numeric
strings, if your system has "strtol".

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  getpwnam.c getpwuid.c
# Wrapped by guy at auspex on Tue Sep 19 11:28:42 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'getpwnam.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'getpwnam.c'\"
else
echo shar: Extracting \"'getpwnam.c'\" \(523 characters\)
sed "s/^X//" >'getpwnam.c' <<'END_OF_FILE'
X#include <stdio.h>
X
X#include <pwd.h>
X
Xextern struct passwd *getpwnam();
X
Xint
Xmain(argc, argv)
X	int argc;
X	char **argv;
X{
X	register struct passwd *pw;
X
X	if (argc != 2) {
X		fprintf(stderr, "Usage: getpwnam <username>\n");
X		exit(1);
X	}
X	if ((pw = getpwnam(argv[1])) == NULL) {
X		fprintf(stderr, "getpwnam: No such user \"%s\"\n",
X		    argv[1]);
X		exit(2);
X	}
X	printf("%s:%s:%d:%d:%s:%s:%s\n",
X	    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
X	    pw->pw_gecos, pw->pw_dir, pw->pw_shell);
X	exit(0);
X	/*NOTREACHED*/
X}
END_OF_FILE
if test 523 -ne `wc -c <'getpwnam.c'`; then
    echo shar: \"'getpwnam.c'\" unpacked with wrong size!
fi
# end of 'getpwnam.c'
fi
if test -f 'getpwuid.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'getpwuid.c'\"
else
echo shar: Extracting \"'getpwuid.c'\" \(528 characters\)
sed "s/^X//" >'getpwuid.c' <<'END_OF_FILE'
X#include <stdio.h>
X
X#include <pwd.h>
X
Xextern struct passwd *getpwuid();
X
Xint
Xmain(argc, argv)
X	int argc;
X	char **argv;
X{
X	register struct passwd *pw;
X
X	if (argc != 2) {
X		fprintf(stderr, "Usage: getpwuid <user id>\n");
X		exit(1);
X	}
X	if ((pw = getpwuid(atoi(argv[1]))) == NULL) {
X		fprintf(stderr, "getpwuid: No such user \"%s\"\n",
X		    argv[1]);
X		exit(2);
X	}
X	printf("%s:%s:%d:%d:%s:%s:%s\n",
X	    pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
X	    pw->pw_gecos, pw->pw_dir, pw->pw_shell);
X	exit(0);
X	/*NOTREACHED*/
X}
END_OF_FILE
if test 528 -ne `wc -c <'getpwuid.c'`; then
    echo shar: \"'getpwuid.c'\" unpacked with wrong size!
fi
# end of 'getpwuid.c'
fi
echo shar: End of shell archive.
exit 0



More information about the Comp.unix.wizards mailing list