c73cd21650d75b43375ddb9458542da6839a70d0
[samba.git] / source / tests / getgroups.c
1 /* this tests whether getgroups actually returns lists of integers
2    rather than gid_t. The test only works if the user running
3    the test is in at least 1 group 
4
5    The test is designed to check for those broken OSes that define
6    getgroups() as returning an array of gid_t but actually return a
7    array of ints! Ultrix is one culprit
8   */
9
10 #if defined(HAVE_UNISTD_H)
11 #include <unistd.h>
12 #endif
13
14 #if defined(HAVE_STDLIB_H)
15 #include <stdlib.h>
16 #endif
17
18 #include <sys/types.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <grp.h>
22
23 main()
24 {
25         int i;
26         int *igroups;
27         char *cgroups;
28         int grp = 0;
29         int  ngroups = getgroups(0,&grp);
30
31         if (sizeof(gid_t) == sizeof(int)) {
32                 fprintf(stderr,"gid_t and int are the same size\n");
33                 return 1;
34         }
35
36         if (ngroups <= 0)
37                 ngroups = 32;
38
39         igroups = (int *)malloc(sizeof(int)*ngroups);
40
41         for (i=0;i<ngroups;i++)
42                 igroups[i] = 0x42424242;
43
44         ngroups = getgroups(ngroups,(gid_t *)igroups);
45
46         if (igroups[0] == 0x42424242)
47                 ngroups = 0;
48
49         if (ngroups == 0) {
50                 printf("WARNING: can't determine getgroups return type\n");
51                 return 1;
52         }
53         
54         cgroups = (char *)igroups;
55
56         if (ngroups == 1 && 
57             cgroups[2] == 0x42 && cgroups[3] == 0x42) {
58                 fprintf(stderr,"getgroups returns gid_t\n");
59                 return 1;
60         }
61           
62         for (i=0;i<ngroups;i++) {
63                 if (igroups[i] == 0x42424242) {
64                         fprintf(stderr,"getgroups returns gid_t\n");
65                         return 1;
66                 }
67         }
68
69         return 0;
70 }