Team BBL
Previous Page Next Page

6.4. Group File

The UNIX System's group file, called the group database by POSIX.1, contains the fields shown in Figure 6.4. These fields are contained in a group structure that is defined in <grp.h>.

Figure 6.4. Fields in /etc/group file

Description

struct group member

POSIX.1

FreeBSD 5.2.1

Linux 2.4.22

Mac OS X 10.3

Solaris 9

group name

char *gr_name

encrypted password

char *gr_passwd

 

numerical group ID

int gr_gid

array of pointers to individual user names

char **gr_mem


The field gr_mem is an array of pointers to the user names that belong to this group. This array is terminated by a null pointer.

We can look up either a group name or a numerical group ID with the following two functions, which are defined by POSIX.1.

#include <grp.h>

struct group *getgrgid(gid_t gid);

struct group *getgrnam(const char *name);

Both return: pointer if OK, NULL on error


As with the password file functions, both of these functions normally return pointers to a static variable, which is overwritten on each call.

If we want to search the entire group file, we need some additional functions. The following three functions are like their counterparts for the password file.

#include <grp.h>

struct group *getgrent(void);

Returns: pointer if OK, NULL on error or end of file

void setgrent(void);

void endgrent(void);


These three functions are not part of the base POSIX.1 standard. They are defined as XSI extensions in the Single UNIX Specification. All UNIX Systems provide them.

The setgrent function opens the group file, if it's not already open, and rewinds it. The getgrent function reads the next entry from the group file, opening the file first, if it's not already open. The endgrent function closes the group file.

    Team BBL
    Previous Page Next Page