[Previous]
[Contents]
[Next]

getpwnam()

get information about the user with a given name

Synopsis:

#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwnam( const char *name );

Description:

The getpwnam() function allows a process to gain more knowledge about user name. This function uses a static buffer that is overwritten by each call.

Returns:

A pointer to an object of type struct passwd containing an entry from the group database with a matching name. Upon error or failure to find a entry with a matching name, a NULL pointer is returned.

Examples:

/*
 * print information from the password entry
 * about the user name given as argv[1].
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>

void main( int argc, char **argv )
  {
    struct passwd *pw;

    if( ( pw = getpwnam( argv[1] ) ) == NULL ) {
      fprintf( stderr, "getpwnam: unknown %s\n",
        argv[1] );
      exit( EXIT_FAILURE );
    }
    printf( "login name  %s\n", pw->pw_name );
    printf( "user ID     %d\n", pw->pw_uid );
    printf( "group ID    %d\n", pw->pw_gid );
    printf( "home dir    %s\n", pw->pw_dir );
    printf( "login shell %s\n", pw->pw_shell );
    exit( EXIT_SUCCESS );
  }

Classification:

POSIX 1003.1

Safety:
Interrupt handler No
Signal handler No
Thread No

See also:

cuserid(), getlogin(), getpwent(), getpwuid()


[Previous]
[Contents]
[Next]