在C语言编程中设置和获取代码组数的方法
在C语言编程中设置和获取代码组数的方法
发布时间:2016-12-28 来源:查字典编辑
摘要:C语言setgroups()函数:设置组代码函数头文件:#include定义函数:intsetgroups(size_tsize,const...

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

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C语言学习
热门C语言学习
编程开发子分类