Ways to set and get code groups in C programming _c language

Source: Internet
Author: User
Tags int size

C language Setgroups () function: Set Group code function
header file:

 #include <grp.h>

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.