[Previous] [Contents] [Next]

getspent(), getspent_r()

Get an entry from the shadow password database

Synopsis:

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

struct spwd* getspent( void );

struct spwd* getspent_r( struct spwd* result, 
                         char* buffer, 
                         int buflen );

Library:

libc

Description:

The getspent() and getspent_r() functions return the next entry from the shadow password database. The getspent() function uses a static buffer that's overwritten by each call.


Note: The fgetspent(), getspent(), getspnam(), and functions share the same static buffer.

Returns:

The getspent() function returns a pointer to an object of type struct spwd containing the next entry from the shadow password database. When getspent() is first called, the database is opened, and remains open until either a NULL is returned to signify end-of-file, or endspent() is called.

Errors:

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

Examples:

#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>

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

int main(int argc, char** argv)
{
   struct spwd* sp;
    char  buf[80];

    setpwent( );
    while( gets( buf ) != NULL ) {
      if( ( sp = getspnam( buf ) ) != ( struct spwd * )0 ) {
        printf( "Valid login name is: %s\n", sp->sp_namp );
      } else {
        setspent( );
        while( ( sp=getspent( ) ) != ( struct spwd * )0 )
          printf( "%s\n", sp->sp_namp );
      }
    }
    endspent();
    return( EXIT_SUCCESS );
}

Classification:

Unix

getspent()

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread No

getspent_r()

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes

See also:

errno, fgetspent(), getgrent(), getlogin(), getspnam(), getpwuid()


[Previous] [Contents] [Next]