Name

getpwent, getpwnam, getpwnam_r, getpwuid, getpwuid_r, setpwent, endpwent
- password database operations

Library

libc.lib

Synopsis

  #include <sys/types.h>
  #include <pwd.h>
  struct passwd * getpwent (void);
  struct passwd * getpwnam (const char *login);
  int getpwnam_r (const char *name, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result);
  struct passwd * getpwuid (uid_t uid);
  int getpwuid_r (uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result);
  void setpwent (void);
  void endpwent (void);

Return values

The functions getpwent, getpwnam, and getpwuid return a valid pointer to a passwd structure on success or NULL if the entry is not found or if an error occurs. If an error does occur, errno will be set. The functions getpwnam_r, and getpwuid_r return 0 if no error occurred, or an error number to indicate failure. It is not an error if a matching entry is not found. (Thus, if result is NULL and the return value is 0, no matching entry exists.)

The endpwent and setpwent functions have no return value.


Detailed description

These APIs belongs to set which are only build supported and not available functionally. The reason why only build support is extended to these APIs is that Symbian operating system does not support the creation of multiple users and groups in its environment. Moreover it may not make sense to have multiple users and groups in the context of a mobile environment.

We refer to the structure
  #include <pwd.h>
struct passwd {
        char    *pw_name;       /* user name */
        char    *pw_passwd;     /* encrypted password */
        uid_t   pw_uid;         /* user uid */
        gid_t   pw_gid;         /* user gid */
        time_t  pw_change;      /* password change time */
        char    *pw_class;      /* user access class */
        char    *pw_gecos;      /* Honeywell login info */
        char    *pw_dir;        /* home directory */
        char    *pw_shell;      /* default shell */
        time_t  pw_expire;      /* account expiration */
        int     pw_fields;      /* internal: fields filled in */
};


The functions getpwnam and getpwuid returns the default values for a single user system i.e., pw_name,pw_passwd,pw_uid and pw_gid set to root,NULL,0 and 0 respectively in case the input parameter uid/login is 0/ root else the error number is set and NULL is returned by the APIs.

The getpwent when called the first time by an application returns the pointer to a valid passwd structure,that of the simulated single-user group.Subsequent calls return NULL until setpwent is called

The functions getpwnam_r, and getpwuid_r are thread-safe versions of getpwnam, and getpwuid, respectively. The caller must provide storage for the results of the search in the pwd, buffer, bufsize, and result arguments. When these functions are successful, the pwd argument will be filled-in, and a pointer to that argument will be stored in result. If an entry is not found or an error occurs, result will be set to NULL.

The setpwent function sets the library such that subsequent getpwent call returns a filled passwd structure.

endpwent function sets up the library such that the subsequent getpwent call returns NULL.


Examples

#include <pwd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*A user function that takes uid as parameter to call getpwuid
Can be replaced by function that takes char* pwnam to call getpwnam*/
void pwuid_user(int uid)
{
        struct passwd* pw_uid;  /*This struct is filled by the getpwuid call*/
        setpwent ();            /*Set read to start of database*/
        /*Call to getpwuid with uid as parameter..could also be replaced with getpwnam
        with pwnam as parameter*/
        pw_uid = getpwuid (uid);
        if(pw_uid == NULL)      /*End of database reached*/
                return;
        else
                printf ("For uid = %d ,user name = %s", uid, pw_uid->pw_name); /*Print a valid user name*/
        endpwent ();            /*Set the read pointer to the end of database
        /*In case of our library,the user has to free the resources allocated
        by the API call itself*/
        free (pw_uid->pw_mem);
        free (pw);
}


Output

For uid = 0 ,user name = root


#include <pwd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*A user function that takes char* pwnam as parameter to call getpwnam_r
Can be replaced by function that takes int uid to call getpwuid_r*/
void pwuid_r_user(char* pwnam)
{
        /*We have to allocate memory for the structures for the getpwnam_r thread-safe call*/
        struct passwd* pw_nam = (struct passwd*)malloc (sizeof (struct passwd));
        pw_nam->pw_mem = (char**)malloc (sizeof(char**));
        char* buffer=(char*)malloc(sizeof(struct passwd));
        size_t bufsize=sizeof(buffer);
        struct passwd* tempresult;
        int result;     
        setpwent ();            /*Set read to start of database*/
        /*Call to getpwnam_r with pwnam as parameter..could also be replaced with getpwuid_r
        with pwuid as parameter*/
        int result=getpwnam_r(pwnam, pw_nam, buffer, bufsize, &tempresult);
        if(result != 0) /*End of database reached*/
                return;
        else
                printf ("For user = %s ,uid = "%d", pwnam, pw_nam->pw_uid); /*Print a valid uid*/
        endpwent ();            /*Set the read pointer to the end of database*/
        /*The user has to free the resources allocated*/
        free(buffer);   
        free (pw_uid->pw_mem);
        free (pw);
}


Output

For pwnam = root uid = 0


#include <pwd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*A user function that takes calls getpwent API*/
void getpwent_user()
{
        struct passwd* pw_ent;  /*This struct is filled by the getpwent call*/
        setpwent ();            /*Set read to start of database*/
        /*Call to getpwent */
        pw_ent = getpwent ();
        if(pw_ent == NULL)      /*End of database reached*/
                return;
        else
                /*Print a valid passwd name*/
                printf ("uid = %d ,user name = %s", pw_ent->uid, pw_ent->pw_name);
        endpwent ();            /*Set the read pointer to the end of database*/
        /*In case of our library,the user has to free the resources allocated
        by the API call itself*/
        free (pw_ent->pw_mem);
        free (pw_ent);
}


Output

uid = 0 ,user name = root


Errors

The getpwuid,getpwnam will fail if:
[ERANGE]
  Not enough memory to allocate the structures.
[ENOENT]
  If an invalid uid or login is passed to the APIs.

The getpwuid_r,getpwnam_r will fail if:

[ENOENT]
  If an invalid uid or pwnam is passed to the APIs.

See also

getgrent

Bugs

The functions getpwent, getpwnam, and getpwuid, leave their results in an internal static object and return a pointer to that object. Subsequent calls to the same function will modify the same object.

The functions getpwent, endpwent, and setpwent are fairly useless in a networked environment and should be avoided, if possible


Feedback

For additional information or queries on this page send feedback

© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user.

Top