加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C语言编程中设置和获取代码组数的方法

发布时间:2020-12-16 03:04:14 所属栏目:百科 来源:网络整理
导读:C语言setgroups()函数:设置组代码函数 头文件: #include grp.h 定义函数: int setgroups(size_t size,const gid_t * list); 函数说明:setgroups()用来将list 数组中所标明的组加入到目前进程的组设置中. 参数size 为list()的gid_t 数目,最大值为NGROUP(3

C语言setgroups()函数:设置组代码函数
头文件:

 #include <grp.h>

定义函数:

int setgroups(size_t size,const gid_t * list);

函数说明:setgroups()用来将list 数组中所标明的组加入到目前进程的组设置中. 参数size 为list()的gid_t 数目,最大值为NGROUP(32)。

返回值:设置成功则返回0,如有错误则返回-1.

错误代码:

  • EFAULT:参数list 数组地址不合法.
  • EPERM:权限不足,必须是root 权限
  • EINVAL:参数size 值大于NGROUP(32).

C语言getgroups()函数:获取组代码函数
头文件:

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

定义函数:

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

函数说明:getgroup() 用来取得目前用户所属的组代码. 参数size 为list() 所能容纳的gid_t 数目. 如果参数size 值为零,此函数仅会返回用户所属的组数。

返回值:返回组识别码,如有错误则返回-1.

错误代码:

  • EFAULT:参数list 数组地址不合法.
  • EINVAL:参数size 值不足以容纳所有的组.

范例

#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]);
}

执行:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读