#include <pwd.h>
int getpwent_r(size_t size;
struct passwd *restrict pwbuf,
char buf[restrict size], size_t size,
struct passwd **restrict pwbufp);
int fgetpwent_r(size_t size;
FILE *restrict stream, struct passwd *restrict pwbuf,
char buf[restrict size], size_t size,
struct passwd **restrict pwbufp);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
getpwent_r(),
glibc 2.19 以降:
_DEFAULT_SOURCE
glibc 2.19 以前:
_BSD_SOURCE || _SVID_SOURCE
fgetpwent_r():
glibc 2.19 以降:
_DEFAULT_SOURCE
glibc 2.19 以前:
_SVID_SOURCE
passwd 構造体は、<pwd.h> で以下のように定義されている:
struct passwd {
char *pw_name; /* ユーザー名 */
char *pw_passwd; /* ユーザーのパスワード */
uid_t pw_uid; /* ユーザー ID */
gid_t pw_gid; /* グループ ID */
char *pw_gecos; /* ユーザー情報 */
char *pw_dir; /* ホームディレクトリ */
char *pw_shell; /* シェルプログラム */
};
この構造体のフィールドの詳細は passwd(5) を参照のこと。
The nonreentrant functions return a pointer to static storage, where this static storage contains further pointers to user name, password, gecos field, home directory and shell. The reentrant functions described here return all of that in caller-provided buffers. First of all there is the buffer pwbuf that can hold a struct passwd. And next the buffer buf of size size that can hold additional strings. The result of these functions, the struct passwd read from the stream, is stored in the provided buffer *pwbuf, and a pointer to this struct passwd is returned in *pwbufp.
| インターフェース | 属性 | 値 |
| getpwent_r() | Thread safety |
MT-Unsafe race:pwent locale
|
| fgetpwent_r() | Thread safety | MT-Safe |
In the above table, pwent in race:pwent signifies that if any of the functions setpwent(), getpwent(), endpwent(), or getpwent_r() are used in parallel in different threads of a program, then data races could occur.
struct passwd * getpwent_r(struct passwd *pwd, char buf[.size], int size);
より良いものでは、以下のようになっている。
int
getpwent_r(struct passwd *pwd, char buf[.size], int size,
FILE **pw_fp);
#define _GNU_SOURCE
#include <pwd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFLEN 4096
int
main(void)
{
struct passwd pw;
struct passwd *pwp;
char buf[BUFLEN];
int i;
setpwent();
for (;;) {
i = getpwent_r(&pw, buf, sizeof(buf), &pwp);
if (i)
break;
printf("%s (%jd)\tHOME %s\tSHELL %s\n", pwp->pw_name,
(intmax_t) pwp->pw_uid, pwp->pw_dir, pwp->pw_shell);
}
endpwent();
exit(EXIT_SUCCESS);
}