gtpc2m3y | C/C++ Language Support User's Guide |
This function gets information about the user with the specified name.
Format
#include <pwd.h> struct passwd *getpwnam(char const *name)
Normal Return
If successful, the getpwnam function returns a pointer to struct passwd with an entry from the user database for the specified name.
The return value is a pointer to static data that is overwritten by each call.
struct passwd, defined in the pwd.h C header file, contains the following members:
Error Return
If unsuccessful, the getpwnam function returns a NULL pointer and sets errno to the appropriate value, as follows:
Programming Considerations
Examples
The following example provides information for user name tpfuser1.
#include <pwd.h> #include <stdio.h> int main(void) { char prthdr[] = "getpwnam() returned the following info for your user id:" struct passwd *p; char user[] = "tpfuser1"; if ( ( p = getpwnam ( user ) ) == NULL ) perror ( "getpwnam() error" ); else { printf( "%s\n", prthdr ); printf( " pw_name : %s\n", p->pw_name ); printf( " pw_uid : %d\n", (int) p->pw_uid ); printf( " pw_gid : %d\n", (int) p->pw_gid ); printf( " pw_dir : %s\n", p->pw_dir ); printf( " pw_shell : %s\n", p->pw_shell); } return 0; }
The getpwnam function returns the following information for the user ID of the calling function.
getpwnam() returned the following info for your user id: pwname : tpfuser1 pw_uid : 260 pw_gid : 100 pw_dir : /usr pw_shell :
Related Information