Path: utzoo!mnetor!tmsoft!torsqnt!jarvis.csri.toronto.edu!rutgers!apple!gem.mps.ohio-state.edu!ginosko!uunet!auspex!guy From: guy@auspex.auspex.com (Guy Harris) Newsgroups: comp.unix.wizards Subject: Re: awk and shell question Keywords: awk sh Message-ID: <2471@auspex.auspex.com> Date: 19 Sep 89 18:36:26 GMT References: <1163@ispi.UUCP> <2412@netcom.UUCP> <2130@munnari.oz.au> <124894@sun.Eng.Sun.COM> Reply-To: guy@auspex.auspex.com (Guy Harris) Organization: Auspex Systems, Santa Clara Lines: 124 >>: >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 'getpwnam.c' <<'END_OF_FILE' X#include X X#include 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 \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 X X#include 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 \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