getpwuid() — Access the user database by user ID

Standards

Standards / Extensions C or C++ Dependencies

POSIX.1
XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _POSIX_SOURCE
#include <pwd.h>

struct passwd *getpwuid(uid_t uid);

General description

Gets information about a user with the specified uid. getpwuid() returns a pointer to a passwd structure containing an entry from the user database for the specified uid. This structure (defined in the pwd.h header file), contains the following members:
pw_name
User name
pw_uid
User ID (UID) number
pw_gid
Group ID (GID) number
pw_dir
Initial working directory
pw_shell
Initial user program

Return values may point to the static data that is overwritten on each call.

Returned value

If successful, getpwuid() returns a pointer.

If unsuccessful, getpwuid() returns a NULL pointer and sets errno to one of the following values:
Error Code
Description
EMVSSAF2ERR
The system authorization facility (SAF) or RACF Get GMAP or Get UMAP service had an error.
EMVSSAFEXTRERR
The SAF or RACF RACROUTE EXTRACT service had an error.

Example

CELEBG17
/* CELEBG17

   This example provides information for user ID 0.

 */
#define _POSIX_SOURCE
#include <sys/types.h>
#include <pwd.h>

main() {
  struct passwd *p;
  uid_t  uid=0;

  if ((p = getpwuid(uid)) == NULL)
    perror("getpwuid() error");
  else {
    printf("getpwuid() returned the following info for uid %d:\n",
           (int) uid);
    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);
  }
}
Output
getpwuid() returned the following info for uid 0:
  pw_name  : MEGA
  pw_uid   : 0
  pw_gid   : 512
  pw_dir   : /u/mega
  pw_shell : /bin/sh