C language on the group of file processing related functions summary
- 2020-04-02 03:19:35
- OfStack
C getgrent() function: get the account data from the group file
The header file:
#include <grp.h> #include <sys/types.h>
Definition function:
struct group * getgrent(void);
Getgrent () is used to read an item group data from the group file (/etc/group), which is returned as a group structure.
struct group
{
char *gr_name; //Group name
char *gr_passwd; //Set the password
gid_t gr_gid; //Group identification number
char **gr_mem; //Group member account
}
Return value: returns group structure data. If NULL is returned, there is no data or an error has occurred.
Note: getgrent() opens the group file on the first call and can be closed using endgrent() after reading the data.
Error code:
ENOMEM: out of memory to configure the group structure.
sample
#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:20
gopher:x:30
dip:x:40
ftp:x:50
nobody:x:99
C language setgrent() function: reads group data from the group file from scratch
The header file:
#include <grp.h> #include <sys/types.h>
Definition function:
void setgrent(void);
Setgrent () is used to point the read and write address of getgrent() back to the beginning of the group file.
Usage reference setpwent().
C endgrent() function: close the file (close the group file)
Related functions:
getgrent, setgrent
The header file:
#include <grp.h> #include <sys/types.h>
Definition function:
void endgrent(void);
Endgrent () is used to close the password file opened by getgrent().
See setgrent() for an example.