Go to the previous, next section.

Scanning the List of All Users

This section explains how a program can read the list of all users in the system, one user at a time. The functions described here are declared in `pwd.h'.

You can use the fgetpwent function to read user entries from a particular file.

Function: struct passwd * fgetpwent (FILE *stream)

This function reads the next user entry from stream and returns a pointer to the entry. The structure is statically allocated and is rewritten on subsequent calls to fgetpwent. You must copy the contents of the structure if you wish to save the information.

This stream must correspond to a file in the same format as the standard password database file. This function comes from System V.

The way to scan all the entries in the user database is with setpwent, getpwent, and endpwent.

Function: void setpwent (void)

This function initializes a stream which getpwent uses to read the user database.

Function: struct passwd * getpwent (void)

The getpwent function reads the next entry from the stream initialized by setpwent. It returns a pointer to the entry. The structure is statically allocated and is rewritten on subsequent calls to getpwent. You must copy the contents of the structure if you wish to save the information.

Function: void endpwent (void)

This function closes the internal stream used by getpwent.

Go to the previous, next section.