Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/group-member.c @ 9268:a553c18c572d
Rename sys_time_.h to sys_time.in.h.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Tue, 02 Oct 2007 00:37:40 +0200 |
parents | 8a1a9361108c |
children | bbbbbf4cd1c5 |
rev | line source |
---|---|
785 | 1 /* group-member.c -- determine whether group id is in calling user's group list |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
2 |
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
3 Copyright (C) 1994, 1997, 1998, 2003, 2005, 2006 Free Software |
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
4 Foundation, Inc. |
785 | 5 |
6 This program is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
4830
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
785 | 19 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
20 #include <config.h> |
785 | 21 |
4662 | 22 #include "group-member.h" |
23 | |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
24 #include <stdbool.h> |
785 | 25 #include <stdio.h> |
26 #include <sys/types.h> | |
4662 | 27 #include <stdlib.h> |
785 | 28 |
6275 | 29 #include <unistd.h> |
785 | 30 |
4542 | 31 #include "xalloc.h" |
785 | 32 |
33 struct group_info | |
34 { | |
35 int n_groups; | |
36 GETGROUPS_T *group; | |
37 }; | |
38 | |
1278
9ad625a30b7d
Use #if, not #ifdef with HAVE_ macros
Jim Meyering <jim@meyering.net>
parents:
1188
diff
changeset
|
39 #if HAVE_GETGROUPS |
785 | 40 |
41 static void | |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
42 free_group_info (struct group_info const *g) |
785 | 43 { |
44 free (g->group); | |
45 } | |
46 | |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
47 static bool |
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
48 get_group_info (struct group_info *gi) |
785 | 49 { |
50 int n_groups; | |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
51 int n_group_slots = getgroups (0, NULL); |
785 | 52 GETGROUPS_T *group; |
53 | |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
54 if (n_group_slots < 0) |
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
55 return false; |
785 | 56 |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
57 /* Avoid xnmalloc, as it goes awry when SIZE_MAX < n_group_slots. */ |
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
58 if (xalloc_oversized (n_group_slots, sizeof *group)) |
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
59 xalloc_die (); |
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
60 group = xmalloc (n_group_slots * sizeof *group); |
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
61 n_groups = getgroups (n_group_slots, group); |
785 | 62 |
63 /* In case of error, the user loses. */ | |
64 if (n_groups < 0) | |
65 { | |
66 free (group); | |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
67 return false; |
785 | 68 } |
69 | |
70 gi->n_groups = n_groups; | |
71 gi->group = group; | |
72 | |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
73 return true; |
785 | 74 } |
75 | |
76 #endif /* not HAVE_GETGROUPS */ | |
77 | |
78 /* Return non-zero if GID is one that we have in our groups list. | |
79 If there is no getgroups function, return non-zero if GID matches | |
80 either of the current or effective group IDs. */ | |
81 | |
82 int | |
1188 | 83 group_member (gid_t gid) |
785 | 84 { |
85 #ifndef HAVE_GETGROUPS | |
86 return ((gid == getgid ()) || (gid == getegid ())); | |
87 #else | |
88 int i; | |
89 int found; | |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
90 struct group_info gi; |
785 | 91 |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
92 if (! get_group_info (&gi)) |
785 | 93 return 0; |
94 | |
95 /* Search through the list looking for GID. */ | |
96 found = 0; | |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
97 for (i = 0; i < gi.n_groups; i++) |
785 | 98 { |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
99 if (gid == gi.group[i]) |
785 | 100 { |
101 found = 1; | |
102 break; | |
103 } | |
104 } | |
790 | 105 |
4830
410ea3e253b9
Revamp xalloc_oversized so that its count arg need not fit into size_t.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
106 free_group_info (&gi); |
785 | 107 |
108 return found; | |
109 #endif /* HAVE_GETGROUPS */ | |
110 } | |
111 | |
112 #ifdef TEST | |
113 | |
114 char *program_name; | |
115 | |
116 int | |
4662 | 117 main (int argc, char **argv) |
785 | 118 { |
119 int i; | |
120 | |
121 program_name = argv[0]; | |
122 | |
123 for (i=1; i<argc; i++) | |
124 { | |
125 gid_t gid; | |
126 | |
127 gid = atoi (argv[i]); | |
128 printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no"); | |
129 } | |
130 exit (0); | |
131 } | |
132 | |
133 #endif /* TEST */ |