LCOV - code coverage report
Current view: top level - src - group-list.c (source / functions) Hit Total Coverage
Test: coreutils.info Lines: 22 36 61.1 %
Date: 2018-01-30 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* group-list.c --Print a list of group IDs or names.
       2             :    Copyright (C) 1989-2008 Free Software Foundation, Inc.
       3             : 
       4             :    This program is free software: you can redistribute it and/or modify
       5             :    it under the terms of the GNU General Public License as published by
       6             :    the Free Software Foundation, either version 3 of the License, or
       7             :    (at your option) any later version.
       8             : 
       9             :    This program is distributed in the hope that it will be useful,
      10             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :    GNU General Public License for more details.
      13             : 
      14             :    You should have received a copy of the GNU General Public License
      15             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      16             : 
      17             : /* Written by Arnold Robbins.
      18             :    Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu.
      19             :    Extracted from id.c by James Youngman. */
      20             : 
      21             : #include <config.h>
      22             : #include <stdio.h>
      23             : #include <sys/types.h>
      24             : #include <pwd.h>
      25             : #include <grp.h>
      26             : 
      27             : #include "system.h"
      28             : #include "error.h"
      29             : #include "mgetgroups.h"
      30             : #include "quote.h"
      31             : #include "group-list.h"
      32             : 
      33             : 
      34             : /* Print all of the distinct groups the user is in. */
      35             : extern bool
      36           1 : print_group_list (const char *username,
      37             :                   uid_t ruid, gid_t rgid, gid_t egid,
      38             :                   bool use_names)
      39             : {
      40           1 :   bool ok = true;
      41             :   struct passwd *pwd;
      42             : 
      43           1 :   pwd = getpwuid (ruid);
      44           1 :   if (pwd == NULL)
      45           0 :     ok = false;
      46             : 
      47           1 :   if (!print_group (rgid, use_names))
      48           0 :     ok = false;
      49             : 
      50           1 :   if (egid != rgid)
      51             :     {
      52           0 :       putchar (' ');
      53           0 :       if (!print_group (egid, use_names))
      54           0 :         ok = false;
      55             :     }
      56             : 
      57             : #if HAVE_GETGROUPS
      58             :   {
      59             :     GETGROUPS_T *groups;
      60             :     size_t i;
      61             : 
      62           1 :     int n_groups = mgetgroups (username, (pwd ? pwd->pw_gid : (gid_t) -1),
      63             :                                &groups);
      64           1 :     if (n_groups < 0)
      65             :       {
      66           0 :         if (username)
      67             :           {
      68           0 :             error (0, errno, _("failed to get groups for user %s"),
      69             :                    quote (username));
      70             :           }
      71             :         else
      72             :           {
      73           0 :             error (0, errno, _("failed to get groups for the current process"));
      74             :           }
      75           0 :         return false;
      76             :       }
      77             : 
      78           2 :     for (i = 0; i < n_groups; i++)
      79           1 :       if (groups[i] != rgid && groups[i] != egid)
      80             :         {
      81           0 :           putchar (' ');
      82           0 :           if (!print_group (groups[i], use_names))
      83           0 :             ok = false;
      84             :         }
      85           1 :     free (groups);
      86           1 :     return ok;
      87             :   }
      88             : #endif /* HAVE_GETGROUPS */
      89             : }
      90             : 
      91             : 
      92             : /* Print the name or value of group ID GID. */
      93             : extern bool
      94           3 : print_group (gid_t gid, bool use_name)
      95             : {
      96           3 :   struct group *grp = NULL;
      97           3 :   bool ok = true;
      98             : 
      99           3 :   if (use_name)
     100             :     {
     101           1 :       grp = getgrgid (gid);
     102           1 :       if (grp == NULL)
     103             :         {
     104           0 :           error (0, 0, _("cannot find name for group ID %lu"),
     105             :                  (unsigned long int) gid);
     106           0 :           ok = false;
     107             :         }
     108             :     }
     109             : 
     110           3 :   if (grp == NULL)
     111           2 :     printf ("%lu", (unsigned long int) gid);
     112             :   else
     113           1 :     printf ("%s", grp->gr_name);
     114           3 :   return ok;
     115             : }
     116             : 
     117             : /*
     118             :  * Local variables:
     119             :  *  indent-tabs-mode: nil
     120             :  * End:
     121             :  */

Generated by: LCOV version 1.10