getpwnam(3) Library Functions Manual getpwnam(3) getpwnam, getpwnam_r, getpwuid, getpwuid_r - C (libc, -lc) #include #include struct passwd *getpwnam(const char *name); struct passwd *getpwuid(uid_t uid); int getpwnam_r(size_t size; const char *restrict name, struct passwd *restrict pwd, char buf[restrict size], size_t size, struct passwd **restrict result); int getpwuid_r(size_t size; uid_t uid, struct passwd *restrict pwd, char buf[restrict size], size_t size, struct passwd **restrict result); glibc (. feature_test_macros(7)): getpwnam_r(), getpwuid_r(): _POSIX_C_SOURCE || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE getpwnam() , (, /etc/passwd, NIS LDAP), name. getpwuid() , , uid. passwd : struct passwd { char *pw_name; /* */ char *pw_passwd; /* */ uid_t pw_uid; /* */ gid_t pw_gid; /* */ char *pw_gecos; /* */ char *pw_dir; /* */ char *pw_shell; /* */ }; passwd(5). The getpwnam_r() and getpwuid_r() functions obtain the same information as getpwnam() and getpwuid(), but store the retrieved passwd structure in the space pointed to by pwd. The string fields pointed to by the members of the passwd structure are stored in the buffer buf of size size. A pointer to the result (in case of success) or NULL (in case no entry was found or an error occurred) is stored in *result. sysconf(_SC_GETPW_R_SIZE_MAX) -1 errno buf ( , ERANGE; ). The getpwnam() and getpwuid() functions return a pointer to a passwd structure, or NULL if the matching entry is not found or an error occurs. If an error occurs, errno is set to indicate the error. If one wants to check errno after the call, it should be set to zero before the call. getpwent(3), getpwnam() getpwuid() ( free(3)). getpwnam_r() getpwuid_r() , *result pwd. , 0 NULL *result. *result NULL. 0 ENOENT ESRCH EBADF EPERM ... name uid . EINTR ; . signal(7). EIO -. EMFILE . ENFILE . ENOMEM passwd. ERANGE . /etc/passwd , attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |getpwnam() | | MT-Unsafe race:pwnam | | | | locale | +----------------------------+----------------------------------------------------------+--------------------------+ |getpwuid() | | MT-Unsafe race:pwuid | | | | locale | +----------------------------+----------------------------------------------------------+--------------------------+ |getpwnam_r(), getpwuid_r() | | MT-Safe locale | +----------------------------+----------------------------------------------------------+--------------------------+ pw_gecos POSIX, . POSIX.1-2008. POSIX.1-2001, SVr4, 4.3BSD. << >>, , POSIX.1-2001. << >> , errno . . POSIX , errno , . UNIX- , : 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM , , . pw_dir . HOME . , HOME ( getpwuid(getuid())->pw_dir), << >> . () getpwnam(" ")->pw_dir . getpwnam_r() ID . #include #include #include #include #include #include int main(int argc, char *argv[]) { struct passwd pwd; struct passwd *result; char *buf; long bufsize; int s; if (argc != 2) { fprintf(stderr, "Usage: %s username\n", argv[0]); exit(EXIT_FAILURE); } bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); if (bufsize == -1) /* Value was indeterminate */ bufsize = 16384; /* Should be more than enough */ buf = malloc(bufsize); if (buf == NULL) { perror("malloc"); exit(EXIT_FAILURE); } s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result); if (result == NULL) { if (s == 0) printf("Not found\n"); else { errno = s; perror("getpwnam_r"); } exit(EXIT_FAILURE); } printf("Name: %s; UID: %jd\n", pwd.pw_gecos, (intmax_t) pwd.pw_uid); exit(EXIT_SUCCESS); } endpwent(3), fgetpwent(3), getgrnam(3), getpw(3), getpwent(3), getspnam(3), putpwent(3), setpwent(3), passwd(5) () Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () . Linux man-pages 6.18 8 2026 . getpwnam(3)