[Previous]
[Contents]
[Next]

getpwent()

return the next entry from the password database

Synopsis:

#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwent( void );

Description:

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

Returns:

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.

Errors:

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

Examples:

/*
 * 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 <sys/types.h>
#include <pwd.h>

void main()
  {
    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 );
      }
    }
    endpwent();
  }

Classification:

UNIX

Safety:
Interrupt handler No
Signal handler No
Thread No

See also:

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


[Previous]
[Contents]
[Next]