Node:Scanning All Groups, Previous:Lookup Group, Up:Group Database



Scanning the List of All Groups

This section explains how a program can read the list of all groups in the system, one group at a time. The functions described here are declared in grp.h.

You can use the fgetgrent function to read group entries from a particular file.

struct group * fgetgrent (FILE *stream) Function
The fgetgrent function reads the next entry from stream. It returns a pointer to the entry. The structure is statically allocated and is overwritten on subsequent calls to fgetgrent. You must copy the contents of the structure if you wish to save the information.

The stream must correspond to a file in the same format as the standard group database file.

int fgetgrent_r (FILE *stream, struct group *result_buf, char *buffer, size_t buflen, struct group **result) Function
This function is similar to fgetgrent in that it reads the next user entry from stream. But the result is returned in the structure pointed to by result_buf. The first buflen bytes of the additional buffer pointed to by buffer are used to contain additional information, normally strings which are pointed to by the elements of the result structure.

This stream must correspond to a file in the same format as the standard group database file.

If the function returns zero result points to the structure with the wanted data (normally this is in result_buf). If errors occurred the return value is non-zero and result contains a null pointer.

The way to scan all the entries in the group database is with setgrent, getgrent, and endgrent.

void setgrent (void) Function
This function initializes a stream for reading from the group data base. You use this stream by calling getgrent or getgrent_r.

struct group * getgrent (void) Function
The getgrent function reads the next entry from the stream initialized by setgrent. It returns a pointer to the entry. The structure is statically allocated and is overwritten on subsequent calls to getgrent. You must copy the contents of the structure if you wish to save the information.

int getgrent_r (struct group *result_buf, char *buffer, size_t buflen, struct group **result) Function
This function is similar to getgrent in that it returns the next entry from the stream initialized by setgrent. Like fgetgrent_r, it places the result in user-supplied buffers pointed to result_buf and buffer.

If the function returns zero result contains a pointer to the data (normally equal to result_buf). If errors occurred the return value is non-zero and result contains a null pointer.

void endgrent (void) Function
This function closes the internal stream used by getgrent or getgrent_r.