#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); |
The endpwent and setpwent functions have no return value.
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. |
#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
[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. | |
The functions getpwent, endpwent, and setpwent are fairly useless in a networked environment and should be avoided, if possible
© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user. |