Methods to set and get the number of code groups in C programming

  • 2020-04-02 03:19:23
  • OfStack

C language setgroups() function: set the group code function
The header file:


 #include <grp.h>

Definition 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 setting of the current process. The parameter size is the number of gid_t of list(), and the maximum value is NGROUP(32).

Return value: 0 for success or -1 for error.

Error code:

EFAULT: parameter list array address is invalid. EPERM: insufficient permissions, must be root EINVAL: the parameter size value is greater than NGROUP(32).

C getgroups() function: gets the group code function
The header file:


#include <unistd.h>  #include <sys/types.h>

Definition function:


int getgroups(int size, gid_t list[]);

Getgroup () is used to get the code of the group to which the current user belongs.

Return value: returns the group identifier or -1 for errors.

Error code:

EFAULT: parameter list array address is invalid. EINVAL: the parameter size value is not enough to hold all groups.

sample


#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:%dn", i, list[i]);
}

Perform:


0:00
1:01
2:02
3:03
4:04
5:06
6:10


Related articles: