C language Setgroups () function: Set Group code function
header file:
To define a function:
int setgroups (size_t size, const gid_t * list);
Function Description: Setgroups () is used to add the groups indicated in the list array to the group settings of the current process. The gid_t number of the parameter size is list () and the maximum value is Ngroup (32).
Return value: Returns 0 if the setting succeeds, or 1 if there is an error.
Error code:
- Efault: Parameter list array address is not valid.
- Eperm: Insufficient permissions, must be root permission
- Einval: Parameter size value is greater than ngroup (32).
C language GetGroups () function: Getting Group code functions
header file:
#include <unistd.h> #include <sys/types.h>
To define a function:
int getgroups (int size, gid_t list[]);
Function Description: Getgroup () is used to obtain the group code that the current user belongs to. The parameter size is the number of gid_t that list () can hold. If the parameter size value is zero, this function returns only the number of groups to which the user belongs.
Return value: Returns the group identifier, or returns 1 if there is an error.
Error code:
- Efault: Parameter list array address is not valid.
- Einval: Parameter size value is not sufficient to accommodate all groups.
Example
#include <unistd.h>
#include <sys/types.h>
main ()
{
gid_t list[500];
int x, I;
x = getgroups (0, list);
GetGroups (x, list);
for (i = 0; i < x; i++)
printf ("%d:%d\n", I, List[i]);
Perform:
0:00
1:01
2:02
3:03
4:04
5:06
6:10