Caution: This version of this document is no longer maintained. For the latest documentation, see


Get the next entry from the password database


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

struct passwd* getpwent( void );



Use the -l c option to qcc to link against this library. This library is usually included automatically.


The getpwent() function returns the next entry from the password database. This function uses a static buffer that's overwritten by each call.

Note: The getpwent(), getpwnam(), and getpwuid(), functions share the same static buffer. For a reentrant version of this function, see getpwent_r().


A pointer to an object of type struct passwd containing the next entry from the password database. When getpwent() is first called, the password database is opened, and remains open until either a NULL is returned to signify end-of-file, or endpwent() is called.


The getpwent() function uses the following functions, and as a result, errno can be set to an error for any of these calls:


 * This program loops, reading a login name from standard
 * input and checking to see if it is a valid name. If it 
 * is not valid, the entire contents of the name in the 
 * password database are printed.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>

int main( void )
    struct passwd* pw;
    char    buf[80];

    setpwent( );
    while( gets( buf ) != NULL ) {
      if( ( pw = getpwnam( buf ) ) != ( struct passwd * )0 ) {
        printf( "Valid login name is: %s\n", pw->pw_name );
      } else {
        setpwent( );
        while( ( pw=getpwent( ) ) != ( struct passwd * )0 )
          printf( "%s\n", pw->pw_name );
    return( EXIT_SUCCESS );


POSIX 1003.1 XSI

Cancellation point Yes
Interrupt handler No
Signal handler No
Thread No

See also:

endpwent(), errno, getgrent(), getlogin(), getpwent_r(), getpwnam(), getpwuid(), setpwent()