O'Reilly logo

Advanced UNIX Programming by Warren W. Gay

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required