Mercurial > hg > octave-shane > gnulib-hg
comparison lib/getgroups.c @ 4795:ac6111c4f643
Include <errno.h>, <stdlib.h>.
(getgroups): First arg is int, not size_t.
Don't let 'free' mangle errno.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Thu, 16 Oct 2003 07:30:56 +0000 |
parents | 66dca1409f3e |
children | b3e190d8e109 |
comparison
equal
deleted
inserted
replaced
4794:89793e77bb31 | 4795:ac6111c4f643 |
---|---|
18 /* written by Jim Meyering */ | 18 /* written by Jim Meyering */ |
19 | 19 |
20 #include <config.h> | 20 #include <config.h> |
21 #include <stdio.h> | 21 #include <stdio.h> |
22 #include <sys/types.h> | 22 #include <sys/types.h> |
23 #include <errno.h> | |
24 #include <stdlib.h> | |
23 | 25 |
24 #include "xalloc.h" | 26 #include "xalloc.h" |
25 | 27 |
26 /* On at least Ultrix 4.3 and NextStep 3.2, getgroups (0, 0) always fails. | 28 /* On at least Ultrix 4.3 and NextStep 3.2, getgroups (0, 0) always fails. |
27 On other systems, it returns the number of supplemental groups for the | 29 On other systems, it returns the number of supplemental groups for the |
28 process. This function handles that special case and lets the system- | 30 process. This function handles that special case and lets the system- |
29 provided function handle all others. */ | 31 provided function handle all others. */ |
30 | 32 |
31 int | 33 int |
32 getgroups (size_t n, GETGROUPS_T *group) | 34 getgroups (int n, GETGROUPS_T *group) |
33 { | 35 { |
34 int n_groups; | 36 int n_groups; |
35 GETGROUPS_T *gbuf; | 37 GETGROUPS_T *gbuf; |
38 int saved_errno; | |
36 | 39 |
37 #undef getgroups | 40 #undef getgroups |
38 | 41 |
39 if (n != 0) | 42 if (n != 0) |
40 return getgroups (n, group); | 43 return getgroups (n, group); |
41 | 44 |
42 n = 20; | 45 n = 20; |
43 gbuf = NULL; | 46 gbuf = NULL; |
44 while (1) | 47 while (1) |
45 { | 48 { |
49 /* No need to worry about address arithmetic overflow here, | |
50 since the ancient systems that we're running on have low | |
51 limits on the number of secondary groups. */ | |
46 gbuf = xrealloc (gbuf, n * sizeof (GETGROUPS_T)); | 52 gbuf = xrealloc (gbuf, n * sizeof (GETGROUPS_T)); |
47 n_groups = getgroups (n, gbuf); | 53 n_groups = getgroups (n, gbuf); |
48 if (n_groups < n) | 54 if (n_groups < n) |
49 break; | 55 break; |
50 n += 10; | 56 n += 10; |
51 } | 57 } |
52 | 58 |
59 saved_errno = errno; | |
53 free (gbuf); | 60 free (gbuf); |
61 errno = saved_errno; | |
54 | 62 |
55 return n_groups; | 63 return n_groups; |
56 } | 64 } |