Detail the getgrgid of function and getgrnam of function in C language
- 2020-04-02 03:19:31
- OfStack
C getgrgid() function: gets the data for the specified gid from the group file
The header file:
#include <grp.h> #include <sys/types.h>
Definition function:
strcut group * getgrgid(gid_t gid);
Getgrgid () is used to search group files one by one according to the group identification number specified by parameter gid, and returns the group data as a group structure when found.
sample
#include <grp.h>
#include <sys/types.h>
main()
{
strcut group *data;
int i = 0;
data = getgrgid(3);
printf("%s:%s:%d:", data->gr_name, data->gr_passwd, data->gr_gid);
while(data->gr_mem[i])
printf("%s, ", data->mem[i++]);
printf("n");
}
Perform:
sys:x:3:root, bin, adm
C getgrnam() function: gets the data for the specified group from the group file
The header file:
#include <grp.h> #include <sys/types.h>
Definition function:
strcut group * getgrnam(const char * name);
Getgrnam () is used to search one by one for the specified group name, and returns the group's data as a group structure when found. Refer to getgrent() for the group structure.
Return value: returns group structure data. If NULL is returned, there is no data or an error has occurred.
sample
#include <grp.h>
#include <sys/types.h>
main()
{
strcut group * data;
int i = 0;
data = getgrnam("adm");
printf("%s:%s:%d:", data->gr_name, data->gr_passwd, data->gr_gid);
while(data->gr_mem[i])
printf("%s, ", data->gr_mem[i++]);
printf("n");
}
Perform:
adm:x:4:root, adm, daemon