C language Getgrent () function: Get account data from group file
header file:
#include <grp.h> #include <sys/types.h>
To define a function:
struct Group * getgrent (void);
Function Description: Getgrent () is used to read a group of data from a group file (/etc/group), which is returned as a team structure. The first group data is obtained on the first call, and the next data is returned every time it is invoked, and Null is returned until no data is already in progress.
struct group
{
char *gr_name;//Group name
char *gr_passwd;//Group password
gid_t gr_gid;//Group Identification code
char **gr_mem;// Group member account
}
Return value: Returns the group structure data, if returned null indicates no data, or an error occurred.
Additional note: Getgrent () opens the group file on the first call, and you can use Endgrent () to close the group file after reading the data.
Error code:
Enomem: There is not enough memory to configure the group structure.
Example
#include <grp.h>
#include <sys/types.h>
main ()
{
struct group *data;
int i;
while (data = Getgrent ())!= 0)
{
i = 0;
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");
}
Endgrent ();
}
Perform:
Root:x:0:root,
bin:x:1:root, bin, daemon,
daemon:x:2:root, bin, daemon,
sys:x:3:root, Bin, ADM,
Adm:x:4:root, ADM, daemon
tty:x:5
disk:x:6:root
lp:x:7:daemon, LP
Mem:x:8
kmem:x:9:
wheel:x:10:root
mail:x:12:mail
news:x:13:news
uucp:x:14:uucp
man:x:15:
games:x :
gopher:x:30
dip:x:40
ftp:x:50
nobody:x:99
C language Setgrent () function: Read group data from the group file from scratch
header file:
#include <grp.h> #include <sys/types.h>
To define a function:
Function Description: Setgrent () is used to refer the read-write address of getgrent () to the beginning of the group file.
Usage Reference setpwent ().
C language Endgrent () function: Close file (Close group file)
related functions:
Header file:
#include <grp.h> #include <sys/types.h>
To define a function:
Function Description: Endgrent () is used to close the password file opened by Getgrent ().
Please refer to setgrent () for examples.