Import upstream version 1.28
[debian/tar] / gnu / group-member.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* group-member.c -- determine whether group id is in calling user's group list
4
5    Copyright (C) 1994, 1997-1998, 2003, 2005-2006, 2009-2014 Free Software
6    Foundation, Inc.
7
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include <config.h>
22
23 /* Specification.  */
24 #include <unistd.h>
25
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <stdlib.h>
29
30 #include "xalloc-oversized.h"
31
32 /* Most processes have no more than this many groups, and for these
33    processes we can avoid using malloc.  */
34 enum { GROUPBUF_SIZE = 100 };
35
36 struct group_info
37   {
38     gid_t *group;
39     gid_t groupbuf[GROUPBUF_SIZE];
40   };
41
42 static void
43 free_group_info (struct group_info const *g)
44 {
45   if (g->group != g->groupbuf)
46     free (g->group);
47 }
48
49 static int
50 get_group_info (struct group_info *gi)
51 {
52   int n_groups = getgroups (GROUPBUF_SIZE, gi->groupbuf);
53   gi->group = gi->groupbuf;
54
55   if (n_groups < 0)
56     {
57       int n_group_slots = getgroups (0, NULL);
58       if (0 <= n_group_slots
59           && ! xalloc_oversized (n_group_slots, sizeof *gi->group))
60         {
61           gi->group = malloc (n_group_slots * sizeof *gi->group);
62           if (gi->group)
63             n_groups = getgroups (n_group_slots, gi->group);
64         }
65     }
66
67   /* In case of error, the user loses.  */
68   return n_groups;
69 }
70
71 /* Return non-zero if GID is one that we have in our groups list.
72    Note that the groups list is not guaranteed to contain the current
73    or effective group ID, so they should generally be checked
74    separately.  */
75
76 int
77 group_member (gid_t gid)
78 {
79   int i;
80   int found;
81   struct group_info gi;
82   int n_groups = get_group_info (&gi);
83
84   /* Search through the list looking for GID. */
85   found = 0;
86   for (i = 0; i < n_groups; i++)
87     {
88       if (gid == gi.group[i])
89         {
90           found = 1;
91           break;
92         }
93     }
94
95   free_group_info (&gi);
96
97   return found;
98 }
99
100 #ifdef TEST
101
102 char *program_name;
103
104 int
105 main (int argc, char **argv)
106 {
107   int i;
108
109   program_name = argv[0];
110
111   for (i = 1; i < argc; i++)
112     {
113       gid_t gid;
114
115       gid = atoi (argv[i]);
116       printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
117     }
118   exit (0);
119 }
120
121 #endif /* TEST */