OpenCoverage

group-list.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/group-list.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* group-list.c --Print a list of group IDs or names.-
2 Copyright (C) 1989-2018 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 <https://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. */-
35extern bool-
36print_group_list (const char *username,-
37 uid_t ruid, gid_t rgid, gid_t egid,-
38 bool use_names, char delim)-
39{-
40 bool ok = true;-
41 struct passwd *pwd = NULL;-
42-
43 if (username)
usernameDescription
TRUEevaluated 35 times by 1 test
Evaluated by:
  • id
FALSEevaluated 15 times by 1 test
Evaluated by:
  • id
15-35
44 {-
45 pwd = getpwuid (ruid);-
46 if (pwd == NULL)
pwd == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 35 times by 1 test
Evaluated by:
  • id
0-35
47 ok = false;
never executed: ok = 0 ;
0
48 }
executed 35 times by 1 test: end of block
Executed by:
  • id
35
49-
50 if (!print_group (rgid, use_names))
!print_group (rgid, use_names)Description
TRUEnever evaluated
FALSEevaluated 50 times by 1 test
Evaluated by:
  • id
0-50
51 ok = false;
never executed: ok = 0 ;
0
52-
53 if (egid != rgid)
egid != rgidDescription
TRUEnever evaluated
FALSEevaluated 50 times by 1 test
Evaluated by:
  • id
0-50
54 {-
55 putchar (delim);-
56 if (!print_group (egid, use_names))
!print_group (egid, use_names)Description
TRUEnever evaluated
FALSEnever evaluated
0
57 ok = false;
never executed: ok = 0 ;
0
58 }
never executed: end of block
0
59-
60 {-
61 gid_t *groups;-
62-
63 int n_groups = xgetgroups (username, (pwd ? pwd->pw_gid : egid), &groups);-
64 if (n_groups < 0)
n_groups < 0Description
TRUEnever evaluated
FALSEevaluated 50 times by 1 test
Evaluated by:
  • id
0-50
65 {-
66 if (username)
usernameDescription
TRUEnever evaluated
FALSEnever evaluated
0
67 {-
68 error (0, errno, _("failed to get groups for user %s"),-
69 quote (username));-
70 }
never executed: end of block
0
71 else-
72 {-
73 error (0, errno, _("failed to get groups for the current process"));-
74 }
never executed: end of block
0
75 return false;
never executed: return 0 ;
0
76 }-
77-
78 for (int i = 0; i < n_groups; i++)
i < n_groupsDescription
TRUEevaluated 310 times by 1 test
Evaluated by:
  • id
FALSEevaluated 50 times by 1 test
Evaluated by:
  • id
50-310
79 if (groups[i] != rgid && groups[i] != egid)
groups[i] != rgidDescription
TRUEevaluated 260 times by 1 test
Evaluated by:
  • id
FALSEevaluated 50 times by 1 test
Evaluated by:
  • id
groups[i] != egidDescription
TRUEevaluated 260 times by 1 test
Evaluated by:
  • id
FALSEnever evaluated
0-260
80 {-
81 putchar (delim);-
82 if (!print_group (groups[i], use_names))
!print_group (...i], use_names)Description
TRUEnever evaluated
FALSEevaluated 260 times by 1 test
Evaluated by:
  • id
0-260
83 ok = false;
never executed: ok = 0 ;
0
84 }
executed 260 times by 1 test: end of block
Executed by:
  • id
260
85 free (groups);-
86 }-
87 return ok;
executed 50 times by 1 test: return ok;
Executed by:
  • id
50
88}-
89-
90/* Convert a gid_t to string. Do not use this function directly.-
91 Instead, use it via the gidtostr macro.-
92 Beware that it returns a pointer to static storage. */-
93static char *-
94gidtostr_ptr (gid_t const *gid)-
95{-
96 static char buf[INT_BUFSIZE_BOUND (uintmax_t)];-
97 return umaxtostr (*gid, buf);
executed 237 times by 1 test: return umaxtostr (*gid, buf);
Executed by:
  • id
237
98}-
99#define gidtostr(g) gidtostr_ptr (&(g))-
100-
101/* Print the name or value of group ID GID. */-
102extern bool-
103print_group (gid_t gid, bool use_name)-
104{-
105 struct group *grp = NULL;-
106 bool ok = true;-
107-
108 if (use_name)
use_nameDescription
TRUEevaluated 121 times by 1 test
Evaluated by:
  • id
FALSEevaluated 237 times by 1 test
Evaluated by:
  • id
121-237
109 {-
110 grp = getgrgid (gid);-
111 if (grp == NULL)
grp == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 121 times by 1 test
Evaluated by:
  • id
0-121
112 {-
113 error (0, 0, _("cannot find name for group ID %lu"),-
114 (unsigned long int) gid);-
115 ok = false;-
116 }
never executed: end of block
0
117 }
executed 121 times by 1 test: end of block
Executed by:
  • id
121
118-
119 char *s = grp ? grp->gr_name : gidtostr (gid);
grpDescription
TRUEevaluated 121 times by 1 test
Evaluated by:
  • id
FALSEevaluated 237 times by 1 test
Evaluated by:
  • id
121-237
120 fputs (s, stdout);-
121 return ok;
executed 358 times by 1 test: return ok;
Executed by:
  • id
358
122}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2