home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <errno.h>
- #include <pwd.h>
- #include "pwinf.h"
- #include <gnu/fileutil.h>
- static char *RCSid = "$Header: getpwnam.c,v 1.4 92/02/25 14:00:08 dtb Exp $";
-
- int fpr_usage();
- int process_it();
-
- static int report_except = 0;
- static char all_found = 1;
- static char *trim_main_name;
-
-
- int main ( argc, argv, envp )
- int argc;
- char **argv;
- char **envp;
- {
- char *optstring = "acd:Ighnprsuv" ;
- int optchar;
- extern char *optarg;
- extern int optind;
- extern int opterr;
- struct pwinfrq *pwinf;
-
- trim_main_name = basename(*argv);
- if ((pwinf = pwinfrq_set_dflt (NULL)) == NULL)
- exit (ENOMEM);
- opterr = 0;
- while ((optchar = getopt(argc, argv, optstring)) != -1)
- switch (optchar) {
- case 'I':
- fprintf (stderr, "%s %s\n", __FILE__, RCSid);
- pwinf_info_fpr (stderr);
- break;
- case 'a':
- pwinf->epw = pwinf->nam = pwinf->uid = pwinf->gid
- = pwinf->age = pwinf->cmt = pwinf->dir =
- pwinf->shl = 1;
- break;
- case 'c':
- pwinf->cmt = 1;
- break;
- case 'd':
- pwinf->dlm = *optarg;
- break;
- case 'g':
- pwinf->gid = 1;
- break;
- case 'h':
- pwinf->dir = 1;
- break;
- case 'n':
- pwinf->nam = 1;
- break;
- case 'p':
- pwinf->epw = 1;
- break;
- case 'r':
- report_except = 1;
- break;
- case 's':
- pwinf->shl = 1;
- break;
- case 'u':
- pwinf->uid = 1;
- break;
- case 'v':
- pwinf->vbs = 1;
- break;
- case '?':
- fpr_usage (stderr);
- exit (EINVAL);
- }
- if (optind == argc) {
- struct passwd *pw = NULL;
- char unametmp[256];
- char *loginname = NULL;
-
-
- if ((loginname = cuserid(NULL)) == NULL)
- loginname = (char *) getlogin();
- if (loginname != NULL) {
- if ((pw = (struct passwd *) getpwnam (loginname))
- == NULL) {
- fprintf (stderr, "%s: No password entry for %s\n",
- trim_main_name, loginname);
- loginname = NULL;
- }
- }
- if (loginname != NULL) {
- strcpy (unametmp, loginname);
- } else {
- int euid;
-
- euid = getuid();
- if ((pw = (struct passwd *) getpwuid (euid)) == NULL) {
- unametmp[0] = '\0';
- } else {
- strcpy (unametmp, pw->pw_name);
- }
- }
- /*
- We need a temporary buffer for the current user name
- rather than passing pw->pw_name to process_it as the
- buffer pointed at by the returning getpwnam call will
- overwrite the passed parameter
- */
- process_it (&unametmp[0], pwinf);
- }
- while (optind < argc) {
- process_it (*(argv + optind++), pwinf);
- }
- exit (all_found ? 0 : 1);
- }
-
-
- int process_it( usrnam, pwinf)
- char *usrnam;
- struct pwinfrq *pwinf;
- {
- struct passwd *pwent;
- int nout = 0;
-
- pwent = (struct passwd *) getpwnam ( usrnam );
- if ( pwent == NULL) {
- all_found = 0;
- if (report_except) {
- fprintf (stderr, "%s: could not find user %s\n",
- trim_main_name, usrnam);
- }
- } else {
- nout = pwinf_fpr (stdout, pwent, pwinf);
- }
- return (nout);
- }
-
-
- int fpr_usage (fp)
- FILE *fp;
- {
- int nout = 0;
- nout += fprintf (stderr, "Usage: %s [-%s] [-%s] usrname [...]\n",
- trim_main_name, "Iacghnprsuv", "d char" );
- return (nout);
- }
-
-
-