A summary of the related functions of C language to group file processing _c language

Source: Internet
Author: User

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:

void setgrent (void);

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:

Getgrent, Setgrent

Header file:

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

To define a function:

void endgrent (void);

Function Description: Endgrent () is used to close the password file opened by Getgrent ().

Please refer to setgrent () for examples.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.