The Password Database Routines

To ease the burden of searching the /etc/passwd file, the getpwent(3) function can be used:

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

struct passwd *getpwent(void);

void setpwent(void);

void endpwent(void);

The getpwent(3) function will automatically open the /etc/passwd file, if it hasn't already been opened. Then a database entry with the fields already parsed and converted is returned in the structure passwd. The returned pointer is valid only until the next call to getpwent(3) is made.

If the first entry did not contain the information you wanted, you can continue to call getpwent(3) for more entries until it returns a null pointer. The null pointer indicates that it has reached the end of the file (or an error ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.