getpwnam(3) Library Functions Manual getpwnam(3) getpwnam, getpwnam_r, getpwuid, getpwuid_r - LIBRARY Standard C library (libc, -lc) #include #include struct passwd *getpwnam(const char *name); struct passwd *getpwuid(uid_t uid); int getpwnam_r(const char *restrict name, struct passwd *restrict pwd, char buf[restrict .buflen], size_t buflen, struct passwd **restrict result); int getpwuid_r(uid_t uid, struct passwd *restrict pwd, char buf[restrict .buflen], size_t buflen, 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). getpwnam_r() getpwuid_r() getpwnam() getpwuid(), passwd , pwd. passwd buf buflen. ( ) NULL ( ) *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 or ENOENT or ESRCH or EBADF or EPERM or ... 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 | +----------------------------+----------------------------------------------------------+--------------------------+ The pw_gecos field is not specified in POSIX, but is present on most implementations. 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 3 , . . , , . Linux man-pages 6.06 31 2023 . getpwnam(3)