(standard c libraries translation )getgrgid

getgrnam, getgrnam_r, getgrgid, getgrgid_r - get group file entry
getgrnam, getgrnam_r, getgrgid, getgrgid_r - 获取group文件的入口

所需头文件
#include <sys/types.h>
#include <grp.h>

struct group *getgrnam(const char *name);

struct group *getgrgid(gid_t gid);

int getgrnam_r(const char *name, struct group *grp,
         char *buf, size_t buflen, struct group **result);

int getgrgid_r(gid_t gid, struct group *grp,
         char *buf, size_t buflen, struct group **result);

The  getgrnam()  function  returns a pointer to a structure containing the broken-out fields of the record in the group database (e.g., the local group file /etc/group, NIS, and LDAP) that matches the group name name.
getgrnam函数返回一个指向包含group数据库结构体的指针,其中组名跟name匹配

The getgrgid() function returns a pointer to a structure containing the broken-out fields of the record in the group database that matches the group ID gid.
getgrgid函数返回一个指向包含group数据库结构体的指针,其中组id跟id匹配

The group structure is defined in <grp.h> as follows:

   struct group {
       char   *gr_name;       /* group name */
       char   *gr_passwd;     /* group password */
       gid_t   gr_gid;        /* group ID */
       char  **gr_mem;        /* group members */
   };

For more information about the fields of this structure, see group(5).

The  getgrnam_r()  and  getgrgid_r() functions obtain the same information as getgrnam() and getgrgid(), but store the retrieved group structure in the space pointed to by grp.  The string fields pointed to by the members of the group structure are stored in the buffer buf of size buflen.  A pointer to the result (in case of success) or NULL (in case no entry was found or an error occurred) is stored in *result.
getgrname_r和getgrgid_r函数与getgrname和getgrgid函数获取同样的信息,但是把得到的group结构体存储到grp指针中,group结构体里面的成员都存储在buflen大小的内存中,指向结果的指针(成功)或者NULL(如果没有入口或者发生错误)存储在result指针中

The call
    sysconf(_SC_GETGR_R_SIZE_MAX)
returns either -1, without changing errno, or an initial suggested size for buf.  (If this size is too small, the call fails with ERANGE, in which case the caller can retry with a larger buffer.)
sysconf(_SC_GETGR_R_SIZE_MAX)调用返回-1,但是没有设置errno,或者一个默认的建议buf size(如果size太小,调用会ERANGE出错,这种情况下调用者应该尝试一个大一些的内存)

The getgrnam() and getgrgid() functions return a pointer to a group structure, or NULL if the matching entry is not found or an error  occurs.   If  an error occurs, errno is set appropriately.  If one wants to check errno after the call, it should be set to zero before the call.
getgrnam和getgrgid函数返回一个指向组结构体的指针,或者NULL如果入口没有找到或者发生了错误,如果发生错误,errno会被设置成适当的值,如果需要在调用完毕后检测这个值,那么必须在调用前设置成0

The  return  value  may point to a static area, and may be overwritten by subsequent calls to getgrent(3), getgrgid(), or getgrnam().  (Do not pass the returned pointer to free(3).)
返回值指向静态数据区,可能会被getgrent(3), getgrgid(), or getgrnam()等函数的后续调用给覆盖(指向静态数据区的指针不能被free,你应该懂的哈)

On success, getgrnam_r() and getgrgid_r() return zero, and set *result to grp.  If no matching group record was found, these  functions  return  0  and store NULL in *result.  In case of error, an error number is returned, and NULL is stored in *result.
在成功的时候getgrnam_r和getgrgid_r返回0,把结果放到指向grp的指针result中,如果没有group记录被找到,这些函数返回0,result指针被赋值为NULL,如果发生错误,返回error号,result指针被赋值NULL

0 or ENOENT or ESRCH or EBADF or EPERM or ... The given name or gid was not found.
0或者ENOEN或者ESRCH或者EBADF或者EPERM或者。。。找不到指定名字的gid

EINTR  A signal was caught.
一个信号被捕捉到
EIO    I/O error.
I/O错误
EMFILE The maximum number (OPEN_MAX) of files was open already in the calling process.
调用的进程已经打开了最多的文件
ENFILE The maximum number of files was open already in the system.
系统已经打开了最多的文件
ENOMEM Insufficient memory to allocate group structure.
分配组结构体的时候内存不足
ERANGE Insufficient buffer space supplied.
提供的内存空间不足

FILES /etc/group local group database file、
/etc/group 本地组数据库文件


testcase如下:

#include <sys/types.h>
#include <grp.h>
#include <stdio.h>

int main(void)
{
	struct group *grp;
	const char *name = "cheny";
	gid_t tmp;

	grp = getgrnam("cheny");
	printf("cheny gid = %d\n", grp->gr_gid);

	tmp = grp->gr_gid;
	grp = getgrgid(tmp);
	printf("group name %d = %s\n", tmp, grp->gr_name);
	return 0;
}

运行结果如下:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ ./a.out
cheny gid = 1000
group name 1000 = cheny


验证一下/etc/group里面是否是这样子的:

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ cat /etc/group |grep cheny
adm:x:4:cheny
cdrom:x:24:cheny
sudo:x:27:cheny,cheny.le
dip:x:30:cheny
plugdev:x:46:cheny
lpadmin:x:108:cheny
cheny:x:1000:
sambashare:x:124:cheny
cheny.le:x:1001:cheny.le
vboxusers:x:125:cheny.le

cheny.le@cheny-ThinkPad-T420:~/cheny/testCode$ cat /etc/group |grep 1000
cheny:x:1000:


可以发现cheny的gid的确是1000,而gid为1000的gid name的确是cheny

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值