home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / shadow / part12 / groups.c < prev    next >
C/C++ Source or Header  |  1993-08-14  |  3KB  |  133 lines

  1. /*
  2.  * Copyright 1991, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. static    char    sccsid[] = "@(#)groups.c    3.2    09:47:19    25 Nov 1991";
  13.  
  14. #include "stdio.h"
  15. #include "pwd.h"
  16. #include "grp.h"
  17.  
  18. /*
  19.  * print_groups - print the groups which the named user is a member of
  20.  *
  21.  *    print_groups() scans the groups file for the list of groups
  22.  *    which the user is listed as being a member of.
  23.  */
  24.  
  25. print_groups (member)
  26. char    *member;
  27. {
  28.     int    i, groups = 0;
  29.     struct    group    *grp;
  30.     struct    group    *getgrent();
  31.  
  32.     setgrent ();
  33.  
  34.     while (grp = getgrent ()) {
  35.         for (i = 0;grp->gr_mem[i];i++) {
  36.             if (strcmp (grp->gr_mem[i], member) == 0) {
  37.                 if (groups++)
  38.                     putchar (' ');
  39.  
  40.                 printf ("%s", grp->gr_name);
  41.             }
  42.         }
  43.     }
  44.     if (groups)
  45.         putchar ('\n');
  46. }
  47.  
  48. /*
  49.  * groups - print out the groups a process is a member of
  50.  */
  51.  
  52. main (argc, argv)
  53. int    argc;
  54. char    **argv;
  55. {
  56.     int    ngroups;
  57. #if NGROUPS > 0
  58. #if NGROUPS > 100
  59.     gid_t    *groups;
  60. #else
  61.     gid_t    groups[NGROUPS];
  62. #endif
  63.     int    i;
  64. #else
  65.     char    *logname;
  66.     char    *getlogin();
  67. #endif
  68.     struct    group    *gr;
  69.     struct    group    *getgrgid();
  70.  
  71.     if (argc == 1) {
  72.  
  73.         /*
  74.          * Called with no arguments - give the group set
  75.          * for the current user.
  76.          */
  77.  
  78. #if NGROUPS > 0
  79.         /*
  80.          * This system supports concurrent group sets, so
  81.          * I can ask the system to tell me which groups are
  82.          * currently set for this process.
  83.          */
  84.  
  85.         ngroups = getgroups (0, 0);
  86. #if NGROUPS > 100
  87.         groups = (gid_t *) malloc (ngroups * sizeof (int *));
  88. #endif
  89.         getgroups (ngroups, groups);
  90.  
  91.         /*
  92.          * Print out the name of every group in the current
  93.          * group set.  Unknown groups are printed as their
  94.          * decimal group ID values.
  95.          */
  96.  
  97.         for (i = 0;i < ngroups;i++) {
  98.             if (i)
  99.                 putchar (' ');
  100.  
  101.             if (gr = getgrgid (groups[i]))
  102.                 printf ("%s", gr->gr_name);
  103.             else
  104.                 printf ("%d", groups[i]);
  105.         }
  106.         putchar ('\n');
  107. #else
  108.         /*
  109.          * This system does not have the getgroups() system
  110.          * call, so I must check the groups file directly.
  111.          */
  112.  
  113.         if (logname = getlogin ())
  114.             print_groups (logname);
  115.         else
  116.             exit (1);
  117. #endif
  118.     } else {
  119.  
  120.         /*
  121.          * The invoker wanted to know about some other
  122.          * user.  Use that name to look up the groups instead.
  123.          */
  124.  
  125.         if (getpwnam (argv[1]) == 0) {
  126.             fprintf (stderr, "unknown user %s\n", argv[1]);
  127.             exit (1);
  128.         }
  129.         print_groups (argv[1]);
  130.     }
  131.     exit (0);
  132. }
  133.