home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / idutil / getpwnam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-12  |  3.0 KB  |  151 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <pwd.h>
  4. #include "pwinf.h"
  5. #include <gnu/fileutil.h>
  6. static char    *RCSid = "$Header: getpwnam.c,v 1.4 92/02/25 14:00:08 dtb Exp $";
  7.  
  8. int    fpr_usage();
  9. int    process_it();
  10.  
  11. static int    report_except = 0;
  12. static char    all_found     = 1;
  13. static char    *trim_main_name;
  14.  
  15.  
  16. int    main ( argc, argv, envp )
  17. int    argc;
  18. char    **argv;
  19. char    **envp;
  20. {
  21.     char    *optstring = "acd:Ighnprsuv" ;
  22.     int    optchar;
  23.     extern char    *optarg;
  24.     extern int    optind;
  25.     extern int    opterr;
  26.     struct pwinfrq *pwinf;
  27.  
  28.     trim_main_name = basename(*argv);
  29.     if ((pwinf = pwinfrq_set_dflt (NULL)) == NULL)
  30.         exit (ENOMEM);
  31.     opterr = 0;
  32.     while ((optchar = getopt(argc, argv, optstring)) != -1)
  33.         switch (optchar) {
  34.         case 'I':
  35.             fprintf (stderr, "%s %s\n", __FILE__, RCSid);
  36.             pwinf_info_fpr (stderr);
  37.             break;
  38.         case 'a':
  39.             pwinf->epw = pwinf->nam = pwinf->uid = pwinf->gid
  40.                  = pwinf->age = pwinf->cmt = pwinf->dir =
  41.                 pwinf->shl = 1;
  42.             break;
  43.         case 'c':
  44.             pwinf->cmt = 1;
  45.             break;
  46.         case 'd':
  47.             pwinf->dlm = *optarg;
  48.             break;
  49.         case 'g':
  50.             pwinf->gid = 1;
  51.             break;
  52.         case 'h':
  53.             pwinf->dir = 1;
  54.             break;
  55.         case 'n':
  56.             pwinf->nam = 1;
  57.             break;
  58.         case 'p':
  59.             pwinf->epw = 1;
  60.             break;
  61.         case 'r':
  62.             report_except = 1;
  63.             break;
  64.         case 's':
  65.             pwinf->shl = 1;
  66.             break;
  67.         case 'u':
  68.             pwinf->uid = 1;
  69.             break;
  70.         case 'v':
  71.             pwinf->vbs = 1;
  72.             break;
  73.         case '?':
  74.             fpr_usage (stderr);
  75.             exit (EINVAL);
  76.         }
  77.     if (optind == argc) {
  78.         struct passwd *pw = NULL;
  79.         char    unametmp[256];
  80.         char    *loginname = NULL;
  81.  
  82.  
  83.         if ((loginname = cuserid(NULL)) == NULL)
  84.             loginname = (char *) getlogin();
  85.         if (loginname != NULL) {
  86.             if ((pw = (struct passwd *) getpwnam (loginname))
  87.                 == NULL) {
  88.                 fprintf (stderr, "%s: No password entry for %s\n",
  89.                                          trim_main_name, loginname);
  90.                 loginname = NULL;
  91.             }
  92.         }
  93.         if (loginname != NULL) {
  94.             strcpy (unametmp, loginname);
  95.         } else {
  96.             int    euid;
  97.  
  98.             euid = getuid();
  99.             if ((pw = (struct passwd *) getpwuid (euid)) == NULL) {
  100.                 unametmp[0] = '\0';
  101.             } else {
  102.                 strcpy (unametmp, pw->pw_name);
  103.             }
  104.         }
  105.         /* 
  106.         We need a temporary buffer for the current user name
  107.         rather than passing pw->pw_name to process_it as the
  108.         buffer pointed at by the returning getpwnam call will
  109.         overwrite the passed parameter
  110.         */
  111.         process_it (&unametmp[0], pwinf);
  112.     }
  113.     while (optind < argc) {
  114.         process_it (*(argv + optind++), pwinf);
  115.     }
  116.     exit (all_found ? 0 : 1);
  117. }
  118.  
  119.  
  120. int    process_it( usrnam, pwinf)
  121. char    *usrnam;
  122. struct pwinfrq *pwinf;
  123. {
  124.     struct passwd *pwent;
  125.     int    nout = 0;
  126.  
  127.     pwent = (struct passwd *) getpwnam ( usrnam );
  128.     if ( pwent == NULL) {
  129.         all_found = 0;
  130.         if (report_except) {
  131.             fprintf (stderr, "%s: could not find user %s\n",
  132.                                  trim_main_name, usrnam);
  133.         }
  134.     } else {
  135.         nout = pwinf_fpr (stdout, pwent, pwinf);
  136.     }
  137.     return (nout);
  138. }
  139.  
  140.  
  141. int    fpr_usage (fp)
  142. FILE     *fp;
  143. {
  144.     int    nout = 0;
  145.     nout += fprintf (stderr,  "Usage: %s [-%s] [-%s] usrname [...]\n",
  146.                  trim_main_name, "Iacghnprsuv", "d char" );
  147.     return (nout);
  148. }
  149.  
  150.  
  151.